Reputation: 59
I think its not waiting at run.waitOne();
here while processing the line of code,suppose i want to pause these lines of code by clicking the pause button.It should wait once again i click the same pause to resume same thread.But it doesnot wait at all.Any idea to achieve this?
ManualResetEvent run=new ManualResetEvent(false);
bool flag=true;
StartButton_Click(object sender,EventArgs e)
{
ThreadStart ts=new ThreadStart(startprocess);
Thread th=new Thread(ts);
th.Start();
}
private void StartProcess()
{
while(true)
{
if (parentform != null)
{
TestForm(parentform);
parentform = null;
}
else
{
if (a[0] == true)
{
value[0] = method1();//method1 return a double value
Thread.Sleep(500);
}
if (a[1] == true)
{
value[1] =method2();
Thread.Sleep(500);
}
if (a[2] == true)
{
value[2] = method3();
Thread.Sleep(500);
}
if (a[3] == true)
{
value[3] = method4();
Thread.Sleep(500);
}
if (a[4] == true)
{
value[4] =method5();
Thread.Sleep(500);
}
if (a[5] == true)
{
value[5] = method6();
Thread.Sleep(500);
}
run.WaitOne();
timer1.Stop();
DateTime now = DateTime.Now;
string datetime = Convert.ToString(now);
database.InsertTest(datetime,value[0], value[1], value[2], value[3], value[4],value[5]);
}
}
}
private void Pausebutton_Click(object sender, EventArgs e)
{
if (flag)
{
timer1.Enabled = true;
run.Set();
flag = false;
}
else
{
timer1.Enabled = false
run.Reset();
flag = true;
}
}
Upvotes: 1
Views: 1767
Reputation: 1500665
You've got Reset
and Set
the wrong way round - you're trying to make it wait if it's already wait... otherwise, you're setting the event, saying "Keep going."
I'd also add that relying on the state of the thread is a bad idea - you should keep track of whether or not you're logically paused, and set/reset the event accordingly. Otherwise, you've got an awkward situation where two clicks of the button in quick succession might pause and then unpause it, or it might just pause it twice.
Upvotes: 4