Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How can i pause and resume an application with a wpf UI layer where the execution of the application should pause/resume depending on the value of a variable in BI layers.
For example,
C#
If(_IsAtomic)
{
    canPause =true;
    if(PauseEventTriggeredByUser)
{
        Pause.(canPause);
    }
}
Else
{
    //continue Execution
}

P.S. I have tried using backGroundWorker but its not working
Posted
Updated 6-May-13 20:03pm
v3
Comments
[no name] 6-May-13 12:16pm    
What does "pausing and resuming a UI layer" mean?
Member 8773837 7-May-13 1:58am    
Sorry, my mistake..It should have been pausing/resuming the application
Sergey Alexandrovich Kryukov 6-May-13 13:41pm    
"Not working" is not informative.
—SA
Member 8773837 7-May-13 2:01am    
the backgroundworker in the abovementioned application calls a function which calls another function and that calls another ..till i reach the function which is atomic..that is, one cannot pause the application while that function is running..one can pause resume before that, or after that.

1 solution

You cannot pause/resume WPF application; it simply makes no sense. You can pause/resume operation of some thread in a controlled manner. To do so, you can use the class System.Threading.ManualResetEvent:
http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx[^].

A thread in question should be put to a wait state at its call to the method WaitOne:
http://msdn.microsoft.com/en-us/library/58195swd.aspx[^].

If the instance of the ManualResetEvent is non-signaled, the thread will be switched off and not scheduled back to execution until awaken, which can happen on different conditions (such as timeout or Thread.Abort), but, most essentially, it happens when the instance of the ManualResetEvent is made signaled by some other thread (typically, a UI thread). This way, you can throttle execution of some thread by other thread(s) by putting it to a wait state in certain points of code. Note that the thread in a wait state uses zero CPU time, so this is not a wasteful spin wait.

—SA
 
Share this answer
 
Comments
db7uk 6-May-13 17:36pm    
Excellent answer!
Sergey Alexandrovich Kryukov 6-May-13 17:48pm    
Thank you.
—SA
Member 8773837 7-May-13 9:24am    
thanks SA for ur solution. I have implemented it but the problem persists. I think this is so because the execute button on being clicked starts the execution down to the BI layers and the delay in manually clicking the pause button renders it useless. I tried using thred.sleep and dispatcher..but guess this is not the right aprroach.Plz help!
Sergey Alexandrovich Kryukov 7-May-13 10:36am    
I cannot see your solution to help you. You did something wrong. How can I know what? Apparently, you are trying to use something using trial-and-error approach, without understanding (why Sleep again?!! why Dispatcher?!! what the Dispatcher does in this case?); it will lead your nowhere. You need to understand everything clearly. "Do or do not. There is no try."

I provided you the clear mechanism which is absolutely universal and unrelated to other aspects.

—SA
Member 8773837 8-May-13 0:01am    
Got u. Thanks again SA.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900