且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据BI层中的变量暂停/恢复wpf应用程序

更新时间:2023-02-03 10:00:36

你无法暂停/恢复WPF应用程序;它根本没有意义。您可以以受控方式暂停/恢复某些线程的操作。为此,您可以使用类 System.Threading.ManualResetEvent

http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx [ ^ ]。



有问题的线程在调用方法时应该处于等待状态 WaitOne

http://msdn.microsoft.com/en-us/library/58195swd.aspx [ ^ ]。



如果 ManualResetEvent 的实例未发出信号,则该线程将被关闭,并且在被唤醒之前不会被调度回执行,这可能发生在不同的条件下(例如超时或 T hread.Abort ),但最重要的是,当某个其他线程(通常是一个UI)发出 ManualResetEvent 的实例时,会发生这种情况线)。这样,您可以通过在某些代码点将其置于等待状态来限制其他线程执行某些线程。请注意,处于等待状态的线程使用零CPU时间,因此这不是浪费的旋转等待。



-SA

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,

If(_IsAtomic)
{
    canPause =true;
    if(PauseEventTriggeredByUser)
{
        Pause.(canPause);
    }
}
Else
{
    //continue Execution
}


P.S. I have tried using backGroundWorker but its not working

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