.Net Compact Framework Doest Not Support Direct Asynchronous method call.

wel working on .net compact framework, i found that as of version 2.0 sp1,it does not support asynchronous calling from a delegate. what this means is that u will not be to call deleagate.BeginXXX.
this is currently not documented very much in the .net cf documentation. The thing is that when u r working in the VS2005 you wil be able to declare a delegate ‘mydel’ and assigned a reference of function say ‘foo’ to it, now when write ‘mydel.’ intellisense wil list the asynchnrous function call namely ‘BeginFoo’ and ‘EndFunction’ and allows u to complete ur line . the compiler will provide the async functiona call stub , beginxxx, endxxx for function xxx, and ur application compiles successfully , now when u run the application it will throw not method not supported exception
this behaviour is not documented in the .net cf documentation. i observed this while working on my application , which has component that was ported from its desktop counterpart. the behaviour appeared from nowhere during the middle of development, while searching for the possible causes , i found that it is not supported on cf and that was burried in an msdn forum,

althoug asynch calls arent supported but threadpool is available on the cf which can lead to an easy workaround for the scenario, a wrapper class with that will take a delegate, and put its own method to be executed on threadpool , does all state management thing required.
class myClass
{
delegate mydelegate ;
object state;
public static object CallAsynch(delegate mydel ,object state)
{
myClass asyn=new myClass();
asyn.mydelegate=mydel
asyn.state=state;
ThreadPool.QueueUserWorkItem(new AsyncCallbackDelegate(asyncCaller),asyn);

}
public asyncCaller(object state)
{
mydelegate();

}
}

that is just a concept, if u want to return the some values from the function u need to add some more code,
🙂

Advertisements

3 responses to “.Net Compact Framework Doest Not Support Direct Asynchronous method call.

  1. Hi,

    I read your post, great, thanks. But i have to return a value from the function, can you help ?

  2. Hi
    thanks for you comments,
    I apologize for replying lately.
    There are 2 ways I can think of to return values from function. I will let you know each.
    1) You can maintain a map of each asyncfunction your are executing and keep its return value in static structure, and provide another method to lookup the return value for that function, ( you will need some async object to use as key in map) it is more like mimicking the .net fx pattern of BeginInvoke EndInvoke pattern.

    2)you can provide another delegate(call back delegate) which the utility class will call when it is finished with your async operation . you can make use of Generics to get a more generalized solution. i.e define your call back delegate as template method and accept the template as argument.
    delegate void MyCallback (T result);

    i would prefer second solution.

    hope that will help.
    regards
    faraz

  3. BeginInvoke in CF is only for when you need your result to get sent back to the UI thread. If you are building a UI app and want to run something on the background the easiest way is to use opennetcf community edition 2.0 BackgroundWorker class.

    At the moment it seems up to 3.5 it isn’t possible to do async calls when you need a result to any thread that made the request, from UI only.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s