在compactframework上等待带有消息泵的事件

时间:2012-08-30 12:53:56

标签: .net compact-framework

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

但我在Windows Mobile上没有这个程序。

情况如下:

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

在没有抽取消息的情况下,我不会在窗体上看到等待使用WaitOne的线程的动画,阻止所有内容......

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

由于

1 个答案:

答案 0 :(得分:0)

实现您正在寻找的内容的一种简单方法是使用属于OpenNETCF framework

的BackgroundWorker类

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

执行如下:

  1. 在表单加载事件上启动动画
  2. 启动backgroundworker
  3. 等待后台工作人员解雇完成事件。
  4. 以下是根据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..
    
    
        }