简体   繁体   中英

.net compact framework does not support invoking delegates asynchronously

I recently started working window phone 7. I created a delegate and tried to call it asynchronously. The code was something like this:

public class1
{
     public delegate void fireAlwaysDelegate();
     fireAlwaysDelegate fad;
     public class1()
     {
       initializeComponents();
       fad=new fireAlwaysDelegate(fireAlways)
     }

     fireAlways()
     {
       //some code
     }

     PhoneApplicationPage_loaded()
     {
        //some code
        fda.beginInvoke(null,null);
     }
}

But, when I executed this code, it threw an exception saying .net compact framework does not support invoking delegates asynchronously. As per my understanding of WP7 framework it uses async calls for almost everything, so i am not able to understand why this is not permitted.

Any work around for this thing.

I wanted to execute some code once the PhoneApplicationPage_loaded is complete and UI is Launched, i thought of calling an async delegate from PhoneApplicationPage_loaded.

Also i will like to understand why async call to delegates is not permitted.

The ability to invoke a delegate target on a thread-pool thread is a bit of an odd-ball feature for a delegate. It falls in the "nice to have" category but isn't essential to run code on a TP thread. The actual implementation of it resembles an iceberg, there's a huge chunk of code required to be able to build a stack frame with arbitrary arguments on another thread, manage their lifetime, capture the execution results and marshal them back to the calling thread.

That code is the CLR Remoting support code. And is missing in the CLR branch that started in the Compact Framework and evolved into Silverlight and Windows Phone. Platforms where size matters, it got cut in the effort to keep it small. Compare ~5 megabytes for Silverlight with ~50 megabytes for the desktop, quite a feat.

The alternative is to use ThreadPool.QueueUserWorkItem() instead. It is restricted in the arguments you can pass, easily worked around by using a lambda expression to capture them. Only thing you need to fret about a bit are exceptions, they'll get raised on the worker thread and will terminate your app if you don't catch them there.

You can use BackgroundWorker instead.

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    private BackgroundWorker bw = new BackgroundWorker();

    public MainPage()
    {
        InitializeComponent();
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
    }

    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        FireAlways();
    }

  public void FireAlways()
  {
    //some code
  }

 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
 {
     if (bw.IsBusy != true)
     {
         bw.RunWorkerAsync();
     }
 }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM