【问题标题】:Waiting for an event with message pump on compactframework在 compactframework 上等待带有消息泵的事件
【发布时间】:2012-08-30 12:53:56
【问题描述】:

我曾经成功使用 MsgWaitForMultipleObjects 来等待事件的句柄并同时发送消息。

但是我在 Windows Mobile 上没有该程序。

情况如下:

  • 我打开显示一些动画的表单
  • 运行线程
  • 等到线程完成(将事件设置为 Set() 时)

如果不发送消息,我将不会在表单上看到动画,例如等待使用 WaitOne 的线程,正在阻塞一切......

如何在 Windows Mobile 上实现相同的功能?

谢谢

【问题讨论】:

  • 对于它的价值,您可以在 CE 平台上 p/invoke MsgWaitForMultipleObjects。工作正常,在某些情况下非常有用。

标签: .net compact-framework


【解决方案1】:

实现您正在寻找的一个简单方法是使用作为 OpenNETCF framework 一部分的 BackgroundWorker 类

它本质上是来自完整 .NET 框架的 System.ComponentModel.BackgroundWorker 类的功能副本。

执行如下:

  1. 表单加载事件开始动画
  2. 启动后台工作人员
  3. 等待后台工作者触发完成事件。

以下是改编自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..


    }

【讨论】:

  • 但是究竟如何等待完成呢?那个背景工作者会为我做什么?我只是不明白...
猜你喜欢
  • 2010-10-28
  • 2022-11-03
  • 1970-01-01
  • 2010-12-07
  • 1970-01-01
  • 1970-01-01
  • 2017-03-13
  • 2011-02-17
  • 1970-01-01
相关资源
最近更新 更多