【问题标题】:Need help on Threading with WIndows Form Control in VB.Net需要有关在 VB.Net 中使用 WIndows 窗体控件进行线程化的帮助
【发布时间】:2014-12-19 06:41:44
【问题描述】:

在我的 Windows 窗体应用程序中,我必须在线程中执行一个方法。方法的执行时间取决于 Tick 事件。因此,每 5 秒发生一次 Tick 事件,并在其中检查上次执行与现在之间经过的时间。如果经过的时间 > 10 秒,则只有它执行创建单独线程的方法。但是,如果原始线程尚未完成其执行,则应用程序不应执行该方法。换句话说,应用程序在一个线程中执行该方法,并在线程完成执行后 10 秒后执行,不一定在两个滴答声上。

现在,问题是: 所以,我需要在代码中放置一个逻辑,在线程完成执行之前停止滴答声。

我试图通过在线程启动时禁用计时器控件并在线程完成执行时再次启用它来解决它,但似乎它不起作用。

Public Class Form1

   Private lastRunDateTime As DateTime = #1/1/1900#


   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Timer1.Interval = 5000
        Timer1.Enabled = True
   End Sub

   'This method takes more than 5 seconds
    Private Sub test()
        For value As Integer = 0 To 10000
            Console.WriteLine(value)
        Next
       'Timer1.Enabled = True

    End Sub



    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
        If DateDiff(DateInterval.Second, lastRunDateTime, Now) > 10 Then
            'Timer1.Enabled = False
            lastRunDateTime = Now
            Dim th = New Threading.Thread(Sub() test())
            th.Start()

        End If


    End Sub


End Class

【问题讨论】:

  • 您使用的是哪个 .net 框架版本?
  • 我的问题只针对你。 您使用的是哪个 .net 框架版本?
  • 它是 4.0 。另请解释版本的重要性?
  • 是的,这很重要,因为您将获得使用最新框架的更好答案。示例:TPl 代替线程。这对你好吗?或者你只需​​要使用线程?
  • 哦,好的。我需要使用线程。这是我的示例应用程序。我将使用这个概念(一旦解决)来完成实际任务。

标签: .net vb.net multithreading .net-4.0


【解决方案1】:

由于该问题与多线程有关,因此需要对 Windows 控件进行线程线程安全调用。定时器控件 (Timer1) 最初是在 Test() 中使用的,它不是线程安全的。因此,通过进行线程安全调用(即使用 BeginInvoke)解决了该问题,该调用对启用或禁用计时器的另一个方法 (timeToggle(boolean)) 进行异步调用。

Public Class Form1

   Private lastRunDateTime As DateTime = #1/1/1900#


   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Timer1.Interval = 5000
        Timer1.Enabled = True
   End Sub

    '*********Delegate Added********************************
    Private Delegate Sub _toggleDelegate(start As Boolean)
    '*********************************************************

    'This method takes more than 5 seconds
    Private Sub test()
        For value As Integer = 0 To 10000
            Console.WriteLine(value)
        Next
        'Timer1.Enabled = True

        '*********New Addition*************
        Me.BeginInvoke(New _toggleDelegate(AddressOf toggleTimer), True)
        '**********************

    End Sub

    '********New Method Added**************
    Private Sub toggleTimer(start As Boolean)
        Timer1.Enabled = start
    End Sub
    '*************************************



    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
        If DateDiff(DateInterval.Second, lastRunDateTime, Now) > 10 Then
            'Me.BeginInvoke(New _toggleDelegate(AddressOf toggleTimer), False)
            '**********uncommented now or can the statement above*******
            Timer1.Enabled = False
            '**********************
            lastRunDateTime = Now
            Dim th = New Threading.Thread(Sub() test())
            th.Start()

        End If


    End Sub


End Class   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    相关资源
    最近更新 更多