夸线程调用控件 夸线程传参

 一、主线程结束时  其他线程还没结束或结束有延时时使用   用Thread.IsbackThread = ture还是会导致线程关闭比较慢而报错

1、声明 线程

2、触发线程  (在线程里面做循环  这样就可以测不触发 、触发后子线程一直活着 、和触发过后停止 三种情况下关闭窗口会不会报错)

3、在加载窗口时就让关闭窗口中事件和 关闭子线程方法挂接

4 EndOtherThread 关闭子线程方法

额外知识  夸线程调用控件要使用委托  这里使用的是系统自带的Action委托  没有参数和返回值的委托

Dim th As Threading.Thread
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If Button4.Text = "夸线程调用控件" Then
            th = New Threading.Thread(Sub()
                                          Dim i As Integer
                                          While i < 500
                                              Invoke(New Action(AddressOf Label1text))
                                              Threading.Thread.Sleep(10)
                                              Invoke(New Action(AddressOf Label1text1))
                                              Threading.Thread.Sleep(10)
                                              i += 1
                                          End While
                                          th.Abort()
                                      End Sub)
            th.IsBackground = True
            th.Start()
        End If
    End Sub

    Private Sub Label1text()
        Label1.Text = "测试成功"
    End Sub

    Private Sub Label1text1()
        Label1.Text = "测试成功2!!!!!!"
    End Sub

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在加载窗口时订阅窗口关闭事件  让窗口关闭时通知EndOthreThread方法
        AddHandler Me.FormClosing, AddressOf EndOthreThread
        'Dim testDelegetSub As testDelegetSub = AddressOf testDelegateSub3
    End Sub

Private Sub EndOthreThread(ByVal sender As Object, ByVal e As FormClosingEventArgs)
        If Not th Is Nothing Then
            'If th.IsAlive Then'如果线程没有被 新建过就会报错
            th.Abort()
        End If
    End Sub

 简化版(在控件报错时闪烁提示)

''' <summary>
    ''' 控件闪烁提示
    ''' </summary>
    ''' <param name="Controul"></param>控件名
    ''' <param name="color"></param>要闪烁的颜色
    ''' <param name="BlinkCount"></param>可选要闪烁的次数
    Public Sub Blink(Controul As Control, color As Color, Optional BlinkCount As Int16 = 3)
        Dim oldColor As Color
        oldColor = Controul.BackColor
        Dim th As New Thread(Sub()
                                 For i As Integer = 0 To BlinkCount - 1
                                     Invoke(New Action(Sub() Controul.BackColor = color))
                                     Thread.Sleep(366)
                                     Invoke(New Action(Sub() Controul.BackColor = oldColor))
                                     Thread.Sleep(366)
                                 Next
                                 Thread.CurrentThread.Abort()
                             End Sub)
        th.IsBackground = True
        th.Start()
    End Sub

 

void ButtonOnClick(object sender,EventArgs e)
{
    this.Invoke(new Action(()=>
    {
        button.Text="关闭";
    }));
}
 最新老版本VS不支持:
Invoke(() =>
{
 button.Text="关闭";
});

 

二、新线程的传参问题    

1、线程标准的只能传一个 object 类型的参数   切不支持返回值

 

 Private Sub Label1textThread(ByVal labelltext As Object)
        MessageBox.Show(labelltext.ToString())
        Threading.Thread.Sleep(30000)
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim th As Threading.Thread = New Threading.Thread(AddressOf Label1textThread)
        th.IsBackground = True
        th.Start("线程传参成功")
    End Sub

2、可传多个任意类型的参数    需要结合lambda 表达式一起用

 Private Sub Add(ByVal a As Integer, ByVal b As Integer)
        MessageBox.Show((a + b).ToString())
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        '通过变量传值
        Dim a As Integer = 10
        Dim b As Integer = 20
        Dim th As Threading.Thread = New Threading.Thread(Sub()
                                                              Add(a, b)
                                                          End Sub)
        th.IsBackground = True
        th.Start()
    End Sub

3、可传多个任意类型的参数加返回值    需要结合lambda 表达式一起用

 Private Function Add(ByVal a As Integer, ByVal b As Integer)
        Return (a + b)
    End Function

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        '通过变量传值
        Dim a As Integer = 10
        Dim b As Integer = 20
        Dim c As Integer = 2
        Dim th As Threading.Thread = New Threading.Thread(Sub()
                                                              c = Add(a, b)
                                                              MessageBox.Show(c.ToString())
                                                          End Sub)
        th.IsBackground = True
        th.Start()

    End Sub

 3.1  额外知识  MessageBox  写的地方不同   为啥要延时接收注意思考

 Dim th As Threading.Thread = New Threading.Thread(Sub()
                                                              c = Add(a, b)
                                                              Threading.Thread.CurrentThread.Abort()
                                                          End Sub)
        th.IsBackground = True
        th.Start()
        Threading.Thread.Sleep(10)
        MessageBox.Show(c.ToString())

 

posted @ 2021-06-25 10:08  浅物  阅读(63)  评论(0)    收藏  举报