AsyncCallback for a stream on a compact basis

I need to implement threads to increase load time in a compact application. I want to start a background thread to make some calls to an external API, while the main thread caches some forms. When the background thread is completed, I need to start two more threads to fill the data cache.

I need a background thread to be able to execute the callback method, so I know that this is done, and the following two threads can be started, but the BeginInvoke method for the delegate is not supported in a compact structure, so how else can I do this?

+3
source share
2 answers

You can organize it yourself, just make sure your thread method called the completed method (or event) when it was done.

Since CF does not support the ParameterizedThreadStart parameter, I once made a small helper class.

The following is a snippet and has not been 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();
}

You need to inherit and execute DoWork and Completed. It would probably make sense to use <T> for state ownership, just noticed that.

+3
source

I know this is an old question, but if you are using CF 3.5, this will be a good solution to the problem. Using delegate lambda ..

ThreadStart starter = () => DoWorkMethodWithParams( param1, param2);
Thread myNewThread = new Thread(starter){IsBackground = true};
myNewThread.Start();
+1
source

Source: https://habr.com/ru/post/1711575/


All Articles