Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to update form from thread

Posted on 2014-03-18
4
Medium Priority
?
334 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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