wspencers

Could someone tells me what is the problem with this codes. The SetProgressBarMethod works fine with background worker and others but not with this. Thanks

private void button3_Click(object sender, EventArgs e)

{

TimeConsuming2Delegate del2 = new TimeConsuming2Delegate(TimeConsuming2Method);

IAsyncResult result;

result = del2.BeginInvoke(7, null, null);

string resultString = del2.EndInvoke(result);

sample3Label.Text = resultString;

}

delegate string TimeConsuming2Delegate(int waktu);

string TimeConsuming2Method(int waktu)

{

int duration = (int)waktu;

for (int i = 0; i < duration; i++)

{

System.Threading.Thread.Sleep(1000);

double ii = (double)i + 1;

double hasil = ii / (double)duration * 100;

SetProgressbarMethod((int)hasil, progressBar3); //changing the value of a progressBar

}

return "Successful";

}

delegate void setProgressbarDelegate(int persen, ProgressBar aProgressBar);

void SetProgressbarMethod(int persen, ProgressBar aProgressBar)

{

ProgressBar myBar = (ProgressBar)aProgressBar;

if (myBar.InvokeRequired)

{

setProgressbarDelegate del = new setProgressbarDelegate(SetProgressbarMethod);

this.Invoke(del, new object[] { persen, aProgressBar }); ---->> the application freezes at this point

}

else

myBar.Value = persen;

}




Re: Visual C# General Asynchronous with endInvoke problem

Peter Ritchie

Looks like it should be this.Invoke(del, new object[] {persen, aProgressBar) by the looks of it. If myBar.InvokeRequired is true that means you're running on a background thread; if that background thread is a thread pool thread (e.g. invoked by an asynchronous delegate) your call this.Invoke(dell, new object[] {persen}); will throw an exception that will just be lost because exceptions from thread pool threads are swallowed.




Re: Visual C# General Asynchronous with endInvoke problem

wspencers

Thanks for the reply Peter, appreciate it!

I am trying to use all threading methods in conjunction with the ProgressBar,

When I eliminate all codes after activating a BeginInvoke method, it works fine.

It works fine also with callbackMethod.

Can I assume that I simply cannot use the progress bar using this method Where should I put the EndInvoke method

Thanks






Re: Visual C# General Asynchronous with endInvoke problem

Peter Ritchie

wspencers wrote:

Thanks for the reply Peter, appreciate it!

I am trying to use all threading methods in conjunction with the ProgressBar,

When I eliminate all codes after activating a BeginInvoke method, it works fine.

It works fine also with callbackMethod.

Can I assume that I simply cannot use the progress bar using this method Where should I put the EndInvoke method

Thanks

Actually, looking at your code more closely, it appears that you're blocking the GUI thread on the EndInvoke. This means that Control.Invoke will block until the GUI thread is available again--which it won't.

There's really no point of having an asynchronous delegate (your first BeginInvoke) if you have an EndInvoke right after it.