Solved

How to update form from thread

Posted on 2014-03-18
4
329 Views
Last Modified: 2014-03-19
Hello,

I have a problem with multithreading.

I have a scoreboard, the update the laptime and position, when someone press a numeric key.

So, someone press key 1, a thread is started to stop the time and display it on the form.
   
 Dim thrStopLane1 As New System.Threading.Thread(AddressOf stopBaan1)
   

Private Sub Scorebord_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar.ToString = "1" Then
            'Baan 1 stopt
            If bitLane1 = False Then
                If Timer1.Enabled = True Then
                    thrStopLane1.Start()
                End If
            End If
end sub

Open in new window


the thread code:
    Private Sub stopBaan1()
        bitLane1 = True
        curpos += 1
        Timer1.Stop()
        Dim span As TimeSpan = DateTime.Now.Subtract(startTime)
        lblTimeLane1.Text = span.Minutes.ToString.PadLeft(2, "0").ToString & ":" & _
      span.Seconds.ToString.PadLeft(2, "0").ToString & "." & span.Milliseconds.ToString.PadLeft(2, "0").ToString
        lblPosLane1.Text = curpos.ToString
        CloseEntry(1, curpos, 12, span.Minutes.ToString.PadLeft(2, "0").ToString & ":" & _
        span.Seconds.ToString.PadLeft(2, "0").ToString & "." & span.Milliseconds.ToString.PadLeft(2, "0").ToString)

    End Sub

Open in new window


When i run my code i get the following message:
Cross-thread operation not valid: Control 'lblTimeLane1' accessed from a thread other than the thread it was created on.

How can i get this done? it's driving me nuts....

My code is in vb.net, but C# will do also.

Thanks a lot,

Edward
0
Comment
Question by:nijmedw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39938121
GUI access from threads other than the main thread have to handled through delegation. You can use the Invoke pattern for this. I demonstrated it in this post:  http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_25730231.html#a30129296

You then call this new method from your other thread. This method handles creating the delegate.
0
 

Author Comment

by:nijmedw
ID: 39939201
Hello,

Thanks for your reply. Can you explain it a little bit more with my example code?

I can't see what i should delicate and what the thread would be.

Thanks a lot.

Kind regards,

Edwar
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39939535
As outlined in the post I linked to, you need a new method to handle the invoking:

Private Sub UpdateText(ByVal ctrl As Control, ByVal value As String)
    If ctrl.InvokeRequired Then
        ctrl.Invoke(New Action(Of Control, String)(AddressOf UpdateText), ctrl, value)
    Else
        ctrl.Text = value
    End If
End Sub

Open in new window


Then you simply call the new method instead of accessing the controls directly:

Private Sub stopBaan1()
    bitLane1 = True
    curpos += 1
    Timer1.Stop()
    Dim span As TimeSpan = DateTime.Now.Subtract(startTime)
    UpdateText(lblTimeLane1, span.Minutes.ToString.PadLeft(2, "0").ToString & ":" & _
        span.Seconds.ToString.PadLeft(2, "0").ToString & "." & span.Milliseconds.ToString.PadLeft(2, "0").ToString)
    UpdateText(lblPosLane1, curpos.ToString)
    CloseEntry(1, curpos, 12, span.Minutes.ToString.PadLeft(2, "0").ToString & ":" & _
    span.Seconds.ToString.PadLeft(2, "0").ToString & "." & span.Milliseconds.ToString.PadLeft(2, "0").ToString)

End Sub

Open in new window

0
 

Author Closing Comment

by:nijmedw
ID: 39940030
Works perfect, thanks a lot, this is just what i needed !!!!

Thanks,

Edward
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question