Results 1 to 5 of 5

Thread: [RESOLVED] [2005] How to access to an object from a separate Thread

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    84

    Resolved [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:
    1. Imports System.Threading
    2.  
    3. Public Class Form1
    4.     Dim par As Thread
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         par.Start()
    7.     End Sub
    8.  
    9.     Public Sub New()
    10.         ' This call is required by the Windows Form Designer.
    11.         InitializeComponent()
    12.         ' Add any initialization after the InitializeComponent() call.
    13.         par = New Thread(AddressOf runs)
    14.     End Sub
    15.    
    16.  
    17. Sub runs()
    18.         Dim i As Long = 0
    19.         While True
    20.             For i = 0 To 300
    21.                 i += 1
    22.                 label1.Text = i
    23.             Next
    24.         End While
    25.     End Sub
    26. End Class

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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.

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Delegate Sub SetTextBoxTextDelegate(ByVal text As String)
    2.  
    3. Private Sub SetTextBoxText(ByVal text As String)
    4.     If Me.TextBox1.InvokeRequired Then
    5.         'This is a worker thread so create a delegate to cross the thread boundary.
    6.         Me.TextBox1.Invoke(New SetTextBoxTextDelegate(AddressOf SetTextBoxText), text)
    7.     Else
    8.         'This is the UI thread so access the control's members directly.
    9.         Me.TextBox1.Text = text
    10.     End If
    11. End Sub
    Just call the SetTextBoxText method from wherever you are and it will look after the threading issues itself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    84

    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
  •  



Click Here to Expand Forum to Full Width