Skip to main content

Prevent the application from freezing

If the extension executes a process that takes a long time, the application may appear to freeze. The following code allows you to draw the UI while the extension is running.

using System.Windows.Threading; 

///<summary>
///DoEvents
///</summary>
void DoEvents()
{
var frame = new DispatcherFrame();

var callback = new DispatcherOperationCallback(obj =>
{
((DispatcherFrame)obj).Continue = false;
return null;
});

Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, callback, frame);
Dispatcher.PushFrame(frame);
}

void SomeCommand(ICommandContext c, ICommandParams p)
{
for ( var i=0;i<100; i++)
{
//Do some processing
...

//Update the application screen
DoEvents();
}
}
Caution

Instead of blocking the user interface, it is possible to operate the screen, which may result in unexpected behavior, so please use with caution.