我曾经成功使用MsgWaitForMultipleObjects来等待事件的句柄并同时输出消息。
但我在Windows Mobile上没有这个程序。
情况如下:
在没有抽取消息的情况下,我不会在窗体上看到等待使用WaitOne的线程的动画,阻止所有内容......
如何在Windows Mobile上实现相同的功能?
由于
答案 0 :(得分:0)
实现您正在寻找的内容的一种简单方法是使用属于OpenNETCF framework
的BackgroundWorker类它本质上是完整.NET框架中System.ComponentModel.BackgroundWorker类的功能副本。
执行如下:
以下是根据MSDN backgroundworker类文档改编的示例,该文档可能为您指明正确的方向。
public Form1()
{
InitializeComponent();
InitializeBackgroundWorker();
}
// Set up the BackgroundWorker object by
// attaching event handlers.
private void InitializeBackgroundWorker()
{
backgroundWorker1.DoWork +=
new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(
backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.ProgressChanged +=
new ProgressChangedEventHandler(
backgroundWorker1_ProgressChanged);
}
protected override void OnLoad(EventArgs e)
{
if (backgroundWorker1.IsBusy != true)
{
// Start the asynchronous operation.
// start your animation here!
backgroundWorker1.RunWorkerAsync();
}
}
// This event handler is where the time-consuming work is done.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
//this is taken from the msdn example, but you would fill
//it in with whatever code is being executed on your bg thread.
for (int i = 1; i <= 10; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
worker.ReportProgress(i * 10);
}
}
}
// This event handler updates the progress.
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//modify your animation here if you like?
}
// This event handler deals with the results of the background operation.
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//your background process is done. tear down your form here..
}