-1

Waitタスクの開始前と完了時にマウス カーソルを変更しようとしていますArrow。ただし、カーソルはすぐに別のものに変わるようです。これは私のコードです:

this.Cursor = Cursors.Wait;
dtResults.WriteXml(saveFileDialog.FileName);
this.Cursor = Cursors.Arrow;
MessageBox.Show("Exporting Complete!", "Complete!", MessageBoxButton.OK, MessageBoxImage.Information);

私が間違っていることについてのアイデアはありますか?

4

2 に答える 2

2

You are performing the tasks synchronously. So, the message pump never really gets the wait cursor call as far as the user will see.

To fix this you should do this asynchronously. You can use the Task Parallel Librarysince you are on .NET 4.0:

this.Cursor = Cursors.Wait
//Avoid any closure problems
string fileName = saveFileDialog.FileName
//Start the task of writing the xml (It will run on the next availabled thread)
var writeXmlTask = Task.Factory.StartNew(()=>dtResults.WriteXml(fileName));
//Use the created task to attach what the action will be whenever the task returns.
//Making sure to use the current UI thread to perform the processing
writeXmlTask.ContinueWith(
    (previousTask)=>{this.Cursor = Cursors.Arrow;MessageBox.Show....}, TaskScheduler.FromCurrentSynchronizationContext());
于 2013-02-18T17:55:09.700 に答える
0

私の推測では、これはすべて UI スレッドで行われています。同期的に行われるためDataTable.WriteXml、UI がブロックされ、更新は行われません。これが、カーソルが待機カーソルとして表示されないように見える理由です。

UI を更新して待機カーソルを表示できるようにするには、呼び出しをdtResults.WriteXmlバックグラウンド スレッドに移動する必要があります。BackgroundWorkerの使用をお勧めします

于 2013-02-18T17:56:27.790 に答える