Results 1 to 4 of 4

Thread: [RESOLVED] Timer multi-threading help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Location
    Botswana
    Posts
    107

    Resolved [RESOLVED] Timer multi-threading help

    hi everyone. can anyone help me with using the timer component to run another thread. when my program is running, i want to run a seperate thread in the background, to run a function that will check a device connected to a usb drive.
    the problem is, when the timer calls the function, my whole program freezes up until the timer finishes (this timer has to run through out the life span of the program)

    any suggestions?

    im using visual studio 2008 express edition (visual basic). i add the timer component (enabled= true, interval=2000 (every 2 seconds). when the timer event firest, i call a function.
    I am using Microsoft Visual Basic 2008 Express Edition. I use Access for my data base

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Timer multi-threading help

    Check out BackgroundWorker on MSDN. It has a lot of good examples and is very simple to use.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Timer multi-threading help

    Assuming that you only want one instance of this task executing at a time, add a BackgroundWorker and call its RunWorkerAsync method in the Timer's Tick event handler. See my signature for an example. Note that if there's any chance that the previous task won't have finished when the Timer Ticks then you must test the IsBusy property of the BGW.

    If you do, or might, want multiple instances of the task running at the same time then you can either create Thread objects explicitly or else use the ThreadPool class.
    vb.net Code:
    1. Private Sub Timer1_Tick(ByVal sender As Object, _
    2.                         ByVal e As EventArgs) Handles Timer1.Tick
    3.     Dim t As New Thread(AddressOf SomeTask)
    4.  
    5.     t.Start()
    6. End Sub
    7.  
    8. Private Sub SomeTask()
    9.     '...
    10. End Sub
    vb.net Code:
    1. Private Sub Timer1_Tick(ByVal sender As Object, _
    2.                         ByVal e As EventArgs) Handles Timer1.Tick
    3.     ThreadPool.QueueUserWorkItem(AddressOf SomeTask)
    4. End Sub
    5.  
    6. Private Sub SomeTask(ByVal data As Object)
    7.     '...
    8. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Location
    Botswana
    Posts
    107

    Re: Timer multi-threading help

    thanks. the links helped. i think i got it working
    I am using Microsoft Visual Basic 2008 Express Edition. I use Access for my data base

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width