lock with while loop on multthread application form c# blocking all
threads on single core machine
having a problem with my multithreaded app on windows form C# application,
this application works fine on multicore machine, but then when it runs in
a single core machine all the threads went hang, I`m using
backgroundWorker, code follow as this:
class custonTime // delay class
{
public void sleep_sec(int sleep)
{
int time_now = Environment.TickCount;
int time_sleep = sleep;
while ((Environment.TickCount - time_now) < time_sleep) ;
}
}
when pres a button on UI the flags action and doAlways are seted true and
the following backgroundworkers are started, the backgorundworker1 await
and fire the task on right time the other one do the task
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
while(action)
{
if(doItAlways)
{
//do important tasks
}
if(task1)
{
//do things
task1=false;
}
if(task2)
{
//do things
task1=false;
}
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
task1=true;
manegerTimewr2.sleep_sec(12000);//call delay class to wait 12 secs
task2=true;
manegerTimewr2.sleep_sec(12000);//call delay class to wait 12 secs
}
My delay class lock not just my backgorundworker1 but the
backgroundworker2 as well, make impossible to do the tasks fired by flag
doItAlways, the application is only released when the while loop finish.
and it just happens on single core machine only
there is another method? a direct and easy method to hang a thread until
elapse some time without block the other ones?
thank you all, Best regards
No comments:
Post a Comment