'Start timer that starts the next one after the stop?
I've been trying out all the timer or stopwatch options for 2 days and I'm encountering the same problem with all of them.
My idea is to start a timer via keypress that starts the next one after the time has expired and so on... I need the whole thing 3 times because each of these timers starts one or more processes and then lets them fade away again in the next timer.
However, I just can't figure out how to adjust the timer or stopwatch so that it makes sense for me, because I have the feeling that the timers in my IF function keep triggering themselves and I would like to prevent that.
It should be possible that timer 1 is only started again by timer 3 and does not start over immediately after it ended itself.
And I still have one question within a question, how do I release my hotkey from the protected area? or does it make more sense to set a bool that acts as a switch? Would like to have my hotkey as an on/off function, but so far it only switches on. And if i press this button maybe 1-2 seconds it repeats but i dont want to repeat thus actions. i need this actions only once after press.
I'm sure someone from the field can be found here who could explain to me immediately what's going wrong with me and I hope so.
Thanks :-)
// DLL libraries used to manage hotkeys
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
const int MYACTION_HOTKEY_ID = 1;
const int MYACTION2_HOTKEY_ID = 2;
public myFORM()
{
InitializeComponent();
RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 0, (int)Keys.X);
RegisterHotKey(this.Handle, MYACTION2_HOTKEY_ID, 0, (int)Keys.Y);
}
// I NEED FOR MY HOTKEY
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID)
{
// STOPWATCH IS CREATED AND STARTED
Stopwatch stopwatch1 = new Stopwatch();
stopwatch1.Start();
// THAT'S 5 SECONDS
for (int i = 0; i < 500; i++)
{
// I DON'T NEED THE AREA I JUST NEED TIME STOPPING
// SO THAT AN ACTION TAKES PLACE
// HERE I THOUGHT THAT I WILL USE THIS AREA BUT HERE EVERYTHING LOOPS
// AND MY PROGRAM BEGINS STUCK
System.Threading.Thread.Sleep(10);
}
// STOPWATCH STOP
stopwatch1.Stop();
// NOW SOMETHING SHOULD BE DONE
// AND HERE PROCESSES / THREADS ARE STARTED //
frmTimer frmTimer = new frmTimer();
frmTimer.ShowDialog();
// THEN I WANT A SECOND DIRECTLY
// STOPWATCH IS CREATED AND STARTED AFTER NO.1 IS FINISHED
// ONCE THE SECOND STARTS STUCKED IT
Stopwatch stopwatch2 = new Stopwatch();
stopwatch2.Start();
for (int i = 0; i < 500; i++)
{
System.Threading.Thread.Sleep(10);
}
stopwatch2.Stop();
Stopwatch stopwatch3 = new Stopwatch();
stopwatch3.Start();
for (int i = 0; i < 500; i++)
{
System.Threading.Thread.Sleep(10);
}
stopwatch3.Stop();
m_StopThread = true;
}// THIS WILL CLOSE MY APPLICATION VIA HOTKEY
if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION2_HOTKEY_ID)
{
this.Close();
}
base.WndProc(ref m);
}
Solution 1:[1]
[ANSWER FOR OTHER PEOPLE, MAYBE IT HELPS OTHER TOO]
i started again from zero to make it clear and this is my result.
Explain: My StartButton starts a Thread, this Thread have a Stopwatch, if this time is over then flag is set to true and the next Thread is starting. At the same time sets the Stopwatch a other Thread with flag to false and so on....
After the last Stopwatch is stopped is my StartButton is clickable again. And all Threads ready for the next round.
[Optional/FASTSTOP]The StopButton sets all bool to false and stops the Threads.
private bool _startbutton = false;
private bool _stopbutton = false;
private bool _entTHREAD = false;
private bool _acTHREAD = false;
private bool _seTHREAD = false;
private bool _fiTHREAD = false;
private bool _spTHREAD = false;
private bool _enTHREAD = false;
private bool _en2THREAD = false;
private void btnStart_Click(object sender, EventArgs e)
{
if(!_startbutton)
{
_startbutton = true;
frmTimer frmTimer = new frmTimer();
frmTimer.Show();
new Thread(Stopwatch).Start();
}
}
private void btnStop_Click(object sender, EventArgs e)
{
if (!_stopbutton)
{
_stopbutton = true;
_entTHREAD = false;
_acTHREAD = false;
_seTHREAD = false;
_fiTHREAD = false;
_spTHREAD = false;
_enTHREAD = false;
_en2THREAD = false;
}
}
void Stopwatch()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < 480; i++)
{
Thread.Sleep(1);
}
stopwatch.Stop();
_entTHREAD = true;
new Thread(entTHREAD).Start();
new Thread(Stopwatch2).Start();
}
void Stopwatch2()
{
Stopwatch stopwatch2 = new Stopwatch();
stopwatch2.Start();
for (int i = 0; i < 600; i++)
{
Thread.Sleep(1);
}
stopwatch2.Stop();
_entTHREAD = false;
_acTHREAD = true;
new Thread(acTHREAD).Start();
new Thread(Stopwatch3).Start();
}
void Stopwatch3()
{
Stopwatch stopwatch3 = new Stopwatch();
stopwatch3.Start();
for (int i = 0; i < 300; i++)
{
Thread.Sleep(1);
}
stopwatch3.Stop();
_acTHREAD = false;
_seTHREAD = true;
_fiTHREAD = true;
_spTHREAD = true;
new Thread(seTHREAD).Start();
new Thread(fiTHREAD).Start();
new Thread(spTHREAD).Start();
new Thread(Stopwatch4).Start();
}
void Stopwatch4()
{
Stopwatch stopwatch4 = new Stopwatch();
stopwatch4.Start();
for (int i = 0; i < 9000; i++)
{
Thread.Sleep(1);
}
stopwatch4.Stop();
_seTHREAD = false;
_fiTHREAD = false;
_spTHREAD = false;
_enTHREAD = true;
new Thread(enTHREAD).Start();
new Thread(Stopwatch5).Start();
}
void Stopwatch5()
{
Stopwatch stopwatch5 = new Stopwatch();
stopwatch5.Start();
for (int i = 0; i < 150; i++)
{
Thread.Sleep(1);
}
stopwatch5.Stop();
_enTHREAD = false;
_en2THREAD = true;
new Thread(en2THREAD).Start();
new Thread(Stopwatch6).Start();
}
void Stopwatch6()
{
Stopwatch stopwatch6 = new Stopwatch();
stopwatch6.Start();
for (int i = 0; i < 100; i++)
{
Thread.Sleep(1);
}
stopwatch6.Stop();
_en2THREAD = false;
_startbutton = false;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
