我有一个程序,它使用一个名为Valve的标签和一个名为Variable的文本框
要点是,如果是Variable = 0,则标签颜色是灰色,如果是Variable = 1,则标签使用在灰色和红色之间闪烁的线。
如果在两个值之间快速切换(输入0,然后删除它,然后输入1,依此类推),那么线程速度就会增加(就像是多线程一样),这几乎是完美的。
奇怪的是,如果在值0和1之间缓慢地交换(每隔2秒+),那么它不会增加闪烁速度(这是程序需要做的)
这是来自以下问题的扩展代码:vb.net multi threading
注意:这只是我在VisiWin.NET上的项目的VB.NET转换。在本例中,TextBox Variable将是从可编程控制器读取的实际变量,标签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发布于 2013-02-08 18:33:27
问题如下:
_flash is False:线程启动,颜色改变,休眠2秒_flash is True:_flash将在输入0后快速设置为enter 1,_flash is False:新线程启动。现在,如果在第一个线程休眠时执行步骤2和3,则有两个正在运行的线程。在第一个线程完成睡眠后,它将看到_flash是True,并将继续运行。
发布于 2013-02-08 18:37:16
这里发生的情况是,您的第一个闪存线程仍在运行,它只是处于两秒睡眠阶段。你的值更改为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,尽管我认为这并不真正适用于您,但如果它在线程休眠期间没有中止线程,那么它应该在下一次循环迭代之前中止。
https://stackoverflow.com/questions/14770399
复制相似问题