Results 1 to 5 of 5

Thread: Thread help

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Thread help

    Okay I have the folowing code.
    VB Code:
    1. Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
    2.         mescounter = mescounter + 1
    3.  
    4.         Me.txtmescount.Text = 60 - mescounter
    5.  
    6.         If mescounter = 60 Then
    7.  
    8.  
    9.             processall() ' process all waiting message and e-mails
    10.             mescounter = 0
    11.  
    12.  
    13.         End If
    14.  
    15.         If doweb Then
    16.  
    17.             If curday <> 1 And curday <> 7 Then
    18.                 If curhour > 7 And curhour < 18 Then
    19.                     webcounter = webcounter + 1
    20.  
    21.                     Me.txtwebcount.Text = 60 - webcounter
    22.                     If webcounter = 60 Then
    23.  
    24.                         processweb() ' exports call data to website
    25.  
    26.                         webcounter = 0
    27.                     End If
    28.                 Else
    29.                     webcounter = webcounter + 1
    30.                     If webcounter = 60 Then
    31.                         webcounter = 0
    32.                     End If
    33.                     Me.txtwebcount.Text = "Paused OOH"
    34.                 End If
    35.             Else
    36.                 webcounter = webcounter + 1
    37.                 If webcounter = 60 Then
    38.                     webcounter = 0
    39.                 End If
    40.                 Me.txtwebcount.Text = "Paused W/E"
    41.             End If
    42.  
    43.  
    44.         End If
    45.  
    46.     End Sub
    47.  
    48.     Private Sub processweb()
    49.         If processingweb = False Then
    50.             processingweb = True
    51.  
    52.             Me.Pbweb.Value = 10
    53.             Me.txtwebcall.Text = CStr(CLng(Me.txtwebcall.Text) + upwebsite.getupcalldata())
    54.             Me.Pbweb.Value = 20
    55.             Me.txtwebcomp.Text = CStr(CLng(Me.txtwebcomp.Text) + upwebsite.getupcompdata())
    56.             Me.Pbweb.Value = 50
    57.             Me.txtwebemp.Text = CStr(CLng(Me.txtwebemp.Text) + upwebsite.getupempdata())
    58.             Me.Pbweb.Value = 70
    59.             Me.txtwebcon.Text = CStr(CLng(Me.txtwebcon.Text) + upwebsite.getupcondata())
    60.  
    61.             Me.Pbweb.Value = 90
    62.             Me.txtremwebemp.Text = CStr(CLng(Me.txtremwebemp.Text) + upwebsite.getwebchanges())
    63.  
    64.             Me.Pbweb.Value = 100
    65.             Me.Pbweb.Value = 0
    66.  
    67.             processingweb = False
    68.         End If
    69.      
    70.  
    71.     End Sub

    Now my problem is that the processweb() sub can sometimes freeze for minutes on end if our Internet conection is down or running very slowly, currently when this happens the other sub processall() doesn't get run. I'm assuming the whole application is locking up until the processweb() task completes.
    To solve this problem would you use threads? and how easy would it be to convert the above to threading, basically I know nothing about threading or even if this is what I need, so perhaps some general pointers would be useful.

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

    Re: Thread help

    It is presumably your calls to members of upwebsite that are holding things up. What do they look like? Can you implement some sort of timeout to cancel them if they take too long?
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: Thread help

    Each one selects some local data, and then updates or inserts this to a remote mysql database, the procedure is hanging on the updating of the remote mysql database, this update normally runs fine but if someone else here is hitting our internet connection hard then this update procedure starts running like an asthmatic slug.

    My question is more about how I handle this situation, I would still like this update to run but more as a background process, which is why I assume I have to use threads.
    My problem being is that I get the feeling that threads can be quite difficult to use and you can end up with unexpected consequences.

    Okay question 1.
    Should I use threads in this situation and if not what should I use?

    Question 2
    If using threads do I have to worry about casusing the sun to explode or more sensibly do I have to worry about things going wierdly wrong?

    Question 3
    By how many goals do you belive England will win by on Saturday?

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

    Re: Thread help

    A single thread can only perform a single task at any one time. If one of your tasks can take a long time and is preventing your main thread from performing other tasks then multi-threading is probably a good option. You haven't specified your version (please do so in future) but if you're suing VB 2005 then the BackgroundWorker class makes your job easier. I suggest that you follow the Articles -> Advanced .NET link in my signature and read the Managed Threading section. If you're using .NET 1.x then it won't all be relevant but most of it will.
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: Thread help

    Oh sorry am using vb 2003.

    Okay had a little read off all that boring thread stuff, then reverted to the method that mankind has been using for millenia.
    Give it a go without any understanding of the consequences.
    So using the above theory have come up with this, which seems to work.

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.  
    3.    t = New Thread(AddressOf processweb)
    4.             t.IsBackground = True
    5.             t.Start()
    6.  
    7. End Sub
    8.  
    9.  
    10.  Private Sub processweb()
    11.         Do
    12.             startwebcount = True
    13.             Me.txtwebcount.Text = 30
    14.             Thread.Sleep(30000)
    15.             startwebcount = False
    16.             If doweb = True Then
    17.                 Me.Pbweb.Value = 10
    18.                 Me.txtwebcall.Text = CStr(CLng(Me.txtwebcall.Text) + upwebsite.getupcalldata())
    19.                 Thread.Sleep(0)
    20.                 Me.Pbweb.Value = 20
    21.                 Me.txtwebcomp.Text = CStr(CLng(Me.txtwebcomp.Text) + upwebsite.getupcompdata())
    22.                 Thread.Sleep(0)
    23.                 Me.Pbweb.Value = 50
    24.                 Me.txtwebemp.Text = CStr(CLng(Me.txtwebemp.Text) + upwebsite.getupempdata())
    25.                 Thread.Sleep(0)
    26.                 Me.Pbweb.Value = 70
    27.                 Me.txtwebcon.Text = CStr(CLng(Me.txtwebcon.Text) + upwebsite.getupcondata())
    28.                 Thread.Sleep(0)
    29.                 Me.Pbweb.Value = 90
    30.                 Me.txtremwebemp.Text = CStr(CLng(Me.txtremwebemp.Text) + upwebsite.getwebchanges())
    31.                 Me.Pbweb.Value = 100
    32.                 Me.Pbweb.Value = 0
    33.             End If
    34.  
    35.         Loop
    36.     End Sub

    As I say this seems to work, but who knows, not me as reading stuff hurts my head, so if anyone wiser and more literate than me could confirm or deny if the above makes sense.
    Cheers

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