的AsyncCallback对紧凑型框架线程 [英] AsyncCallback for a thread on compact framework

查看:149
本文介绍了的AsyncCallback对紧凑型框架线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现线程来提高加载时间在一个紧凑的框架应用程序。我想火的后台线程一些呼叫做外部API,而主线程缓存一些表格。当后台线程完成,我要火了两个线程来填充数据缓存。

I need to implement threading to improve load time in a compact framework app. I want to fire off a background thread to do some calls to an external API, while the main thread caches some forms. When the background thread is done, I need to fire off two more threads to populate a data cache.

我需要后台线程能够执行一个回调方法,所以我知道它的完成和下两个线程可以启动,但不支持在紧凑的框架上委托的BeginInvoke方法,所以还能怎么我做到这一点?

I need the background thread to be able to execute a callback method so I know it's done and the next two threads can be started, but the BeginInvoke method on a delegate is not supported in the compact framework, so how else can I do this?

推荐答案

您可以自己安排吧,只要确保你的线程方法调用完成的方法(或事件)时,它的完成。

You can arrange it yourself, simply make sure your thread method calls a completed method (or event) when it's done.

由于CF不支持ParameterizedThreadStart要么,我曾经做了一个小帮手类。

Since CF doesn't support the ParameterizedThreadStart either, I once made a little helper class.

以下是一个提取物和未重新测试

The following is an extract and was not re-tested:

//untested
public abstract class BgHelper
{
    public System.Exception Error { get; private set; }
    public System.Object State { get; private set; }

    public void RunMe(object state)
    {
        this.State = state;
        this.Error = null;

        ThreadStart starter = new ThreadStart(Run);
        Thread t = new Thread(starter);
        t.Start();            
    }

    private void Run()
    {
        try
        {
            DoWork();                
        }
        catch (Exception ex)
        {
            Error = ex;
        }
        Completed(); // should check Error first
    }

    protected abstract void DoWork() ;

    protected abstract void Completed();
}

您需要继承和实施DoWork的和已完成。或许,这将是有意义的使用< T>为国有财产,只注意到这一点。

You are required to inherit and implement DoWork and Completed. It would probably make sense to use a < T> for the State property, just noticed that.

这篇关于的AsyncCallback对紧凑型框架线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆