none
Do a task in future RRS feed

  • Question

  • Hi,

    I simply use a custom DateTimePicker control in vb.net and use choose a date & time in the future for some task.

    Inside a timer I simply check:

    If DateTimeInput.Value = Now Then Task...

    That works fine, my first question is that is this a good way to do it?

    But my second question which is vital, is that to show a progress bar, how to calculate how much time remains and show it via progress bar?

    Thanks for advise.

    Thursday, November 7, 2019 6:36 PM

Answers

  • Hi

    OK, have a look at this and see if it helps. This is a stand alone example, and you need to add controls as per image below.

    ' Form1 with Label1 (current time)
    ' DateTimePicker1 (Task start time)
    ' Button1 to start Timer
    ' 
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With DateTimePicker1
    			.Format = DateTimePickerFormat.Custom
    			.CustomFormat = "dd MMM yyyy HH:mm:ss"
    			.Value = Now
    		End With
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		' set delay before Task Start
    		Dim TaskStart As DateTime = DateTimePicker1.Value
    		Dim delay As TimeSpan = TaskStart - Now
    		If delay.TotalSeconds < 2 Then
    			MessageBox.Show("Need to set future time further away!")
    			Exit Sub
    		End If
    		With ProgressBar1
    			.Value = 0
    			.Maximum = CInt(delay.TotalSeconds)
    		End With
    
    		With Timer1
    			.Interval = 1000
    			.Enabled = True
    		End With
    	End Sub
    
    	Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    		Dim v As Integer = ProgressBar1.Value + 1
    		If v > ProgressBar1.Maximum Then
    			Timer1.Enabled = False
    			' START TASK HERE
    			MessageBox.Show("Started")
    		Else
    			ProgressBar1.Value = v
    		End If
    	End Sub
    
    	Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
    		Label1.Text = Now.ToString("dd MMM yyyy HH:mm:ss")
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by OSVBNET Friday, November 8, 2019 9:26 AM
    Thursday, November 7, 2019 8:38 PM

All replies

  • Hi

    It is not clear what you want.

    You need a start time and an end time to enable a ProgressBar to be set to show progress. Do you have both of those in mind?

    *

    Your use of DateTimeInput.Value = Now Then is not really of much use - what do you do with Task at that point? Is it the start time for a the Task?

    *

    You need to explain more. What are you trying to achieve? Are you trying to find the elapsed time for a Task perhaps? In any case, your starting point has to be to get/decide a start time and an end time. Once you have those, post back here and explain.

    *

    Or, maybe you want to start a Task at some time in the future and you want a ProgressBar to display the time until the Task will start? Sounds a more plausible thing to be doing to me!


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, November 7, 2019 7:34 PM
    Thursday, November 7, 2019 7:32 PM
  • Thanks, I simply mean, using a DateTimePicker, user select a date and time in the future, and I should do a task on that time!

    Pretty simple! Inside a Timer with 1 second interval, I check if DateTimeInput.Value = Now

    Then it means, now's the time to run the task.

    I seems to work now, I just can't figure out how to calculate a ProgressBar!

    * Yes, it's start time of the task, wanna start a task in the future and need to show the remaining time in ProgressBar.
    • Edited by OSVBNET Thursday, November 7, 2019 7:59 PM
    Thursday, November 7, 2019 7:58 PM
  • Hi

    OK, have a look at this and see if it helps. This is a stand alone example, and you need to add controls as per image below.

    ' Form1 with Label1 (current time)
    ' DateTimePicker1 (Task start time)
    ' Button1 to start Timer
    ' 
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With DateTimePicker1
    			.Format = DateTimePickerFormat.Custom
    			.CustomFormat = "dd MMM yyyy HH:mm:ss"
    			.Value = Now
    		End With
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		' set delay before Task Start
    		Dim TaskStart As DateTime = DateTimePicker1.Value
    		Dim delay As TimeSpan = TaskStart - Now
    		If delay.TotalSeconds < 2 Then
    			MessageBox.Show("Need to set future time further away!")
    			Exit Sub
    		End If
    		With ProgressBar1
    			.Value = 0
    			.Maximum = CInt(delay.TotalSeconds)
    		End With
    
    		With Timer1
    			.Interval = 1000
    			.Enabled = True
    		End With
    	End Sub
    
    	Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    		Dim v As Integer = ProgressBar1.Value + 1
    		If v > ProgressBar1.Maximum Then
    			Timer1.Enabled = False
    			' START TASK HERE
    			MessageBox.Show("Started")
    		Else
    			ProgressBar1.Value = v
    		End If
    	End Sub
    
    	Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
    		Label1.Text = Now.ToString("dd MMM yyyy HH:mm:ss")
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by OSVBNET Friday, November 8, 2019 9:26 AM
    Thursday, November 7, 2019 8:38 PM
  • Hey bud, thanks many many times! Just on a side note, if instead of

    CInt(delay.TotalSeconds)

    I use Convert. should I use .ToInt16 or .ToInt32 or .ToInt64 ?!

    I think CInt equals to Int32 right?

    I ask this because delay.TotalSeconds is Double and if so should use Convert.ToInt64?
    • Edited by OSVBNET Thursday, November 7, 2019 10:21 PM
    Thursday, November 7, 2019 9:15 PM
  • Hey bud, thanks many many times! Just on a side note, if instead of

    CInt(delay.TotalSeconds)

    I use Convert. should I use .ToInt16 or .ToInt32 or .ToInt64 ?!

    And how long can be this set in the future? Better to set a reasonable limit, for example, maximum 1 month later from now on...

    Hi

    Check out HERE for Int16 v Ibt32

    As for the future DateTime - quite a long time, so long that you will likely not be around, so don't hold your breath. A mere Month is like a grain of sand in the ocean :)


    Regards Les, Livingston, Scotland

    Thursday, November 7, 2019 10:20 PM
  • Thanks indeed :)
    Friday, November 8, 2019 9:26 AM