To learn well "Multithreading" I have three buttons, two text boxes and a picture box on a form, each of the three doing completely different things (adding, subtracting and showing pictures).
The first time I tried this programm I used the sub:
Me.CheckForIllegalCrossThreadCalls = False I think You know that frase.
I could pushed wich ever button and when I want, the appropiate thread started immidiately again. That was excactly what I want.
But hey, I've learned that is not the way I should programm!
So now I'm using the following:
If Label1.InvokeRequired Then
Me.BeginInvoke(New UpdateLabel1Delegate(AddressOf UpdateLabel1Text), message)
Else
DoSomething
End If
End Sub
The three "Threads" are started by pressing the appropriate button. Works fine!
I can press the buttons neatly, but I have to wait until a thread is ready and then start a next thread. I can NOT press any button at random and at any time to restart the relevant thread.
Is there someone who can give me a clue or specific hint how to do this? Hopefully with a piece of relevant code. I've search a lot on the internet, it must be there, but where?
Thanks in advance,
Eric
P.S. Edited code:
Imports System.Threading
Public Class Form1
Dim Number1 As Integer
Dim Number2 As Integer
Dim Thread1 As System.Threading.Thread
Dim Thread2 As System.Threading.Thread
Dim Thread3 As System.Threading.Thread
Private Delegate Sub UpdateLabel1Delegate(ByVal Number1 As Integer)
Private Delegate Sub UpdateLabel2Delegate(ByVal Number2 As Integer)
Private Delegate Sub UpdatePicturebox1Delegate()
Private Delegate Sub Button1_Clickdelegate(sender As Object, e As EventArgs)
Private Delegate Sub Button2_Clickdelegate(sender As Object, e As EventArgs)
Private Delegate Sub Button3picturebox1delegate(sender As Object, e As EventArgs)
Dim FirstTime1 As Boolean = True
Dim FirstTime2 As Boolean = True
Dim FirstTime3 As Boolean = True
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Thread1.Abort()
If Button1.InvokeRequired Then
Me.BeginInvoke(New Button1_Clickdelegate(AddressOf Button1_Click))
Else
Thread1 = New System.Threading.Thread(AddressOf CountUp)
Thread1.Start() T
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Button2.InvokeRequired Then
Me.BeginInvoke(New Button2_Clickdelegate(AddressOf Button2_Click))
Else
Thread2 = New System.Threading.Thread(AddressOf CountDown)
Thread2.Start()
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If PictureBox1.InvokeRequired Then
Me.BeginInvoke(New Button3picturebox1delegate(AddressOf Button3_Click))
Else
Thread3 = New System.Threading.Thread(AddressOf UpdatePicturebox1)
Thread3.Start()
End If
End Sub
Private Sub CountUp()
Number1 = 0
Do Until Number1 = 1000
Number1 = Number1 + 1
UpdateLabel1Text(Number1)
Loop
End Sub
Private Sub CountDown()
Number2 = 1000
Do Until Number2 = 0
Number2 = Number2 - 1
UpdateLabel2Text(Number2)
Loop
End Sub
Private Sub UpdateLabel1Text(ByVal Number1 As Integer)
If Label1.InvokeRequired Then
Me.BeginInvoke(New UpdateLabel1Delegate(AddressOf UpdateLabel1Text), Number1)
Else
Label1.Text = Number1
Me.Refresh()
End If
End Sub
Private Sub UpdateLabel2Text(ByVal Number2 As Integer)
If Label2.InvokeRequired Then
Me.BeginInvoke(New UpdateLabel2Delegate(AddressOf UpdateLabel2Text), Number2)
Else
Label2.Text = Number2
Me.Refresh()
End If
End Sub
Private Sub UpdatePicturebox1()
Dim Aantal As Integer = 1000, x As Integer
If PictureBox1.InvokeRequired Then
Me.BeginInvoke(New UpdatePicturebox1Delegate(AddressOf UpdatePicturebox1))
Else
For x = 1 To Aantal
If PictureBox1.Visible = True Then
PictureBox1.Visible = False
Else
PictureBox1.Visible = True
Me.Refresh()
End If
Next
End If
End Sub
End Class
What I have tried:
If Label1.InvokeRequired Then
Me.BeginInvoke(New UpdateLabel1Delegate(AddressOf UpdateLabel1Text), message)
Else
DoSomething
End If