Click here to Skip to main content
15,885,952 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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:

VB
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 'CountUp

    Private Sub CountDown()
        Number2 = 1000
        Do Until Number2 = 0
            Number2 = Number2 - 1
            UpdateLabel2Text(Number2)
        Loop
    End Sub 'CountDown

    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
                    'Me.Refresh()
                Else
                    PictureBox1.Visible = True
                    Me.Refresh()
                End If
            Next 'x
        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
Posted
Updated 29-Dec-18 1:02am
v2
Comments
[no name] 29-Dec-18 7:16am    
I'm surprised, that you even can see any real Action of your threads. For a test put Thread.Sleep(50) in all of the "thread Loops".

1 solution

Invoke and CheckForIllegalCrossThreadCalls are only relevant when you try to access a Control from a thread other than your original UI thread: if you access them from within a thread that you create, then you should use Invoke - which moves the code back to the original UI thread. That has nothing to do with "threads starting immediately", and Invoke does not replace the actual creating of a thread!

Look at your code (we obviously can't see it): your button press should create and start a thread. In the thread method, you use Invoke to access your labels / textboxes / picturebox.
 
Share this answer
 
Comments
Member 11112814 28-Dec-18 15:46pm    
Yoy wrote:"your button press should create and start a thread". That's the problem!
The button press SHOULD create and start a thread, and it does indeed, but NOT before the previous thread has ended!
OriginalGriff 29-Dec-18 3:25am    
So show us the code that you are using to create and start the thread.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900