BeginInvoke equivalent in a class library

  • Thread starter Thread starter arkam
  • Start date Start date
A

arkam

Hi,

I found a sample on internet :

formMain.BeginInvoke(new MyClickHandler(this.OnMyClick));

I would like to do the same but in a class library where there is no forms !

Where can I find a BeginInvoke equivalent for a class library ?

Thanks

Arkam
 
arkam said:
I found a sample on internet :

formMain.BeginInvoke(new MyClickHandler(this.OnMyClick));

I would like to do the same but in a class library where there is no forms !

Where can I find a BeginInvoke equivalent for a class library ?

It doesn't always make sense to. With forms, there's always a thread
running as a message pump for a particular control - that doesn't exist
for most classes!

However, you can call BeginInvoke on a delegate to get it to run in a
ThreadPool thread.
 
Jon Skeet said:
It doesn't always make sense to. With forms, there's always a thread
running as a message pump for a particular control - that doesn't exist
for most classes!

However, you can call BeginInvoke on a delegate to get it to run in a
ThreadPool thread.

I already tried this but it did'nt work. Here is a little code sample
:

private delegate void CaptureDone();

private void MyFunction()
{
CaptureDone d = new CaptureDone(this.OnGrabberCaptureDone);
d.BeginInvoke(null, null);
}

This does not work like with BeginInvoke of a Control ! I compared the
two methods and it definitively only works when I use
Control.BeginInvoke !

In fact it works but in my case I can see that OnGrabberCaptureDone is
not called like with Control.BeginInvoke ! I can see this because I am
using locking functions on a resource and it should be released
befaore calling OnGrabberCaptureDone !
 
arkam said:
I already tried this but it did'nt work. Here is a little code sample
:

private delegate void CaptureDone();

private void MyFunction()
{
CaptureDone d = new CaptureDone(this.OnGrabberCaptureDone);
d.BeginInvoke(null, null);
}

This does not work like with BeginInvoke of a Control ! I compared the
two methods and it definitively only works when I use
Control.BeginInvoke !

What do you mean by "only works"? You certainly shouldn't use it for
GUI operations, as the delegate won't be invoked on the UI thread.
In fact it works but in my case I can see that OnGrabberCaptureDone is
not called like with Control.BeginInvoke ! I can see this because I am
using locking functions on a resource and it should be released
befaore calling OnGrabberCaptureDone !

Please describe what you want to happen and what's actually happening,
in plain terms which don't require any additional background knowledge
which you don't provide.
 
Jon Skeet said:
What do you mean by "only works"? You certainly shouldn't use it for
GUI operations, as the delegate won't be invoked on the UI thread.


Please describe what you want to happen and what's actually happening,
in plain terms which don't require any additional background knowledge
which you don't provide.

Ok,

I have downloaded a code sample which uses Control.BeginInvoke to
start another function asynchronously. I suspect the calling function
to have to terminate before it releases some resource. The
asynchronous function can then use this resource.

The code sample uses Control.BeginInvoke and it works.
I tried to use delegate.BeginInvoke and it does not work like
Control.BeginInvoke because I get an error in the called function
saying the resource is still locked.

My sources are :
http://www.codeproject.com/cs/media/directshownet.asp
The code is in the SampleGrabber example.

My code is at:
http://www.sourceforge.net/projects/openmc

But just to know, is :?
 
arkam said:
I have downloaded a code sample which uses Control.BeginInvoke to
start another function asynchronously. I suspect the calling function
to have to terminate before it releases some resource. The
asynchronous function can then use this resource.

So how are you trying to ensure that the calling function terminates
first?
But just to know, is :
?

Well it's not the equivalent in that all calls to BeginInvoke on a
control will end up being handled by a single thread, eventually -
whereas if you call delegate.BeginInvoke twice, those two calls could
occur at the same time, because they are serviced by ThreadPool
threads.
 
Jon Skeet said:
So how are you trying to ensure that the calling function terminates
first?

I simply don't ! It is just that the sample I downloaded works !
Well it's not the equivalent in that all calls to BeginInvoke on a
control will end up being handled by a single thread, eventually -
whereas if you call delegate.BeginInvoke twice, those two calls could
occur at the same time, because they are serviced by ThreadPool
threads.

Thanks for the precision !
 
Back
Top