Solved

How to update form from thread

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

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 …
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 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