|
-
Oct 28th, 2006, 06:04 PM
#1
Thread Starter
Lively Member
[RESOLVED] [2005] How to access to an object from a separate Thread
Hi all guys, i want to increase a label in Form1 using a thread, but it say "Cross-thread operation not valid: Control 'Label1' accessed from a thread other than the thread it was created on.", how can i access to the label from a thread?
VB Code:
Imports System.Threading
Public Class Form1
Dim par As Thread
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
par.Start()
End Sub
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
par = New Thread(AddressOf runs)
End Sub
Sub runs()
Dim i As Long = 0
While True
For i = 0 To 300
i += 1
label1.Text = i
Next
End While
End Sub
End Class
-
Oct 28th, 2006, 06:59 PM
#2
Re: [2005] How to access to an object from a separate Thread
You have to look into using delegates and calling the delegates from the thread. Search this forum for "delegate invoke" or something to that matter and you should pull several threading questions and examples about the subject.
Its not a particularly "simple" matter and takes some time to get used to if you haven't done it, I still have problems with it.
Some links that might help:
http://msdn2.microsoft.com/en-us/lib...69(VS.80).aspx
http://msdn.microsoft.com/msdnmag/is...g/default.aspx
http://www.aspfree.com/c/a/VB.NET/De...h-VB-NET-2005/
Last edited by gigemboy; Oct 28th, 2006 at 07:03 PM.
-
Oct 28th, 2006, 07:00 PM
#3
Re: [2005] How to access to an object from a separate Thread
You should invoke your method on the correct thread, you can do this delegating the function (sync with Invoke or Async with BeginInvoke).
The restriction is because the UI thread is the main gdi thread that draws your interface, Microsoft does not want you to change things that are under that thread responsibility as you can crash, lock , a priority inversion or generate a race condition on the UI thread. .NET 2.0 enforces that rule that was overlooked on .NET 1.1.
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 28th, 2006, 10:23 PM
#4
Re: [2005] How to access to an object from a separate Thread
E.g. setting the Text property of TextBox from a worker thread:
VB Code:
Private Delegate Sub SetTextBoxTextDelegate(ByVal text As String)
Private Sub SetTextBoxText(ByVal text As String)
If Me.TextBox1.InvokeRequired Then
'This is a worker thread so create a delegate to cross the thread boundary.
Me.TextBox1.Invoke(New SetTextBoxTextDelegate(AddressOf SetTextBoxText), text)
Else
'This is the UI thread so access the control's members directly.
Me.TextBox1.Text = text
End If
End Sub
Just call the SetTextBoxText method from wherever you are and it will look after the threading issues itself.
-
Oct 29th, 2006, 06:18 AM
#5
Thread Starter
Lively Member
Re: [2005] How to access to an object from a separate Thread
Great!, Thx a lot
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
|