BeginInvoke call another function

L

LP

Hi,

I have a method that's called from another thread through a delegate. It's
checking if (InvokeRequired){} and does BeginInvoke() if true. So it will be
executed on the same UI thread. here's the code in question:

private void wrkP_OnProcessCompleted(WorkerProcessBalanceEventArgs e)
{
if (InvokeRequired)
{
BeginInvoke(new delegateUpdateProgress(wrkP_OnProcessCompleted), new
object[] {e});
return;
}
anotherPrivateFunction();
//more code here
}

Question do I need a delegate for anotherPrivateFunction(); or it will
be executed by UI thread anyway?
 
A

Ajay Kalra

anotherPrivateFunction is simply another call to the function. You dont
need any delegate declaration for it. If anotherPrivateFunction alters
controls on the form, it will/should do what you have done here, use
InvokeRequired.
 
L

LP

If anotherPrivateFunction alters controls on the form

Yes, it does.
InvokeRequired.

You are saying it will use Delegate or I need to program it?


I
 
A

Ajay Kalra

anotherPrivateFunction will look similar to wrkP_OnProcessCompleted.
You will check InvokeRequired and then you will instatiate a new
delegate object to call itself. Something like ths:

1. // Declare delegatAnotherPrivateFunction in your class

2. Function can be something like this:

private void anotherPrivateFunction(object obj)
{
if (InvokeRequired)
{
BeginInvoke(new
delegatAnotherPrivateFunction(anotherPrivateFunction), new ..);
return;
}
....
}
 
D

Daniel Pratt

Hi LP,

LP said:
Hi,

I have a method that's called from another thread through a delegate. It's
checking if (InvokeRequired){} and does BeginInvoke() if true. So it will
be
executed on the same UI thread. here's the code in question:

private void wrkP_OnProcessCompleted(WorkerProcessBalanceEventArgs e)
{
if (InvokeRequired)
{
BeginInvoke(new delegateUpdateProgress(wrkP_OnProcessCompleted), new
object[] {e});
return;
}
anotherPrivateFunction();
//more code here
}

Question do I need a delegate for anotherPrivateFunction(); or it will
be executed by UI thread anyway?

What you have will work fine. You do not need to create a separate
delegate for anotherPrivateFunction().

Regards,
Daniel
 

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