Solved

How to update form from thread

Posted on 2014-03-18
4
331 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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