多线程故障



我有一个程序,它使用一个名为Valve的标签和一个称为Variable 的文本框

要点是如果CCD_ 3则标签颜色是灰色,如果Variable = 1,则标签使用在灰色和红色之间闪烁的线程。

除了在两个值之间变化非常快(输入0,然后删除它,然后输入1,依此类推)之外,这几乎完全有效。然后线程速度会增加(就像是多线程一样)。

奇怪的是,如果在值0&1缓慢(每2秒+),然后它不会增加闪烁速度(这是程序需要做的)

这是以下问题的扩展代码:vb.net多线程

注意:这只是我在VisiWin.NET上的项目中的VB.NET转换。在本例中,TextBox Variable将是从PLC读取的实际变量,标签Valve将是表示流程图模拟中的过程螺线管的三角形。每个电磁阀将由不同的变量控制。

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading
Imports System.Diagnostics
Public Class Form1
Private _flash As Boolean = False
Private Sub Variable1_TextChanged(sender As System.Object, e As System.EventArgs) Handles Variable.TextChanged

    If Variable.Text = "1" And Not _flash Then
        _flash = True
        Dim FlashThread As New Thread(New ThreadStart(AddressOf FlashLabel))
        FlashThread.Start()
    End If
    If Variable.Text = "0" Then
        _flash = False
        Valve.ForeColor = Color.Gray
    End If
End Sub

Private Sub FlashLabel()
    Dim _Color As Color = Color.Gray
    While _flash
        If Valve.ForeColor = _Color Then
            Valve.ForeColor = Color.Red
        Else
            Valve.ForeColor = Color.Gray
        End If
        System.Threading.Thread.Sleep(2000)
    End While
End Sub
End Class

这里发生的事情是,您的第一个闪烁线程仍在运行,它只是处于两秒的睡眠阶段。你的值变为0,它不会脱离循环,因为它处于睡眠状态,然后变量再次变回1,线程醒来并继续,这时你已经产生了另一个线程在做同样的事情,所以看起来线程运行得更快。

我建议将其更改为计时器,因为您可以在变量为0时停止计时器,然后在变量为1:时重新启动计时器

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading
Imports System.Diagnostics
Public Class Form1
Private _timer As New System.Windows.Forms.Timer()
Private Sub Variable1_TextChanged(sender As System.Object, e As System.EventArgs) Handles Variable.TextChanged

    If Variable.Text = "1" And Not _flash Then
        _flash = True
        _timer.Interval = 2000
        _timer.Enabled = True
        _timer.Start()
    End If
    If Variable.Text = "0" Then
        _flash = False
        _timer.Stop()
        _timer.Enabled = False
        Valve.ForeColor = Color.Gray
    End If
End Sub

Private Sub FlashLabel() Handles _timer.Tick
    Dim _Color As Color = Color.Gray
    If Valve.ForeColor = _Color Then
        Valve.ForeColor = Color.Red
    Else
        Valve.ForeColor = Color.Gray
    End If
End Sub
End Class

计时器文档:http://msdn.microsoft.com/en-gb/library/system.windows.forms.timer.aspx

或者,您可以将线程存储在一个字段中,并在变量设置为0:时终止它

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading
Imports System.Diagnostics
Public Class Form1
Private _flash As Boolean = False
Private _flashThread as Thread
Private Sub Variable1_TextChanged(sender As System.Object, e As System.EventArgs) Handles Variable.TextChanged

    If Variable.Text = "1" And Not _flash Then
        _flash = True
        _flashThread As New Thread(New ThreadStart(AddressOf FlashLabel))
        _flashThread.Start()
    End If
    If Variable.Text = "0" Then
        _flash = False
        _flashThread.Abort()
        Valve.ForeColor = Color.Gray
    End If
End Sub

Private Sub FlashLabel()
    Dim _Color As Color = Color.Gray
    While _flash
        If Valve.ForeColor = _Color Then
            Valve.ForeColor = Color.Red
        Else
            Valve.ForeColor = Color.Gray
        End If
        System.Threading.Thread.Sleep(2000)
    End While
End Sub
End Class

请参阅http://msdn.microsoft.com/en-GB/library/ty8d3wta.aspx关于中止线程的注意事项,尽管我认为这些都不适用于您,但如果它在睡眠时间没有中止线程,它应该在循环的下一次迭代之前中止。

问题如下:

  1. 输入1,_flashFalse:线程启动,更改颜色并休眠2秒
  2. 输入1后快速输入0,_flashTrue_flash将设置为False
  3. 输入0后快速输入1,_flash再次为False:启动新线程

现在,如果步骤2和3发生在第一个线程睡眠的时候,那么您有两个正在运行的线程。在第一个线程完成睡眠之后,它将看到_flashTrue,并将继续运行。

最新更新