我曾经成功地使用 MsgWaitForMultipleObjects 来等待事件的句柄并同时发送消息。
但是我在 Windows Mobile 上没有该程序。
情况如下:
- 我打开显示一些动画的表单
- 运行线程
- 等到线程完成(当它将事件设置为 Set() 时)
如果不发送消息,我将看不到表单上的动画,例如使用 WaitOne 等待线程,阻塞一切......
如何在 Windows Mobile 上实现相同的功能?
谢谢
我曾经成功地使用 MsgWaitForMultipleObjects 来等待事件的句柄并同时发送消息。
但是我在 Windows Mobile 上没有该程序。
情况如下:
如果不发送消息,我将看不到表单上的动画,例如使用 WaitOne 等待线程,阻塞一切......
如何在 Windows Mobile 上实现相同的功能?
谢谢
实现您正在寻找的一个简单方法是使用作为OpenNETCF 框架一部分的 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..
}