SABRINE
SABRINE

Reputation: 27

Run two events simultaneously

The code for my animated text already stored in my Table1 works very well without any error in my MDIParent1.But when I execute another code for exemple an print code when i click on Button_Print The animated text will stop Temporary and the animation of the text will be very heavy.Please How to run other code without effect on my animated text ? My code in 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

In MDIParent1 Load .. I put this code :

 Call Text_Panel_Animation()

And In MDIParent1 .. i have also :

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

I tried with this code but i have a same problem :

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

Upvotes: 0

Views: 141

Answers (1)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112772

You can start the printing in a new task

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

Note that you have to take precautions to be able to access the UI from this other task. See: VB.NET: Invoke Method to update UI from secondary threads


I would not do any UI stuff in the other task, since this can lead to problems. Instead, I would only query the DB and render the report in another task and then await this task before doing UI-related things. I'm not using Crystal Reports myself, but it would probably be something like this

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

Do not forget the Async keyword in the method header.

Note: If Me is the main form, then Me.Dispose() will terminate the application.

Upvotes: 1

Related Questions