Async Method Call (w. delegates and callback)

S

sebastian

Hello there,

I'm currently developing an NETCF 3.5 based application which has to
perfom several time consuming tasks whose outputs are needed (no 'fire
and forget') but I want to preserve UI responsiveness to do do other
things while the async methods are crunching numbers ;)

Altough VS2008s IntelliSense shows BeginInvoke and EndInvoke on my
delegate it seems async method calling with callback doesn't really
work on NETCF as all I got was a lousy 'NotSupportedException' (http://
www.programmersheaven.com/2/Calling-a-method-asynchronously)

It's strange, because the BeginInvoke, and EndInvoke of
SoapHttpClientProtocol does exactly that, it takes a target, an async
callback and a object for my parameters.

Does anyone know how to implement async method call, w. callback in
NETCF? Maybe SoapHttpClientProtocol can help ;)

Greetings,

Sebastian mauer
 
S

Simon Hart [MVP]

I must say I have never used BeginInvoke, I usally create a worker thread
which calls Invoke to get back to the UI layer - this works for me. I have
however used the asynch support with Web Services on the CF 1.0 + 2.0 in the
past which allows you to pass over a delegate and I can confirm this does
work. Can you post some code? and where does the NotSupportedException occur
in this process? also what device are you running on?
 
D

dbgrick

Simon is correct. Try using a worker thread. Pass in an object that
contains pertinent information and create a completion event (Something like
OnDone) then wire up this event in your main thread, start the new thread.
You can continue to do work in your main thread and process the completion
event when done. You can also wire in some update events to your passed
object and update display as needed. Keep in mind that the event from the
worker thread will have to use control.Invoke to update display properties.

Here is a simple form with a label (lblMine) and a button (btnRun) the
button click starts a thread that updates the display after a 2 second delay.

public partial class ThreadForm : Form
{
ThreadWorker worker;

public ThreadForm()
{
worker = null;

InitializeComponent();
}

private void btnRun_Click(object sender, EventArgs e)
{
worker = new ThreadWorker();
worker.OnDone += new EventHandler(worker_OnDone);
ThreadStart ths = new ThreadStart(worker.Start);

Thread thread = new Thread(worker.Start);

thread.Start();
}

void worker_OnDone(object sender, EventArgs e)
{
EventHandler<MyEventArgs> evtHandler;

if (this.InvokeRequired)
{
evtHandler = new EventHandler<MyEventArgs>(UpdateText);
this.Invoke(evtHandler, new object[] { this, new
MyEventArgs("Thread Done") });
}
else
{
UpdateText(this, new MyEventArgs("Thread Done"));
}
}

void UpdateText(object sender, MyEventArgs outTxt)
{
lblMine.Text = outTxt.Text;
}
}

public class MyEventArgs : EventArgs
{
public string Text;

public MyEventArgs(string s)
{
Text = s;
}
}

public class ThreadWorker
{
public event EventHandler OnDone;

public ThreadWorker()
{
}

public void Start()
{
Thread.Sleep(2000);

if (OnDone != null)
{
OnDone(this, EventArgs.Empty);
}
}
}

I hope this helps.
 
D

David Hyde

Delegate.BeginInvoke() isn't supported on CF, but Control.BeginInvoke() is.
I derived my class from System.Windows.Forms.Control and used this
BeginInvoke() to call the delegate.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top