Problems starting a Timer Programmaticall y within a BackgroundWorke r.
I am trying to start a Timer inside a Backgroundworke r. I want to start the BackGroundWorke r and then have a timer tick a calculation reppeting inside the BackGroundWorke r. The Code below seems to work ok for sending back the informaton "DoWork - Enter" and "DoWork - Exit, but the Timer "Timer_Ping " is newer ticked and the line "Time - Tick" is newer send back.
Why is my timer not starting? What am I doing wrong?
Peter Schwennesen
I am trying to start a Timer inside a Backgroundworke r. I want to start the BackGroundWorke r and then have a timer tick a calculation reppeting inside the BackGroundWorke r. The Code below seems to work ok for sending back the informaton "DoWork - Enter" and "DoWork - Exit, but the Timer "Timer_Ping " is newer ticked and the line "Time - Tick" is newer send back.
Why is my timer not starting? What am I doing wrong?
Peter Schwennesen
Code:
Imports System.Text
Imports System.Drawing
Imports System.ComponentModel
Imports System
Public Class PingVindow
Inherits TableLayoutPanel
Private TableLayoutPanel_Ping As TableLayoutPanel = CType(Me, TableLayoutPanel)
Private ListBox_PingResult As New ListBox
Private WithEvents BackgroundWorker_Ping As New BackgroundWorker
Public Sub New()
With ListBox_PingResult
.Dock = DockStyle.Fill
.IntegralHeight = False
.Font = New Font("Courier New", 9.0!, FontStyle.Regular)
.Items.Add("Test Started")
End With
With TableLayoutPanel_Ping
.RowCount = 1
.RowStyles.Add(New RowStyle(SizeType.Percent, 100))
.ColumnCount = 2
.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 50))
.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 50))
.Controls.Add(ListBox_PingResult, 0, 0)
.Dock = DockStyle.Fill
End With
BackgroundWorker_Ping.WorkerReportsProgress = True
BackgroundWorker_Ping.RunWorkerAsync()
End Sub
'------------------------------------------------------------------------------------------------------
Private Sub BackgroundWorker_Ping_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker_Ping.DoWork
BackgroundWorker_Ping.ReportProgress(0, "DoWork - Enter")
Dim Timer_Ping As New Timer
AddHandler Timer_Ping.Tick, AddressOf Timer_Ping_Tick
Timer_Ping.Interval = 1000
Timer_Ping.Enabled = True
Timer_Ping.Start()
BackgroundWorker_Ping.ReportProgress(0, "DoWork - Exit")
End Sub
Private Sub BackgroundWorker_Ping_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker_Ping.ProgressChanged
Me.ListBox_PingResult.Items.Add(e.UserState.ToString)
End Sub
'------------------------------------------------------------------------------------------------------
Public Sub Timer_Ping_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
BackgroundWorker_Ping.ReportProgress(0, "Timer - Tick")
End Sub
End Class