如何使每个表单成为自己的线程并能够更新?

时间:2013-03-03 16:31:36

标签: vb.net multithreading winforms

我的应用程序(聊天应用程序)中有三个(4个计算主要的)线程,我想将每个表单放入它自己的线程中(一个线程不需要表单,因此它无关紧要)。我正在使用的两种形式是包含本地数字字体的时钟。问题是threadb将启动并运行,但threadc将保留为文本LABEL1而不会完全更改为:MM / dd / yyyy hh:mm:ss tt。我想知道如何让这些线程一起工作。

这是线程代码(在porgram的开头创建,这是来自sub official_start):

    threadb = New Threading.Thread(AddressOf form3threadsub)
    threadc = New Threading.Thread(AddressOf form4threadsub)
    threadb.IsBackground = False
    threadc.IsBackground = False
    threadb.Start()
    threadc.Start()

以下是线程sub的代码:

    Private Sub form3threadsub()
        Dim first As Boolean = False
        Do
            System.Threading.Thread.Sleep(100)
            If first = False Then
                form3.ShowDialog()

                first = True
            End If
            form3.Label1.Text = DateTime.UtcNow.ToString("MM/dd/yyyy hh:mm:ss tt")
            form3.Update()
        Loop
    End Sub

    Private Sub form4threadsub()
        Dim first As Boolean = False
        Do
            System.Threading.Thread.Sleep(100)
            If first = False Then
                Form4.ShowDialog()

                first = True
            End If
            Form4.Label1.Text = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt")
            Form4.Update()
        Loop
    End Sub

这是按钮2点击:

    Dim clicked As Boolean = True 

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        If clicked Then
            threadb.Suspend()'obsolete
            threadc.Suspend()'obsolete
            Button2.Text = "Resume Clocks"
            clicked = False
        Else
            threadb.Resume()'obsolete
            threadc.Resume()'obsolete
            Button2.Text = "Pause Clocks"
            clicked = True
        End If
    End Sub

1 个答案:

答案 0 :(得分:0)

我想我找到了自己的解决方案。我拿出了额外的表格,并用主表格上的标签替换了它们。每个标签都是它自己的线程并相应地更新。