Home » VB.NetRSS

Start / Pause button to start and/or pause a program

I am trying to make a start and pause button (and hence continue) to start, pause, and continue my program.  The start button works but the pause button does not.  Can someone look at my code and see what the pause button needs to do to pause the program?  I am using VB 2005.  Here is my code.  Thanks

Imports Microsoft.Win32
Imports System.Threading
PublicClass Form1
  Dim m_event As ManualResetEvent = New ManualResetEvent(False)
  Dim tw As Thread
  PrivateSub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
    
  EndSubPrivateSub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    'when timer ends Itunes runs and plays a song
    Process.Start("Itunes.exe", "c:\Pieces_of_April.mp3")
    Timer1.Enabled = FalseMe.Visible = FalseEndEndSubPrivateSub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
    If RadioButton2.Checked = TrueThen
      Timer1.Enabled = True
      Timer1.Interval = 1200000  'clock runs for 20 minutesEndIfEndSubPrivateSub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
    If RadioButton3.Checked = TrueThen
      Timer1.Enabled = True
      Timer1.Interval = 1800000  'clock runs for 30 minutesEndIfEndSubPrivateSub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
    If RadioButton4.Checked = TrueThen
      Timer1.Enabled = True
      Timer1.Interval = 2700000  'clock runs for 45 minutesEndIfEndSubPrivateSub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged
    If RadioButton5.Checked = TrueThen
      Timer1.Enabled = True
      Timer1.Interval = 3600000  'clock runs for 60 minutesEndIfEndSubPrivateSub Form1_FormClosing(ByVal sender AsObject, ByVal e As System.Windows.Forms.FormClosingEventArgs) HandlesMyBase.FormClosing
    tw.Abort()
    tw.Join()
    m_event.Close()
  EndSubPrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
    Start.Text = "Start"
    Pause.Text = "Pause"
    Pause.Enabled = False
    tw = New Thread(AddressOf run)
    tw.Start()
  EndSubPrivateSub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
    m_event.Set()
    Start.Enabled = False
    Pause.Enabled = TrueIf RadioButton1.Checked = TrueThen
      Timer1.Enabled = True
      Timer1.Interval = 15000  'clock runs for 15 secondsEndIfEndSubPrivateSub Pause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pause.Click
    m_event.Reset()
    Pause.Enabled = False
    Start.Enabled = TrueEndSubPublicSub run()
    Dim CounterCreationData AsInteger = 0
    DoWhileTrue
      m_event.WaitOne()
      Console.WriteLine("executing" & DateTime.Now.ToLongTimeString)
    LoopEndSubEndClass

Thanks

David H.

 

3 Answers Found

 

Answer 1

Put this code in the Pause Click:

Timer1.Enabled = False
 

Answer 2

I did that but it doesn't do what I really want it too and that is to pause... but pick up where it

left off when the start button is pressed again.  The Timer1.Enabled = False will stop the

program but when I press start again the timer goes back to the beginning of the time that was

set.

 

Answer 3

OK I see your issue. Setting timer to 15000 means it ticks every 15 secs, so you do not have any control over it in between this time (I mean you do not know how much time has elapsed etc) So what I suggest is to do this:

Set 

Timer1.Interval = 1000

Dim timeCounter As Int16

PrivateSub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  timeCounter++
  If timeCounter == 15 Then
    'when timer ends Itunes runs and plays a song
    Process.Start("Itunes.exe", "c:\Pieces_of_April.mp3")
    Timer1.Enabled = FalseMe.Visible = False
  End If
  EndEndSub

What we are doing here is that Timer1 will tick every sec so you can pause it using 
Timer1.Enabled = False in Pause Click
Also when you start again, timeCounter is somewhere between 0 to 15 and would get back to incrementing towards 15 and when it reaches 15 (in 15 secs) the song is played.

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter