我如何限制否。基于用户输入的线程数 [英] How do i restrict the no. of threads based on user input

查看:63
本文介绍了我如何限制否。基于用户输入的线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个带有文本框的窗口表单,用户输入no.of线程和按钮的值。基于值4,我将在按钮点击时启动4个线程,这将在4个线程上执行Get方法...从此我需要测量哪个线程花费很长时间以及哪个线程花费最短的时间来执行该方法。 ..我怎么能实现这个...请帮助....

这是我试过的wat:

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:

Dim i As Int16
      For i = 1 To txtThreadsNo.Text
          Dim myThread1 As New Thread(AddressOf mythreadmethod1)
          myThread1.Start()
          
      Next







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





txtThreadsNo.Text为2.

这会导致错误不支持跨线程操作有效:控制''txtLongest''从其创建的线程以外的线程访问。我怎样才能摆脱这个。







谢谢。



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.

推荐答案

1。)创建一个包含线程对象和两个日期对象的小类(开始日期)在其中完成日期。每次创建新线程时,请使用此类创建它。设置开始日期然后启动线程,完成日期然后完成它的任务。



2.)将所有已启动的线程添加到全局集合中。让每个线程在完成时检查全局集合以查看它们是否全部完成(使用SyncLock)。如果是的话,让它回调你的UI线程(使用委托)用时间和性能统计数据来更新你的UI。



这就是我的意思无论如何都会这样做。好运,



- Pete
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


这是使用委托更新UI控件的一个非常简单且有限的示例来自后台主题:



Here''s a very simple and limited example of using a delegate to update your UI control from a background thread:

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


这篇关于我如何限制否。基于用户输入的线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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