具有定时器错误的多线程 [英] MultiThreading with Timer-Error

查看:77
本文介绍了具有定时器错误的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这是更新的代码。我实际上是在尝试查找每个线程的执行时间并将它们存储在列表中。从列表中我试图找到最长和最短的时间......但它一直显示为零...代码中我错了...请帮助。



 公共  Form1 
< span class =code-keyword>私有 _worker 线程
私有 代理 Sub UpdateDelegate( ByVal s 作为 字符串 ByVal s1 As String
Dim elapsedTimeList 作为 列表( TimeSpan)
< span class =code-keyword> Priva te Sub UpdateStatus( ByVal s As 字符串 ByVal s1 作为 字符串
txtLongest.Text = s
txtShortest.Text = s1
结束 Sub
私人 Sub Form1_Load( ByVal sender 作为 对象 ByVal e As System.EventArgs) Handles .Load

结束 Sub

私人 Sub btnGetMerchant_Click(发件人作为系统。 Object ,e As System.EventArgs) Handles btnGetMerchant.Click
Dim i 作为 Int16
对于 i = 1 txtThreadsNo。文字
如果 _worker 然后

_worker = 线程( AddressOf mythreadmethod1)
_worker.Start()

结束 如果

下一步
结束 Sub
私有 Sub mythreadmethod1()
Dim threadNo As 整数 = txtThreadsNo.Text
Dim min As TimeSpan
Dim max As TimeSpan
Dim sw 作为 秒表()
sw.Start()

对于 i 作为 整数 = < span class =code-digit> 0
1000

下一步

sw。停止()
elapsedTimeList.Add(sw.Elapsed)


如果(elapsedTimeList.Count = threadNo)那么

min = elapsedTimeList.Min
max = elapsedTimeList.Max

结束 如果
Dim longesttime 作为 字符串 = String .Format(min.ToString())
Dim shortesttime As 字符串 = 字符串 .Format(max.ToString())
Dim del As UpdateDelegate = AddressOf UpdateStatus
Me .Invoke(del,longesttime,shortesttime)

' 半秒钟的贪睡
Thread.Sleep( 500

' 线程的死宝贝(将在退出此子时终止)
_worker = 没什么

结束 Sub
结束

解决方案

你应该创建一个方法,比如说, public int TotalMilliseconds(string s),接受一个字符串并返回总毫秒数。一旦你拥有它,你可以通过对数组项进行简单的单次迭代轻松计算 max min 值。


float maxperiod = convert.tofloat32(list.max());

float minperidd = convert.tofloat32(list.min());



////试试


我觉得你最好做这样的事情:



  Dim  elapsedTimeList  As  列表(  TimeSpan)
elapsedTimeList.Add(sw.Elapsed)





那么你可以使用Linq获得最大和最小的



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





然后将时间跨度格式化为字符串执行以前的操作:

显示最小值的示例:

 String.Format(分钟:{1} {0}秒:{2} {0} Mili秒:{3},vbLf,min.Minutes,min.Seconds,min.TotalMilliseconds)


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.

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

解决方案

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.


float maxperiod=convert.tofloat32( list.max());
float minperidd=convert.tofloat32( list.min());

////try that


I think you are better off doing something like this:

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



Then you can use Linq to get the largest and smallest

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)


这篇关于具有定时器错误的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆