BeginInvoke, plz help
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

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...)

Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
MakeMeHappy <Click here to reveal e-mail address> wrote:
[Original message clipped]

Have a look at
http://www.pobox.com/~skeet/csharp/multithreading.html#windows.forms

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
    
FusionGuy
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...
}
}

"Jon Skeet [C# MVP]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
<"FusionGuy" <sbearusa32 at em s n dot com (that's msn.com)>> wrote:
[Original message clipped]

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.

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
    
FusionGuy
This is the way it was done in a recent MS Webcast. Nothing was ever
mentioned about Control.BeginInvoke...?

"Jon Skeet [C# MVP]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
<"FusionGuy" <sbearusa32 at em s n dot com (that's msn.com)>> wrote:
[Original message clipped]

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.

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
    
Etienne Boucher
Maybe you could use the Control.Invoke method on your datagrid from the
worker thread.

Etienne Boucher

"MakeMeHappy" <Click here to reveal e-mail address> a écrit dans le message de
news:Click here to reveal e-mail address...
> Hi, I have some problems with understand how to call BeginInvoke properly
so
[Original message clipped]

Reply to this message...
 
 
System.Delegate
System.IAsyncResult
System.Web.UI.Control




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com