BeginInvoke, plz help

  • Thread starter Thread starter MakeMeHappy
  • Start date Start date
M

MakeMeHappy

Hi, I have some problems with understand how to call BeginInvoke properly so
I need your help...
I have a "worker" thread in my app where I download a website, and after
some processing on html code I want to fire event from the "worker" thread
(my EventArg is a object which describles some data extracted from html
code), whose body (event body) must update datagrid control (which should be
done in main application thread, I suppose), but I don't know how to fire
that event with BeginInvoke...
plz help
Regards!
(Sorry for my poor English...)
 
Maybe you could use the Control.Invoke method on your datagrid from the
worker thread.

Etienne Boucher
 
MakeMeHappy said:
Hi, I have some problems with understand how to call BeginInvoke properly so
I need your help...
I have a "worker" thread in my app where I download a website, and after
some processing on html code I want to fire event from the "worker" thread
(my EventArg is a object which describles some data extracted from html
code), whose body (event body) must update datagrid control (which should be
done in main application thread, I suppose), but I don't know how to fire
that event with BeginInvoke...

Have a look at
http://www.pobox.com/~skeet/csharp/multithreading.html#windows.forms
 
Here's a basic example:

// Basic asynchronous callback design

using System;
using System.Threading;

public class Class1
{
// create the delegate
private delegate void DoSomethingDelegate();
...
}

private void SomeMethod()
{
// instantiate the delegate
DoSomethingDelegate dlgt = new DoSomethingDelegate(LongRunningMethod);

// the method to call when the asynchronous method completes
ASyncCallback cb = new ASyncCallback(OnMethodComplete);

// start the asynchronous process; returns immediately
IAsyncResult ar = dlgt.BeginInvoke(cb, null);

// some code to update the UI
BeginUIUpdate();
}

// The method called when the
// asynchronous process completes
private void OnMethodComplete()
{
// do some UI update here...
}
}
 
Here's a basic example:

// Basic asynchronous callback design

using System;
using System.Threading;

public class Class1
{
// create the delegate
private delegate void DoSomethingDelegate();
...
}

private void SomeMethod()
{
// instantiate the delegate
DoSomethingDelegate dlgt = new DoSomethingDelegate(LongRunningMethod);

// the method to call when the asynchronous method completes
ASyncCallback cb = new ASyncCallback(OnMethodComplete);

// start the asynchronous process; returns immediately
IAsyncResult ar = dlgt.BeginInvoke(cb, null);

// some code to update the UI
BeginUIUpdate();
}

// The method called when the
// asynchronous process completes
private void OnMethodComplete()
{
// do some UI update here...
}
}

No, that doesn't work properly - using Delegate.BeginInvoke will mean
that OnMethodComplete is called in a thread pool thread, not the UI
thread, so you can't do any UI updating there. You need to call
Control.Invoke or Control.BeginInvoke.
 
This is the way it was done in a recent MS Webcast. Nothing was ever
mentioned about Control.BeginInvoke...?
 
This is the way it was done in a recent MS Webcast. Nothing was ever
mentioned about Control.BeginInvoke...?

Are you sure it was updating the UI? It's possible that the webcast was
wrong, of course...

The golden rule of Windows Forms threading is that you don't update the
UI from any thread other than the one which created the control you're
updating, and Delegate.BeginInvoke is going to execute on a thread pool
thread rather than the UI thread, definitely.

See the link I posted before for more information.
 

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

Back
Top