简体   繁体   English

.net紧凑框架不支持异步调用委托

[英].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. 我最近开始使用Window Phone7。我创建了一个委托,并尝试异步调用它。 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. 但是,当我执行此代码时,它抛出了一个异常,说.net紧凑框架不支持异步调用委托。 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. 根据我对WP7框架的了解,它几乎对所有内容都使用异步调用,因此我无法理解为什么不允许这样做。

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. 我想在PhoneApplicationPage_loaded完成并启动UI后执行一些代码,我想到了从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. 它属于“ nice to have”类别,但对于在TP线程上运行代码不是必需的。 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. 该代码是CLR Remoting支持代码。 And is missing in the CLR branch that started in the Compact Framework and evolved into Silverlight and Windows Phone. 在Compact Framework中启动并发展为Silverlight和Windows Phone的CLR分支中缺少此功能。 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. 比较Silverlight的约5兆字节和台式机的约50兆字节,这是一个壮举。

The alternative is to use ThreadPool.QueueUserWorkItem() instead. 替代方法是使用ThreadPool.QueueUserWorkItem()。 It is restricted in the arguments you can pass, easily worked around by using a lambda expression to capture them. 它受到您可以传递的参数的限制,可以通过使用lambda表达式捕获它们来轻松解决。 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. 您可以改用BackgroundWorker

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();
     }
 }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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