VB.NET 多线程调用(笔记)

调用画面类内部

Public Class  AAA

    Private Shared mstrTitle As String

    Private Shared mobjDocument As GrapeCity.ActiveReports.Document.PageDocument                     
    Private Shared mfrmForm As AAA= Nothing

    Private Sub AAA_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    End Sub


    Public Shared Sub sub_CallForm(ByVal aaa As GrapeCity.ActiveReports.Document.PageDocument, _
                                   ByVal pstrMsg As String)
        Dim objT1 As Threading.Thread

        mobjDocument = aaa
        mstrTitle = pstrMsg

        objT1 = New Threading.Thread(AddressOf sub__CallForm)
        objT1.Start()

    End Sub

    Private Shared Sub sub__CallForm()
        If mfrmFormIs Nothing Then
                mfrmForm= New frmPageView2()
                mfrmForm.Text = mstrTitle
                mfrmForm.SetPageDocument(mobjDocument)
                mfrmForm.Viewer1.LoadDocument(mobjDocument)
                mfrmForm.ShowDialog()
        End If
    End Sub

    Public Sub SetPageDocument(objDocument As GrapeCity.ActiveReports.Document.PageDocument)
        Try
            mobjDocument = objDocument

        Catch ex As Exception
            LogUtil.Fatal(Me, ex)
            Exit Sub
        End Try

    End Sub

End Class


调用

  Public Sub sub_PrintView(ByVal aa As GrapeCity.ActiveReports.Document.PageDocument, _
                                 ByVal bb As String)
            Try
                frmPageView2.sub_CallForm(aa, bb)
            Catch ex As Exception
                LogUtil.Fatal(Me, ex)
                Throw (ex)
            End Try
        End Sub

在码农的生活中,很多级码农都有这样的经历,会被一个小小的技术问题拦住,然后进度跟不上了,被老板XXXX一大通了。心情不爽了。 好吧,这个曾经是我遇到拦路虎之一。但事实上不是什么大技术。技术就是一层纸,破了就破了。 这是一个关于如何跨窗体操作控件或过程的一个例子。比如,你想用窗体A的按键来执行窗体B的文本框变色。 Imports System Imports System.Threading Imports System.Text Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load CheckForIllegalCrossThreadCalls = False '不写这行,会出错,不允许线程的数据写到TextBox1.Text 中去。 Form2.Show() End Sub Private Sub form1_FormClosing(sender As Object, e As EventArgs) Handles Me.FormClosing ' If runThread.IsAlive = True Then runThread.Abort() End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click '用的是委托方式 Me.Invoke(New MethodInvoker(AddressOf THREAD2)) End Sub Private Sub THREAD2() Static j As Integer j = j + 1 TextBox1.Text = " 这是 [线程] 操作" & vbCrLf & _ " Button2被点了: " & j & " 次" & vbCrLf & "要求是from2.textbox.text= textbox1.text 。[问题]但为什么不能成功显示呢?" End Sub Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged Form2.TextBox1.Text = TextBox1.Text End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click Static j As Integer j = j + 1 TextBox1.Text = " 这是 [非线程] 操作" & vbCrLf & _ "Button3 点击了: " & j & " 次" & vbCrLf & _ "要求是from2.textbox.text= textbox1.text, 可以成功显示,这个是对的。" End Sub End Class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值