none
Start timer after Background worker completed? RRS feed

  • Question

  • I'm trying to make a timer start after the background worker is finished, but nothing happens.  I heard you can't start a timer off of a background worker, so i was wondering, how could make this work with the code that i have?

    Private Sub bWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bWorker.RunWorkerCompleted
            Timer1.Start()
            Label1.Text = "Extracting"
            If My.Computer.FileSystem.DirectoryExists("C:\Users\Jacob\AppData\Roaming\Vid Maddness Inc\VMCA\Data") Then
                System.IO.Directory.Delete("C:\Users\Jacob\AppData\Roaming\Vid Maddness Inc\VMCA\Data", True)
                MkDir("C:\Users\Jacob\AppData\Roaming\Vid Maddness Inc\VMCA\Data")
            End If
        End Sub

    Monday, February 27, 2017 6:20 AM

Answers

  • What is the problem? 

    Your code runs. 

    Public Class Form1
        Private WithEvents Bgw As New System.ComponentModel.BackgroundWorker
        Private WithEvents Tim As New Timer With {.Enabled = False, .Interval = 1000}
        Private Sub bWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles Bgw.RunWorkerCompleted
            Tim.Start()
            Controls(0).Text = "Extracting"
            If My.Computer.FileSystem.DirectoryExists("C:\Test\Data") Then
                System.IO.Directory.Delete("C:\Test\Data", True)
            End If
            MkDir("C:\Test\Data")
        End Sub
        Private Sub Tim_Tick(sender As Object, e As EventArgs) Handles Tim.Tick
            Static count As Integer = 0
            count += 1
            Controls(0).Text = CStr(count)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Controls.Add(New Label)
            Bgw.RunWorkerAsync()
        End Sub
    End Class


    Success
    Cor


    • Edited by Cor Ligthert Monday, February 27, 2017 1:31 PM
    • Marked as answer by Vid Maddness Tuesday, February 28, 2017 12:31 AM
    Monday, February 27, 2017 1:29 PM

All replies

  • Hi

    Yes, you can do it from the BackGroundWorkerCompleted event, only not directly from the DoWork event.

    Have you set up the Timer with Enabled = True and a suitable Inteval?


    Regards Les, Livingston, Scotland

    Monday, February 27, 2017 6:51 AM
  • A Timer to start and do what? Looks to me like the Timer needs a delegate that acts on the behalf of the timer to do something? Maybe you need to spawn a Timer thread using a delegate or just spawn a thread with a delegate and make it wait?

    Maybe you start the thread and make it wait first with the thread waiting for a switch to be set to true to make the delegate start execution, after the switch is set to true when the background process stops shuts down.

    http://www.informit.com/articles/article.aspx?p=23020

    Monday, February 27, 2017 7:14 AM
  • Vid,

    Are you trying to show elapsed time while the process runs?

    If so then you need a Stopwatch, not a timer:

    Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private WithEvents bgw As System.ComponentModel.BackgroundWorker Private sw As Stopwatch Private Sub Form1_Load(sender As System.Object, _ e As System.EventArgs) _ Handles MyBase.Load bgw = New System.ComponentModel.BackgroundWorker sw = New Stopwatch sw.Start() bgw.RunWorkerAsync() End Sub Private Sub _ bgw_DoWork(sender As Object, _ e As System.ComponentModel.DoWorkEventArgs) _ Handles bgw.DoWork ' Your process runs here... End Sub Private Sub _ bgw_RunWorkerCompleted(sender As Object, _ e As System.ComponentModel.RunWorkerCompletedEventArgs) _ Handles bgw.RunWorkerCompleted sw.Stop() bgw.Dispose() ' The elapsed time (a TimeSpan) will be the ' stopwatch's .Elapsed property: MessageBox.Show("Elapsed: " & sw.Elapsed.ToString) End Sub End Class



    "One who has no vices also has no virtues..."

    Monday, February 27, 2017 12:03 PM
  • What is the problem? 

    Your code runs. 

    Public Class Form1
        Private WithEvents Bgw As New System.ComponentModel.BackgroundWorker
        Private WithEvents Tim As New Timer With {.Enabled = False, .Interval = 1000}
        Private Sub bWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles Bgw.RunWorkerCompleted
            Tim.Start()
            Controls(0).Text = "Extracting"
            If My.Computer.FileSystem.DirectoryExists("C:\Test\Data") Then
                System.IO.Directory.Delete("C:\Test\Data", True)
            End If
            MkDir("C:\Test\Data")
        End Sub
        Private Sub Tim_Tick(sender As Object, e As EventArgs) Handles Tim.Tick
            Static count As Integer = 0
            count += 1
            Controls(0).Text = CStr(count)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Controls.Add(New Label)
            Bgw.RunWorkerAsync()
        End Sub
    End Class


    Success
    Cor


    • Edited by Cor Ligthert Monday, February 27, 2017 1:31 PM
    • Marked as answer by Vid Maddness Tuesday, February 28, 2017 12:31 AM
    Monday, February 27, 2017 1:29 PM
  • The timer doesn't start when i put Timer1.start() under bWorker_RunWorkerCompleted
    Monday, February 27, 2017 9:05 PM
  • The timer doesn't start when i put Timer1.start() under bWorker_RunWorkerCompleted

    Please make it obvious who you're addressing -- we don't all see this forum the same way.

    *****

    Explain your goal in all of this please.


    "One who has no vices also has no virtues..."

    Monday, February 27, 2017 9:09 PM
  • The timer doesn't start when i put Timer1.start() under bWorker_RunWorkerCompleted

    Then test my code, I made it, the only thing you have to do is create a windows forms project, copy a paste the code in the code part and click start. You even don't need to make controls. You will see it starts. 

    After that you can see what you forgot.


    Success
    Cor


    Monday, February 27, 2017 9:59 PM
  • The timer doesn't start when i put Timer1.start() under bWorker_RunWorkerCompleted

    Please make it obvious who you're addressing -- we don't all see this forum the same way.

    *****

    Explain your goal in all of this please.


    "One who has no vices also has no virtues..."

    My goal is to download files from and ftp server using the background worker, then have a timer start AFTER the background worker is complete.  The timer controls a progress bar, once the progress bar hits a value of 100, then i have something else happen.  My problem is that the timer won't start after the background worker is finished, and i'm not sure how to make it start
    Monday, February 27, 2017 11:09 PM
  • The timer doesn't start when i put Timer1.start() under bWorker_RunWorkerCompleted

    Then test my code, I made it, the only thing you have to do is create a windows forms project, copy a paste the code in the code part and click start. You even don't need to make controls. You will see it starts. 

    After that you can see what you forgot.


    Success
    Cor


    Ok i'll give it a try
    Monday, February 27, 2017 11:10 PM

  • My goal is to download files from and ftp server using the background worker, then have a timer start AFTER the background worker is complete.  The timer controls a progress bar, once the progress bar hits a value of 100, then i have something else happen.  My problem is that the timer won't start after the background worker is finished, and i'm not sure how to make it start

    That makes no sense. If your download is complete then what does the timer do?


    "One who has no vices also has no virtues..."

    Tuesday, February 28, 2017 12:04 AM

  • My goal is to download files from and ftp server using the background worker, then have a timer start AFTER the background worker is complete.  The timer controls a progress bar, once the progress bar hits a value of 100, then i have something else happen.  My problem is that the timer won't start after the background worker is finished, and i'm not sure how to make it start

    That makes no sense. If your download is complete then what does the timer do?


    "One who has no vices also has no virtues..."

    forget it i found a way around this issue
    Tuesday, February 28, 2017 12:30 AM