Solved

How to update form from thread

Posted on 2014-03-18
4
324 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
  • 2
  • 2
4 Comments
 
LVL 74

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 74

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now