Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a window form with a text box where the user enters the value for no .of threads and a button . Based on the value say 4 i would start 4 threads on button click which will execute Get method on 4 threads ...From this i need to measure which thread is taking long time and which thread is taking the shortest time to execute the method...How can i achieve this...Please help....
Here is wat i tried:
VB
Dim i As Int16
      For i = 1 To txtThreadsNo.Text
          Dim myThread1 As New Thread(AddressOf mythreadmethod1)
          myThread1.Start()
          
      Next


VB
Private Sub mythreadmethod1()
        Dim sw As New Stopwatch()
        sw.Start()

        For i As Integer = 0 To 1000

        Next

        sw.Stop()
        Dim ExecutionTimeTaken As String = String.Format("Minutes :{0}" & vbLf & "Seconds :{1}" & vbLf & " Mili seconds :{2}", sw.Elapsed.Minutes, sw.Elapsed.Seconds, sw.Elapsed.TotalMilliseconds)

        txtLongest.Text = ExecutionTimeTaken
    End Sub


The txtThreadsNo.Text is 2.
This causes an error "Cross-thread operation not valid: Control 'txtLongest' accessed from a thread other than the thread it was created on."How can i get rid of this.



Thanks.
Posted
Updated 9-May-13 5:33am
v5
Comments
pdoxtader 9-May-13 12:24pm    
You can not access a UI element from a background thread. Only code executing on the UI thread can access UI elements.

You need to learn how to use delegates. That's the way background threads can pass data to the UI thread so that UI elements can be updated.

Have a look here: http://www.codeproject.com/Articles/30458/Delegates-in-VB-NET

1.) Create a small class that has a thread object and two date objects (start date and finish date) in it. Every time you create a new thread, use this class to create it. Set the start date then you start the thread, and the finished date then it completes it's task.

2.) Add all started threads to a global collection. Have each thread check the global collection when it completes to see if they are all done (use SyncLock). If they are, have it call back into your UI thread (using a delegate) to update your UI with the times and performance stats.

That's what I would do anyway. Good Luck,

- Pete
 
Share this answer
 
v2
Here's a very simple and limited example of using a delegate to update your UI control from a background thread:

VB
Public Delegate Sub UiUpdateDelegate(ByVal message As String)
Public updateMyUI As UiUpdateDelegate = AddressOf UpdateUI

Private Sub UpdateUI(ByVal message As String)

    If Me.InvokeRequired then
        Me.Invoke(updateMyUI, message)
    Else
        ' We're on the UI thread now. Update our text box.
        txtLongest.Text = message
    End If

End Sub

Private Sub mythreadmethod1()
    Dim sw As New Stopwatch()
    sw.Start()

    For i As Integer = 0 To 1000

    Next

    sw.Stop()
    Dim ExecutionTimeTaken As String = String.Format("Minutes :{0}" & vbLf & "Seconds :{1}" & vbLf & " Mili seconds :{2}", sw.Elapsed.Minutes, sw.Elapsed.Seconds, sw.Elapsed.TotalMilliseconds)

    UpdateUI(ExecutionTimeTaken)
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900