BeginInvoke call another function

  • Thread starter Thread starter LP
  • Start date Start date
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?
 
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.
 
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
 
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;
}
....
}
 
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
 
Back
Top