WPF BusyWindow unbreakable by main thread

How to create really unbreakable wpf busy window in separate thread? Main thread run some a few second lasting intensive computation code. This code can not allow call UI refresh (DoEvents)(do not have cycle), and is not moveable to separate thread.

Busy Window have some spinner/progress, which should rotate/move. But it doesn't rotate/move if the main thread run intensive computation.

Most of "busy window in separate thread" - founded on web - do not work, if the main thread run intensive computation (nor Task, nor BackgroundWorker). Limitation is .net framework 4.0.

For testing, if I try to run intensive code in the main thread:

while(true) {}

then most web "busy window in separate thread" became blocked.

Best results I have with this code:

 var thread = new Thread(new ThreadStart(() =>
        {
            SynchronizationContext.SetSynchronizationContext(
                new DispatcherSynchronizationContext(
                    Dispatcher.CurrentDispatcher));

            asyncCtl = creator();
            asyncCtl.CreateControl();
            asyncCtl.Show();

            System.Windows.Threading.Dispatcher.Run();
        }));


        thread.SetApartmentState(ApartmentState.STA);
        thread.IsBackground = true;
        thread.Start();

        Dispatcher.CurrentDispatcher.ShutdownStarted += CurrentDispatcher_ShutdownStarted;
        Dispatcher.CurrentDispatcher.ShutdownFinished += CurrentDispatcher_ShutdownFinished;

When computation function finished, this code is called:

  asyncCtl.Dispatcher.BeginInvokeShutdown(DispatcherPriority.Background);

But - events ShutdownStarted / ShutdownFinished are called on main thread exits. This seems to me too late. If application run BusyWindow 10 times, these events are called 10 times on main thread exits. I afraid if there are 10 times shown BusyWindow, there stay hang 10 non-shutdowned threads which ends lately - on main application exits.

So, how to correctly create and dispose such busy window? Thanks.

链接地址: http://www.djcxy.com/p/38000.html

上一篇: 使用宏提取Microsoft Outlook中的IP地址

下一篇: 主线程不能破解WPF BusyWindow