8 Replies - 5266 Views - Last Post: 29 September 2012 - 08:00 AM Rate Topic: -----

#1 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 131
  • Joined: 21-January 12

System.Thread.Timer

Posted 29 September 2012 - 02:31 AM

hi guys, i'm new in vb.net, can you please tell me why my form automatically close after i run this code?

Public Class Form1
    Private myTimer As System.Threading.Timer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myCallback As New System.Threading.TimerCallback(AddressOf Task1)
        myTimer = New System.Threading.Timer(myCallback, Nothing, 100, 100)
    End Sub
    Private Sub Task1(ByVal state As Object)
        Label1.Left -= 1
        If Label1.Right <= Me.Left Then
            Label1.Left = Me.Right
        End If
    End Sub
End Class



i try to make scrolling text with that code.

Is This A Good Question/Topic? 0
  • +

Replies To: System.Thread.Timer

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: System.Thread.Timer

Posted 29 September 2012 - 02:51 AM

Let me show you why, by example (notice the increased period parameter (last one) for myTimer, because you don't want to see messagebox ever 0.1 seconds):

Public Class Form1
    Private myTimer As System.Threading.Timer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myCallback As New System.Threading.TimerCallback(AddressOf Task1)
        myTimer = New System.Threading.Timer(myCallback, Nothing, 100, 10000)
    End Sub

    Private Sub Task1(ByVal state As Object)
        Try
            Label1.Left -= 1
            If Label1.Right <= Me.Left Then
                Label1.Left = Me.Right
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

This post has been edited by lucky3: 29 September 2012 - 02:53 AM

Was This Post Helpful? 1
  • +
  • -

#3 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 131
  • Joined: 21-January 12

Re: System.Thread.Timer

Posted 29 September 2012 - 03:10 AM

thanks alot, you are the man :gun_bandana:

delegate.. isn't it?

can you give code with delegate in it? :helpsmilie:

This post has been edited by artemix22: 29 September 2012 - 03:11 AM

Was This Post Helpful? 0
  • +
  • -

#4 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: System.Thread.Timer

Posted 29 September 2012 - 03:37 AM

You are right: the delegate. I put my code in spoiler, because you might want to try to solve it alone first. I also modified the parameter type, because if any object can be passed to Task1, you might get errors again. Not every object has Left or Width property.
Spoiler

Was This Post Helpful? 1
  • +
  • -

#5 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 131
  • Joined: 21-January 12

Re: System.Thread.Timer

Posted 29 September 2012 - 05:38 AM

thats what i'm talking about, thanks again dude! you are really the man :gun_bandana:

i spend whole this day to solve this stuff :sweatdrop:

and here is what i do (with my listbox too), so i didn't get any lag issue again in my scrolling text :D

Public Class Form1
    Private myTimer, myTimer2 As System.Threading.Timer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myCallback As New System.Threading.TimerCallback(AddressOf Task1)
        myTimer = New System.Threading.Timer(myCallback, Label1, 10, 10)
    End Sub
    Private Delegate Sub LabelDelegate(ByVal myLabel As Label)
    Private Sub Task1(ByVal myLabel As Label)
        If myLabel.InvokeRequired Then
            myLabel.Invoke(New LabelDelegate(AddressOf Task1), myLabel)
        Else
            myLabel.Left -= 1
            If myLabel.Left <= 0 - myLabel.Width Then
                myLabel.Left = Me.Width
            End If
        End If
    End Sub



    Private Delegate Sub ListBoxDelegate(ByVal myListBox As ListBox)
    Private Sub Task2(ByVal myListBox As ListBox)
        If myListBox.InvokeRequired Then
            myListBox.Invoke(New ListBoxDelegate(AddressOf Task2), myListBox)
        Else
            If ListBox1.Items.Count = 0 Then
            ElseIf ListBox1.SelectedIndex >= ListBox1.Items.Count - 1 Then
                ListBox1.SelectedIndex = 0
                Panel1.BackgroundImage = Image.FromFile(ListBox1.SelectedItem.ToString)
            Else
                ListBox1.SelectedIndex += 1
                Panel1.BackgroundImage = Image.FromFile(ListBox1.SelectedItem.ToString)
            End If
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.SelectedIndex = 0
        Dim myCallback2 As New System.Threading.TimerCallback(AddressOf Task2)
        myTimer2 = New System.Threading.Timer(myCallback2, ListBox1, 3000, 3000)
    End Sub
End Class


This post has been edited by artemix22: 29 September 2012 - 06:07 AM

Was This Post Helpful? 0
  • +
  • -

#6 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: System.Thread.Timer

Posted 29 September 2012 - 06:52 AM

Slide show stuff ;) Great! I'm glad to see how easily you adapted delegate to your further needs. BTW, if your ListBox1 is not always populated, than you might get an error when clicking button1 (SelectedIndex = 0). Or if it's not populated with paths to image files, you might catch an error in your Task2 method. But that's just IF... ;)

Edit: one more thing: you'd perhaps want to handle form resizing and scrolling text position adjusting with that.

This post has been edited by lucky3: 29 September 2012 - 06:59 AM

Was This Post Helpful? 1
  • +
  • -

#7 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 131
  • Joined: 21-January 12

Re: System.Thread.Timer

Posted 29 September 2012 - 07:09 AM

you mean this ? :D

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ListBox1.Items.Count = 0 Then
        Else
            ListBox1.SelectedIndex = 0
            Dim myCallback2 As New System.Threading.TimerCallback(AddressOf Task2)
            myTimer2 = New System.Threading.Timer(myCallback2, ListBox1, 3000, 3000)
        End If
    End Sub




for form size i use windows state maximized, it's for display purpose on LCD TV.

This post has been edited by artemix22: 29 September 2012 - 07:13 AM

Was This Post Helpful? 0
  • +
  • -

#8 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: System.Thread.Timer

Posted 29 September 2012 - 07:28 AM

You can also do it without Else: If ListBox1.Items.Count > 0 Then. The same goes for Task2 if... You might want to add Try...Catch in your Task2, because if this app is for some third person, to use it for displaying purposes in shop or similar, they might add folder to listbox, where non image files are present.

This post has been edited by lucky3: 29 September 2012 - 07:32 AM

Was This Post Helpful? 0
  • +
  • -

#9 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 131
  • Joined: 21-January 12

Re: System.Thread.Timer

Posted 29 September 2012 - 08:00 AM

no dude, picture path came from database, so, it must be no folder while they use it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1