同时运行两个事件

时间:2019-04-08 19:37:05

标签: vb.net

已经存储在表1中的动画文本代码可以很好地工作,而MDIParent1中没有任何错误。但是当我单击Button_Print时执行另一个代码作为打印代码时,动画文本将停止Temporary动画,并且文字会很沉重。请如何在不影响动画文字的情况下运行其他代码? 我在Module1中的代码

Public Sub Text_Panel_Animation()
    Try
        Dim da As New OleDbDataAdapter("Select * from Table1 order by Id", Con)
        Dim dt As New DataTable
        da.Fill(dt)
        MDIParent1.Label1.Left = 0 - MDIParent1.Label1.Width
        If dt.Rows.Count > 0 Then
            For r As Integer = 1 To dt.Columns.Count - 1
                MDIParent1.Label1.Text &= "  " & (dt(0)(r).ToString)
            Next
            MDIParent1.Timer1.Start()
        End If
        Con.Close()
    Catch ex As Exception
        MsgBox(ex.Message(), MsgBoxStyle.Critical, "Error")
    End Try
End Sub

在MDIParent1加载中。我输入以下代码:

 Call Text_Panel_Animation()

在MDIParent1中..我也有:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    If Label1.Left >= Me.Panel1.Width Then Label1.Left = 0 - Label1.Width
    Label1.Left += 1
End Sub

我尝试使用此代码,但是我遇到了同样的问题:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Task.Run(Sub()
         Try
             Dim Dt As New DataTable
             Dim SQLstr As String = "Select * from Table1"
             SQLstr = "Select * from Table1 order BY Id"
             Dim Da As New OleDbDataAdapter(SQLstr, Con)
             Da.Fill(Dt)
             Dim Rpt As New Crystal1
             Rpt.SetDataSource(Dt)
             Dim frm As New Form1
             Me.Dispose()
             frm.Show()
             Form1.CrystalReportViewer1.Zoom(100%)
         Catch
         End Try
    End Sub)
End Sub

1 个答案:

答案 0 :(得分:1)

您可以在新任务中开始打印

Private Sub Button_Print_Click (sender As Object, e As EventArgs) Handles Button_Print.Click) 
    Task.Run(Sub()
        'TODO: Do the printing here...
    End Sub)
End Sub

请注意,您必须采取预防措施,才能从此其他任务访问UI。参见:VB.NET: Invoke Method to update UI from secondary threads


在其他任务中,我不会做任何UI事情,因为这可能会导致问题。相反,我只会查询数据库并在另一个任务中呈现报表,然后在执行与UI相关的事情之前等待此任务。我本人不是在使用Crystal Reports,但是可能是这样的

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Rpt As Crystal1

    Await Task.Run(
        Sub()
            Try
                Dim Dt As New DataTable
                Dim SQLstr As String
                SQLstr = "Select * from Table1 order BY Id"
                Dim Da As New OleDbDataAdapter(SQLstr, Con)
                Da.Fill(Dt)
                Rpt = New Crystal1
                Rpt.SetDataSource(Dt)
            Catch
            End Try
        End Sub)

    Dim frm As New Form1
    Me.Dispose()
    frm.Show()
    Form1.CrystalReportViewer1.Zoom(100%)
    Form1.CrystalReportViewer1.ReportSource = Rpt
End Sub

不要忘记方法标题中的Async关键字。

注意:如果Me是主要形式,则Me.Dispose()将终止应用程序。