设置等待消息并等待光标

时间:2010-09-14 18:25:31

标签: c# wpf

WPF VS 2010 C#

有一个窗口,其中后面的代码的开头是为一个需要45秒或更长时间的视图收集数据...你将如何做一个持续等待的请等待消息 视图被收集。 还有,有没有办法有等待光标?

1 个答案:

答案 0 :(得分:2)

将数据加载到BackgroundWorker。使用BackroundWorkers ProgressChanged - 事件来显示进度消息。

// Show here your wait message make a ProgressBar visible    
Mouse.OverrideCursor = Cursors.Wait; // Maybe you only want to set the cursor of the window. Then change this line code (and the reset)
BackgroundWorker bgWorker = new BackgroundWorker() { WorkerReportsProgress=true};
bgWorker.DoWork += (s, e) => {
    // Load here your file/s
    // Use bgWorker.ReportProgress(); to report the current progress
};
bgWorker.ProgressChanged+=(s,e)=>{
    // Here you will be informed about progress and here it is save to change/show progress. You can access from here savely a ProgressBars or another control.
};
bgWorker.RunWorkerCompleted += (s, e) => {
    // Here you will be informed if the job is done. Close here your wait message or hide your progressbar
    Mouse.OverrideCursor = null;
};
bgWorker.RunWorkerAsync();

因为加载操作现在运行异步,所以您必须确保您的UI不允许用户执行当前应用程序状态(加载)中无效的操作。执行此操作的最简单方法是禁用IsEnabled - 属性设置为false的窗口。