Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cross-thread operation not valid 2

Status
Not open for further replies.

mancroft

Programmer
Oct 26, 2002
267
GB
Cross-thread operation not valid

Hello

i am trying to put a counter variable created in a thread into a label and am getting "Cross-thread operation not valid".

Is there an easy way around this?

Thank you.
 
if you look at th help you get when you get this error (you are working in 2005 aren't you) then it will give you at least 5 options. One of them being a delegate with begininvoke and invokerequired. try that.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Actually a very interesting site. So have a star.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Here is some code that is a lot simpler to understand... and it works:

Code:
Imports System.Threading

Public Class Form1

    Private Delegate Sub DelAddItem(ByVal Message As String)

    Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim T1 As Thread

        T1 = New Thread(AddressOf Thread1Work)
        T1.IsBackground = True
        T1.Start()

    End Sub

    Protected Sub Thread1Work()

        Dim del As DelAddItem
        Dim o(0) As Object
        del = New DelAddItem(AddressOf DelegateWork)
        Dim I As Integer
        For I = 0 To 100
            o(0) = "Hello " & I.ToString
            Thread.Sleep(700)
            Label1.Invoke(del, o)
        Next

    End Sub

    Private Sub DelegateWork(ByVal Message As String)

        Label1.Text = Message

    End Sub

End Class
 
And you can make it even more simple by doing this.

Code:
Imports System.Threading

[Blue]Public[/Blue] [Blue]Class[/Blue] Form1

    [Blue]Private[/Blue] Delegate [Blue]Sub[/Blue] DelAddItem([Blue]ByVal[/Blue] Message [Blue]As[/Blue] [Blue]String[/Blue])

    [Blue]Private[/Blue] [Blue]Sub[/Blue] btnButton1_Click([Blue]ByVal[/Blue] sender [Blue]As[/Blue] System.Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.EventArgs) [Blue]Handles[/Blue] Button1.Click

        [Blue]Dim[/Blue] T1 [Blue]As[/Blue] Thread
for inttemp as integer = 0 to 100
        T1 = [Blue]New[/Blue] Thread(AddressOf Thread1Work([Red]"Hello"[/Red] & inttemp)
        T1.IsBackground = [Blue]True[/Blue]
        T1.Start()
next

    [Blue]End[/Blue] [Blue]Sub[/Blue]

    [Blue]Protected[/Blue] [Blue]Sub[/Blue] Thread1Work(byval message as string)
        if me.label1.invokerequired then
            [Blue]Dim[/Blue] del [Blue]As[/Blue] DelAddItem = [Blue]New[/Blue] DelAddItem(AddressOf threadWork)
            me.invoke(del,new object() {message)
       else
            label1.text = message
       end if
    [Blue]End[/Blue] [Blue]Sub[/Blue]
[Blue]End[/Blue] [Blue]Class[/Blue]

untested but should work

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top