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

This is the updated code..I am actually trying to find the execution time of each thread and store them in a list. From the list i am trying to find the longest and shortest time...But it is displaying zero all the time...Where am i wrong in the code...Please help.

VB
Public Class Form1
    Private _worker As Thread
    Private Delegate Sub UpdateDelegate(ByVal s As String, ByVal s1 As String)
    Dim elapsedTimeList As New List(Of TimeSpan)
    Private Sub UpdateStatus(ByVal s As String, ByVal s1 As String)
        txtLongest.Text = s
        txtShortest.Text = s1
    End Sub
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      
    End Sub 

    Private Sub btnGetMerchant_Click(sender As System.Object, e As System.EventArgs) Handles btnGetMerchant.Click
        Dim i As Int16
        For i = 1 To txtThreadsNo.Text
            If _worker Is Nothing Then

                _worker = New Thread(AddressOf mythreadmethod1)
                _worker.Start()

            End If
           
        Next
End Sub
   Private Sub mythreadmethod1()
       Dim threadNo As Integer = txtThreadsNo.Text
       Dim min As TimeSpan
       Dim max As TimeSpan
       Dim sw As New Stopwatch()
       sw.Start()

       For i As Integer = 0 To 1000

       Next

       sw.Stop()
       elapsedTimeList.Add(sw.Elapsed)


       If (elapsedTimeList.Count = threadNo) Then

           min = elapsedTimeList.Min
           max = elapsedTimeList.Max

       End If
       Dim longesttime As String = String.Format(min.ToString())
       Dim shortesttime As String = String.Format(max.ToString())
       Dim del As UpdateDelegate = AddressOf UpdateStatus
       Me.Invoke(del, longesttime, shortesttime)

       ' half a second snooze
       Thread.Sleep(500)

       ' thread's dead baby (will terminate when it exits this sub)
       _worker = Nothing

   End Sub
End Class
Posted
Updated 9-May-13 9:38am
v8
Comments
Sergey Alexandrovich Kryukov 9-May-13 12:23pm    
Never use ArrayList, it is obsolete since v.2.0, when the generics were introduced, use System.Collections.Generic.List<>.
What's the problem? Writing a loop, or a comparison operator, or what? What did you try?
—SA
vidkaat 9-May-13 14:06pm    
Sir,

I have pasted the code till what i have done....It might look horrible as i am doing this multi threading for the first time...So please bear with me..and help me correct the code.
Sergey Alexandrovich Kryukov 9-May-13 15:39pm    
For now, I just formatted your code with "pre" tags for you. Please click "Improve question" to see how it should be done. Format you code samples this way next time...
—SA
Sergey Alexandrovich Kryukov 9-May-13 15:43pm    
Use TotalMilliseconds as in Solution 2. Why do you sleep in the thread?
—SA
Pheonyx 9-May-13 12:23pm    
Why are you storing it in an array of strings? and not an array of timespans?

You should create a method, say, public int TotalMilliseconds(string s), accepting one string and returning total milliseconds. Once you have it you may easily compute max and min values with a simple single iteration on the array items.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-May-13 15:41pm    
True, a 5.
(Did you assume that System.Diagnostics.Stopwatch is used?)
—SA
float maxperiod=convert.tofloat32( list.max());
float minperidd=convert.tofloat32( list.min());

////try that
 
Share this answer
 
I think you are better off doing something like this:

VB
Dim elapsedTimeList As New List(Of TimeSpan)
       elapsedTimeList.Add(sw.Elapsed)


Then you can use Linq to get the largest and smallest

VB
dim min as timespan = elapsedTimeList.Min();
dim max as timespan = elapsedTimeList.Max();


Then to format the timespan as a string do what you did before:
Example showing minimum:
String.Format("Minutes :{1}{0}Seconds :{2}{0}Mili seconds :{3}", vbLf, min.Minutes, min.Seconds, min.TotalMilliseconds)
 
Share this answer
 
Comments
vidkaat 9-May-13 14:08pm    
Thanks for the answer...I have updated the question please help.
Pheonyx 9-May-13 14:10pm    
have a read of this article. you are cross threading while you try to update the ui from a different thread. This tells you how to do it using delegates.

http://timhastings.co.uk/2011/09/vb-net-using-delegates-for-form-updates-from-worker-threads/
vidkaat 9-May-13 14:39pm    
Thanks a lot...I read it and updated my code accordingly..I have updated my code above....But i could not retrieve the longest and shortest execution time so please help
Pheonyx 9-May-13 14:41pm    
What do you mean by you could not retrieve the longest and shortest times?
vidkaat 9-May-13 14:53pm    
I solved it.Thanks a lot.

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