开发者

wpf working state

I have a grid in my application. After user selects some files in ofdialog application processes some calculations. While 开发者_StackOverflow中文版app is making calculations it looks like it is not responding. How to display some picture and make main window in black&white while calculating? Maybe make some dp in MainWindow a la "IsBusy" and bind a popup with picture to it?

How you implement this logic in yours apps?


One easy way is to use the busy indicator from Extended WPF Toolkit:

Dowload the binaries and add project reference to WPFToolkit.Extended.dll.

Next add following namespace in your 'main window':

xmlns:ext="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit.Extended"

Then add the busy indicator in the view (place it so that when shown, it will occupy the whole screen) Here my main window has two rows and I want the control to span on both rows. The control's IsBusy property is bound to a bool property in the view's data context.

<ext:BusyIndicator Grid.RowSpan="2" x:Name="busyIndicator" IsBusy="{Binding IsBusy}" />

The long lasting calculation should be processed in another thread so that it won't block the user interface. For threading you can use BackgroundWorker class.


You should have the long running tasks in a seperate thread to avoid UI blocking. Here's one way you could achieve that:

Define background thread as below:

//Delegate that you could pass into the worker thread
public delegate void ProgressMonitor(string s);

//Call this to start background work
void StartLongRunningWork(ProgressMonitor mon)
{
    using (BackgroundWorker bgw = new BackgroundWorker())
    {
        bgw.DoWork             += WorkerThread;
        bgw.RunWorkerCompleted += WorkerThreadCompleted;
        bgw.RunWorkerAsync(mon);
    }
}

void WorkerThread(object sender, DoWorkEventArgs e)
{
    ProgressMonitor pm = (ProgressMonitor)e.Argument;
    WorkerActual(pm, <any other parameters>);
}

void WorkerActual(ProgressMonitor pm,<any other parameters>)
{
    ...
    pm("Doing x");
    Do long running task
    pm("Doing y");
    ...
}

//This function is called in case of Exception, Cancellation or successful completion
//of the background worker. Handle each event appropriately
void WorkerThreadCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Error != null)
    {
        //Long running task threw an exception
    }
    else
        if (e.Cancelled)
        {
            //Long running task was cancelled
        }
        else
        {
            //Long running task was successfuly completed
        }
}

And Call it as below:

private void UpDateProgressLabel(string s)
{
    this.Dispatcher.BeginInvoke((Action)delegate
       {
           NotificationLabel.Content = s;
       });
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    StartLongRunningWork(UpDateProgressLabel);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜