|
-
Jun 26th, 2006, 10:14 AM
#1
Thread Starter
Hyperactive Member
Thread help
Okay I have the folowing code.
VB Code:
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
mescounter = mescounter + 1
Me.txtmescount.Text = 60 - mescounter
If mescounter = 60 Then
processall() ' process all waiting message and e-mails
mescounter = 0
End If
If doweb Then
If curday <> 1 And curday <> 7 Then
If curhour > 7 And curhour < 18 Then
webcounter = webcounter + 1
Me.txtwebcount.Text = 60 - webcounter
If webcounter = 60 Then
processweb() ' exports call data to website
webcounter = 0
End If
Else
webcounter = webcounter + 1
If webcounter = 60 Then
webcounter = 0
End If
Me.txtwebcount.Text = "Paused OOH"
End If
Else
webcounter = webcounter + 1
If webcounter = 60 Then
webcounter = 0
End If
Me.txtwebcount.Text = "Paused W/E"
End If
End If
End Sub
Private Sub processweb()
If processingweb = False Then
processingweb = True
Me.Pbweb.Value = 10
Me.txtwebcall.Text = CStr(CLng(Me.txtwebcall.Text) + upwebsite.getupcalldata())
Me.Pbweb.Value = 20
Me.txtwebcomp.Text = CStr(CLng(Me.txtwebcomp.Text) + upwebsite.getupcompdata())
Me.Pbweb.Value = 50
Me.txtwebemp.Text = CStr(CLng(Me.txtwebemp.Text) + upwebsite.getupempdata())
Me.Pbweb.Value = 70
Me.txtwebcon.Text = CStr(CLng(Me.txtwebcon.Text) + upwebsite.getupcondata())
Me.Pbweb.Value = 90
Me.txtremwebemp.Text = CStr(CLng(Me.txtremwebemp.Text) + upwebsite.getwebchanges())
Me.Pbweb.Value = 100
Me.Pbweb.Value = 0
processingweb = False
End If
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.
-
Jun 26th, 2006, 05:33 PM
#2
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?
-
Jun 27th, 2006, 04:29 AM
#3
Thread Starter
Hyperactive Member
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?
-
Jun 27th, 2006, 05:54 PM
#4
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.
-
Jun 28th, 2006, 08:34 AM
#5
Thread Starter
Hyperactive Member
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:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
t = New Thread(AddressOf processweb)
t.IsBackground = True
t.Start()
End Sub
Private Sub processweb()
Do
startwebcount = True
Me.txtwebcount.Text = 30
Thread.Sleep(30000)
startwebcount = False
If doweb = True Then
Me.Pbweb.Value = 10
Me.txtwebcall.Text = CStr(CLng(Me.txtwebcall.Text) + upwebsite.getupcalldata())
Thread.Sleep(0)
Me.Pbweb.Value = 20
Me.txtwebcomp.Text = CStr(CLng(Me.txtwebcomp.Text) + upwebsite.getupcompdata())
Thread.Sleep(0)
Me.Pbweb.Value = 50
Me.txtwebemp.Text = CStr(CLng(Me.txtwebemp.Text) + upwebsite.getupempdata())
Thread.Sleep(0)
Me.Pbweb.Value = 70
Me.txtwebcon.Text = CStr(CLng(Me.txtwebcon.Text) + upwebsite.getupcondata())
Thread.Sleep(0)
Me.Pbweb.Value = 90
Me.txtremwebemp.Text = CStr(CLng(Me.txtremwebemp.Text) + upwebsite.getwebchanges())
Me.Pbweb.Value = 100
Me.Pbweb.Value = 0
End If
Loop
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|