473,840 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delegates and Worker Threads, Episode II

Re: Original post = Windows forms - how do I get them to render/update
properly? from August 22.

Okay I am making some progress with being able to use delegates to run
my shelled processes on worker threads. Yes I have gotten it to work,
kind-of - that is I have gotten the shelled processes off of the UI
thread (I think?). But the UI still is not updating properly! Still I
have white boxes for forms, label text that does not update, and extra
windows in the taskbar.

Also I am not sure about the shelled apps themselves - they do not
appear to be multi-threaded because they essentially "take over" the
system - it does not respond at all - for short periods of time.

Anyways something is still preventing the UI in my app from updating
properly. What in the world am I doing wrong here?

Below is some code from the app. I have shortened a lot of stuff to
make it more readable...

....

//part I - declare delegates
public class _SomeClass : System.Windows. Forms.Form
{
private delegate bool _CCDelegate();
private delegate bool _URTDelegate();

[other code below...]

....

//part II - instantiate and invoke

[...other code above]

//check communications
this.lblStatus. Text = "Status: Checking communications, please
wait...";
_CCDelegate CCDelegate = new _CCDelegate(CC) ;
IAsyncResult CCResult = CCDelegate.Begi nInvoke(null, null);
this.Activate() ; //try to force form to update
this.lblStatus. Refresh(); //try to force form to update
this.Refresh(); //try to force form to update
//all or part of form remains white and not updated
if(!(CCDelegate .EndInvoke(CCRe sult)))
{
this.lblStatus. Text = "Status: Check failed.";
Environment.Exi t(-1);
}
this.lblStatus. Text = "Status: Check was successful.";

//Upload report totals
this.lblStatus. Text = "Status: Uploading report totals, please
wait...";
_URTDelegate URTDelegate = new _URTDelegate(UR T);
IAsyncResult URTResult = URTDelegate.Beg inInvoke(null, null);
this.Activate() ; //try to force form to update
this.lblStatus. Refresh(); //try to force form to update
this.Refresh(); //try to force form to update
//all or part of form remains white and not updated
if(!(URTDelegat e.EndInvoke(URT Result)))
{
this.lblStatus. Text = "Status: Upload of report totals failed.";
Environment.Exi t(-1);
}
this.lblStatus. Text = "Status: Upload of report totals was
successful.";

[other code below...]

....

//part III - functions

//this entire function should now be on a worker thread, right?
//this entire function should now be separated from the UI thread,
right?
public bool CC()
{
try
{
bool StatusFlag = false;

[...code to launch task on Process class goes here...]
//careful to include no interaction with UI

return StatusFlag;
}
catch(Exception x)
{
MessageBox.Show ("FATAL ERROR - " + x.Message);
bool StatusFlag = false;
return StatusFlag;
}
}

//this entire function should now be on a worker thread, right?
//this entire function should now be separated from the UI thread,
right?
public bool URT()
{
try
{
bool StatusFlag = false;

[...code to launch task on Process class goes here...]
//careful to include no interaction with UI

return StatusFlag;
}
catch(Exception x)
{
MessageBox.Show ("FATAL ERROR - " + x.Message);
bool StatusFlag = false;
return StatusFlag;
}
}

....

Okay now I understand that I am using asynchonous processing here, yet
I am blocking with EndInvoke...but I have to make this stuff process
synchonously. It is very important that one process finishes before
the next one begins. If there is a better way then will someone please
post the code?

At this point all I need is:

1. The ability to get processes off of the UI thread so that the UI
continues to update properly.

2. The ability to run those processes syncronously.

I would really appreciate any help from any of you as I really want to
be able to make this work.

Thanks.
Nov 15 '05 #1
12 2169
Joey,

When you say
[...code to launch task on Process class goes here...]
in your delegates, do you mean you are actually launching a process and
perhaps monitoring it, via System.Diagnost ics.Process? If so, you can start
a process, set its .ExitCode += new EventHandler(Ca llbackFuncHere) ; and then
set its .EnableRaisingE vents = true; Thus you will get a callback when the
process completes (wheter good or bad) from that callback you can chain more
process starts with new callback handlers to "serialize" the whole flow.

My guess is that your delegates take some time to execute. When you do the
following:
IAsyncResult CCResult = CCDelegate.Begi nInvoke(null, null);
this.Activate() ; //try to force form to update
this.lblStatus. Refresh(); //try to force form to update
this.Refresh(); //try to force form to update
//all or part of form remains white and not updated
if(!(CCDelegate .EndInvoke(CCRe sult)))
My theory is that these guys:
this.Activate() ; //try to force form to update
this.lblStatus. Refresh(); //try to force form to update
this.Refresh(); //try to force form to update


all do their magic via a message posted to the main thread. But before you
relinquish program control (to the internal message loop) you hit:
if(!(CCDelegate .EndInvoke(CCRe sult)))


Which blocks... And viola, its just like you did a plain old invoke!

If you are not using System.Diagnost ics.Process to actually create other
processes in the delegates, perhaps you should pass the BeginInvoke a
"callback" delegate. From within this callback you do what amounts to a
non-blocking EndInvoke() and proceed to issue the next BeginInvoke with a
new callback, and so on till everything is serialized to your liking but the
main thread is basically sitting around in its message loop!
Hope this helps.

Regards,
Erin.


Nov 15 '05 #2
Guys, I have read all of the links. I have tried the suggested
options, though I am not sure about the code one would use to
implement either. I have successfully created and used delegates (as
shown in the posted code). However, the UI ***still does not
update***. I have been stuck on this for a few months now. Can anyone
out there offer a simple solution to make this work? Specifically, how
could one modify the posted code to make it update the UI properly? I
am running out of options here...I guess I don't "get it". This
continues to be extremely frustrating, but I am not going to give
up...

Thanks,
JP

"Joe Mayo" <jm***@ddiieess ppaammeerrssddi iee.com> wrote in message news:<Og******* *******@TK2MSFT NGP12.phx.gbl>. ..
Hi Joey,

Here are some articles on threading that should help you get up to speed.

http://www.windowsforms.net/Default....d=40#Threading

Remember to use Control.Invoke/BeginInvoke, to call a method on your main UI
thread that updates your UI. Also, if you use BeginInvoke, be sure to call
EndInvoke to avoid a memory leak.

Also, check out the asynchronous delegate pattern that uses the
AsyncCallback parameter of BeginInvoke. I may be mistaken, but I think part
of your UI updating problem may be that you are calling EndInvoke in the
same method as BeginInvoke and blocking on your main UI thread. Here's a
reference to the async delegate patterns:

http://msdn.microsoft.com/library/de...rnoverview.asp

Look at the example that says "Supply the callback delegate when beginning
the asynchronous call."

Joe
--
http://www.csharp-station.com
"Joey Powell" <jo*********@go ldcoinc.com> wrote in message
news:bd******** *************** **@posting.goog le.com...
Re: Original post = Windows forms - how do I get them to render/update
properly? from August 22.

Okay I am making some progress with being able to use delegates to run
my shelled processes on worker threads. Yes I have gotten it to work,
kind-of - that is I have gotten the shelled processes off of the UI
thread (I think?). But the UI still is not updating properly! Still I
have white boxes for forms, label text that does not update, and extra
windows in the taskbar.

Also I am not sure about the shelled apps themselves - they do not
appear to be multi-threaded because they essentially "take over" the
system - it does not respond at all - for short periods of time.

Anyways something is still preventing the UI in my app from updating
properly. What in the world am I doing wrong here?

Below is some code from the app. I have shortened a lot of stuff to
make it more readable...

...

//part I - declare delegates
public class _SomeClass : System.Windows. Forms.Form
{
private delegate bool _CCDelegate();
private delegate bool _URTDelegate();

[other code below...]

...

//part II - instantiate and invoke

[...other code above]

//check communications
this.lblStatus. Text = "Status: Checking communications, please
wait...";
_CCDelegate CCDelegate = new _CCDelegate(CC) ;
IAsyncResult CCResult = CCDelegate.Begi nInvoke(null, null);
this.Activate() ; //try to force form to update
this.lblStatus. Refresh(); //try to force form to update
this.Refresh(); //try to force form to update
//all or part of form remains white and not updated
if(!(CCDelegate .EndInvoke(CCRe sult)))
{
this.lblStatus. Text = "Status: Check failed.";
Environment.Exi t(-1);
}
this.lblStatus. Text = "Status: Check was successful.";

//Upload report totals
this.lblStatus. Text = "Status: Uploading report totals, please
wait...";
_URTDelegate URTDelegate = new _URTDelegate(UR T);
IAsyncResult URTResult = URTDelegate.Beg inInvoke(null, null);
this.Activate() ; //try to force form to update
this.lblStatus. Refresh(); //try to force form to update
this.Refresh(); //try to force form to update
//all or part of form remains white and not updated
if(!(URTDelegat e.EndInvoke(URT Result)))
{
this.lblStatus. Text = "Status: Upload of report totals failed.";
Environment.Exi t(-1);
}
this.lblStatus. Text = "Status: Upload of report totals was
successful.";

[other code below...]

...

//part III - functions

//this entire function should now be on a worker thread, right?
//this entire function should now be separated from the UI thread,
right?
public bool CC()
{
try
{
bool StatusFlag = false;

[...code to launch task on Process class goes here...]
//careful to include no interaction with UI

return StatusFlag;
}
catch(Exception x)
{
MessageBox.Show ("FATAL ERROR - " + x.Message);
bool StatusFlag = false;
return StatusFlag;
}
}

//this entire function should now be on a worker thread, right?
//this entire function should now be separated from the UI thread,
right?
public bool URT()
{
try
{
bool StatusFlag = false;

[...code to launch task on Process class goes here...]
//careful to include no interaction with UI

return StatusFlag;
}
catch(Exception x)
{
MessageBox.Show ("FATAL ERROR - " + x.Message);
bool StatusFlag = false;
return StatusFlag;
}
}

...

Okay now I understand that I am using asynchonous processing here, yet
I am blocking with EndInvoke...but I have to make this stuff process
synchonously. It is very important that one process finishes before
the next one begins. If there is a better way then will someone please
post the code?

At this point all I need is:

1. The ability to get processes off of the UI thread so that the UI
continues to update properly.

2. The ability to run those processes syncronously.

I would really appreciate any help from any of you as I really want to
be able to make this work.

Thanks.

Nov 15 '05 #3
Can you provide a short and complete prorgram - that compiles - and then
I'll have a look at it (and I'm bet some other people will as well). It's
sometimes hard to piece together exactly what is happenning form small code
fragments (especially when >> is in front of every line).

See Jon Skeet's page on short but complete:
http://www.pobox.com/~skeet/csharp/complete.html

--
Mike Mayer
http://www.mag37.com/csharp/
mi**@mag37.com
"Joey Powell" <jo*********@go ldcoinc.com> wrote in message
news:bd******** *************** ***@posting.goo gle.com...
Guys, I have read all of the links. I have tried the suggested
options, though I am not sure about the code one would use to
implement either. I have successfully created and used delegates (as
shown in the posted code). However, the UI ***still does not
update***. I have been stuck on this for a few months now. Can anyone
out there offer a simple solution to make this work? Specifically, how
could one modify the posted code to make it update the UI properly? I
am running out of options here...I guess I don't "get it". This
continues to be extremely frustrating, but I am not going to give
up...

Thanks,
JP

"Joe Mayo" <jm***@ddiieess ppaammeerrssddi iee.com> wrote in message

news:<Og******* *******@TK2MSFT NGP12.phx.gbl>. ..
Hi Joey,

Here are some articles on threading that should help you get up to speed.
http://www.windowsforms.net/Default....d=40#Threading

Remember to use Control.Invoke/BeginInvoke, to call a method on your main UI thread that updates your UI. Also, if you use BeginInvoke, be sure to call EndInvoke to avoid a memory leak.

Also, check out the asynchronous delegate pattern that uses the
AsyncCallback parameter of BeginInvoke. I may be mistaken, but I think part of your UI updating problem may be that you are calling EndInvoke in the
same method as BeginInvoke and blocking on your main UI thread. Here's a reference to the async delegate patterns:

http://msdn.microsoft.com/library/de...rnoverview.asp
Look at the example that says "Supply the callback delegate when beginning the asynchronous call."

Joe
--
http://www.csharp-station.com
"Joey Powell" <jo*********@go ldcoinc.com> wrote in message
news:bd******** *************** **@posting.goog le.com...
Re: Original post = Windows forms - how do I get them to render/update
properly? from August 22.

Okay I am making some progress with being able to use delegates to run
my shelled processes on worker threads. Yes I have gotten it to work,
kind-of - that is I have gotten the shelled processes off of the UI
thread (I think?). But the UI still is not updating properly! Still I
have white boxes for forms, label text that does not update, and extra
windows in the taskbar.

Also I am not sure about the shelled apps themselves - they do not
appear to be multi-threaded because they essentially "take over" the
system - it does not respond at all - for short periods of time.

Anyways something is still preventing the UI in my app from updating
properly. What in the world am I doing wrong here?

Below is some code from the app. I have shortened a lot of stuff to
make it more readable...

...

//part I - declare delegates
public class _SomeClass : System.Windows. Forms.Form
{
private delegate bool _CCDelegate();
private delegate bool _URTDelegate();

[other code below...]

...

//part II - instantiate and invoke

[...other code above]

//check communications
this.lblStatus. Text = "Status: Checking communications, please
wait...";
_CCDelegate CCDelegate = new _CCDelegate(CC) ;
IAsyncResult CCResult = CCDelegate.Begi nInvoke(null, null);
this.Activate() ; //try to force form to update
this.lblStatus. Refresh(); //try to force form to update
this.Refresh(); //try to force form to update
//all or part of form remains white and not updated
if(!(CCDelegate .EndInvoke(CCRe sult)))
{
this.lblStatus. Text = "Status: Check failed.";
Environment.Exi t(-1);
}
this.lblStatus. Text = "Status: Check was successful.";

//Upload report totals
this.lblStatus. Text = "Status: Uploading report totals, please
wait...";
_URTDelegate URTDelegate = new _URTDelegate(UR T);
IAsyncResult URTResult = URTDelegate.Beg inInvoke(null, null);
this.Activate() ; //try to force form to update
this.lblStatus. Refresh(); //try to force form to update
this.Refresh(); //try to force form to update
//all or part of form remains white and not updated
if(!(URTDelegat e.EndInvoke(URT Result)))
{
this.lblStatus. Text = "Status: Upload of report totals failed.";
Environment.Exi t(-1);
}
this.lblStatus. Text = "Status: Upload of report totals was
successful.";

[other code below...]

...

//part III - functions

//this entire function should now be on a worker thread, right?
//this entire function should now be separated from the UI thread,
right?
public bool CC()
{
try
{
bool StatusFlag = false;

[...code to launch task on Process class goes here...]
//careful to include no interaction with UI

return StatusFlag;
}
catch(Exception x)
{
MessageBox.Show ("FATAL ERROR - " + x.Message);
bool StatusFlag = false;
return StatusFlag;
}
}

//this entire function should now be on a worker thread, right?
//this entire function should now be separated from the UI thread,
right?
public bool URT()
{
try
{
bool StatusFlag = false;

[...code to launch task on Process class goes here...]
//careful to include no interaction with UI

return StatusFlag;
}
catch(Exception x)
{
MessageBox.Show ("FATAL ERROR - " + x.Message);
bool StatusFlag = false;
return StatusFlag;
}
}

...

Okay now I understand that I am using asynchonous processing here, yet
I am blocking with EndInvoke...but I have to make this stuff process
synchonously. It is very important that one process finishes before
the next one begins. If there is a better way then will someone please
post the code?

At this point all I need is:

1. The ability to get processes off of the UI thread so that the UI
continues to update properly.

2. The ability to run those processes syncronously.

I would really appreciate any help from any of you as I really want to
be able to make this work.

Thanks.

Nov 15 '05 #4
Sorry, the URL was incorrect. This one is right...

http://www.goldcoinc.com/temp/POST543Report.cs_

"Michael Mayer" <mi**@mag37.com > wrote in message news:<eC******* *******@tk2msft ngp13.phx.gbl>. ..
Can you provide a short and complete prorgram - that compiles - and then
I'll have a look at it (and I'm bet some other people will as well). It's
sometimes hard to piece together exactly what is happenning form small code
fragments (especially when >> is in front of every line).

See Jon Skeet's page on short but complete:
http://www.pobox.com/~skeet/csharp/complete.html

--
Mike Mayer
http://www.mag37.com/csharp/
mi**@mag37.com
"Joey Powell" <jo*********@go ldcoinc.com> wrote in message
news:bd******** *************** ***@posting.goo gle.com...
Guys, I have read all of the links. I have tried the suggested
options, though I am not sure about the code one would use to
implement either. I have successfully created and used delegates (as
shown in the posted code). However, the UI ***still does not
update***. I have been stuck on this for a few months now. Can anyone
out there offer a simple solution to make this work? Specifically, how
could one modify the posted code to make it update the UI properly? I
am running out of options here...I guess I don't "get it". This
continues to be extremely frustrating, but I am not going to give
up...

Thanks,
JP

"Joe Mayo" <jm***@ddiieess ppaammeerrssddi iee.com> wrote in message

news:<Og******* *******@TK2MSFT NGP12.phx.gbl>. ..
Hi Joey,

Here are some articles on threading that should help you get up to speed.
http://www.windowsforms.net/Default....d=40#Threading

Remember to use Control.Invoke/BeginInvoke, to call a method on your main UI thread that updates your UI. Also, if you use BeginInvoke, be sure to call EndInvoke to avoid a memory leak.

Also, check out the asynchronous delegate pattern that uses the
AsyncCallback parameter of BeginInvoke. I may be mistaken, but I think part of your UI updating problem may be that you are calling EndInvoke in the
same method as BeginInvoke and blocking on your main UI thread. Here's a reference to the async delegate patterns:

http://msdn.microsoft.com/library/de...rnoverview.asp
Look at the example that says "Supply the callback delegate when beginning the asynchronous call."

Joe
--
http://www.csharp-station.com
"Joey Powell" <jo*********@go ldcoinc.com> wrote in message
news:bd******** *************** **@posting.goog le.com...
> Re: Original post = Windows forms - how do I get them to render/update
> properly? from August 22.
>
> Okay I am making some progress with being able to use delegates to run
> my shelled processes on worker threads. Yes I have gotten it to work,
> kind-of - that is I have gotten the shelled processes off of the UI
> thread (I think?). But the UI still is not updating properly! Still I
> have white boxes for forms, label text that does not update, and extra
> windows in the taskbar.
>
> Also I am not sure about the shelled apps themselves - they do not
> appear to be multi-threaded because they essentially "take over" the
> system - it does not respond at all - for short periods of time.
>
> Anyways something is still preventing the UI in my app from updating
> properly. What in the world am I doing wrong here?
>
> Below is some code from the app. I have shortened a lot of stuff to
> make it more readable...
>
> ...
>
> //part I - declare delegates
> public class _SomeClass : System.Windows. Forms.Form
> {
> private delegate bool _CCDelegate();
> private delegate bool _URTDelegate();
>
> [other code below...]
>
> ...
>
> //part II - instantiate and invoke
>
> [...other code above]
>
> //check communications
> this.lblStatus. Text = "Status: Checking communications, please
> wait...";
> _CCDelegate CCDelegate = new _CCDelegate(CC) ;
> IAsyncResult CCResult = CCDelegate.Begi nInvoke(null, null);
> this.Activate() ; //try to force form to update
> this.lblStatus. Refresh(); //try to force form to update
> this.Refresh(); //try to force form to update
> //all or part of form remains white and not updated
> if(!(CCDelegate .EndInvoke(CCRe sult)))
> {
> this.lblStatus. Text = "Status: Check failed.";
> Environment.Exi t(-1);
> }
> this.lblStatus. Text = "Status: Check was successful.";
>
> //Upload report totals
> this.lblStatus. Text = "Status: Uploading report totals, please
> wait...";
> _URTDelegate URTDelegate = new _URTDelegate(UR T);
> IAsyncResult URTResult = URTDelegate.Beg inInvoke(null, null);
> this.Activate() ; //try to force form to update
> this.lblStatus. Refresh(); //try to force form to update
> this.Refresh(); //try to force form to update
> //all or part of form remains white and not updated
> if(!(URTDelegat e.EndInvoke(URT Result)))
> {
> this.lblStatus. Text = "Status: Upload of report totals failed.";
> Environment.Exi t(-1);
> }
> this.lblStatus. Text = "Status: Upload of report totals was
> successful.";
>
> [other code below...]
>
> ...
>
> //part III - functions
>
> //this entire function should now be on a worker thread, right?
> //this entire function should now be separated from the UI thread,
> right?
> public bool CC()
> {
> try
> {
> bool StatusFlag = false;
>
> [...code to launch task on Process class goes here...]
> //careful to include no interaction with UI
>
> return StatusFlag;
> }
> catch(Exception x)
> {
> MessageBox.Show ("FATAL ERROR - " + x.Message);
> bool StatusFlag = false;
> return StatusFlag;
> }
> }
>
> //this entire function should now be on a worker thread, right?
> //this entire function should now be separated from the UI thread,
> right?
> public bool URT()
> {
> try
> {
> bool StatusFlag = false;
>
> [...code to launch task on Process class goes here...]
> //careful to include no interaction with UI
>
> return StatusFlag;
> }
> catch(Exception x)
> {
> MessageBox.Show ("FATAL ERROR - " + x.Message);
> bool StatusFlag = false;
> return StatusFlag;
> }
> }
>
> ...
>
> Okay now I understand that I am using asynchonous processing here, yet
> I am blocking with EndInvoke...but I have to make this stuff process
> synchonously. It is very important that one process finishes before
> the next one begins. If there is a better way then will someone please
> post the code?
>
> At this point all I need is:
>
> 1. The ability to get processes off of the UI thread so that the UI
> continues to update properly.
>
> 2. The ability to run those processes syncronously.
>
> I would really appreciate any help from any of you as I really want to
> be able to make this work.
>
> Thanks.

Nov 15 '05 #5
Calling EndInvoke stops the asynchronous processing and blocks until the
function returns. You do not need to call EndInvoke in your class at all. If
you want notification that the process has finsihed, then pass in an
AsyncCallback to the BeginInvoke method. When the process finishes, the
callback you passed in will be called.

If you want to have a progress report at certain intervals then you have a
little more coding to do: it boils down to checking the state of the object
you are processing at a certain interval.

Hope this helps. I will try to reply later with a full example.

Josh
"Joey Powell" <jo*********@go ldcoinc.com> wrote in message
news:bd******** *************** ***@posting.goo gle.com...
Guys, I have read all of the links. I have tried the suggested
options, though I am not sure about the code one would use to
implement either. I have successfully created and used delegates (as
shown in the posted code). However, the UI ***still does not
update***. I have been stuck on this for a few months now. Can anyone
out there offer a simple solution to make this work? Specifically, how
could one modify the posted code to make it update the UI properly? I
am running out of options here...I guess I don't "get it". This
continues to be extremely frustrating, but I am not going to give
up...

Thanks,
JP

"Joe Mayo" <jm***@ddiieess ppaammeerrssddi iee.com> wrote in message

news:<Og******* *******@TK2MSFT NGP12.phx.gbl>. ..
Hi Joey,

Here are some articles on threading that should help you get up to speed.
http://www.windowsforms.net/Default....d=40#Threading

Remember to use Control.Invoke/BeginInvoke, to call a method on your main UI thread that updates your UI. Also, if you use BeginInvoke, be sure to call EndInvoke to avoid a memory leak.

Also, check out the asynchronous delegate pattern that uses the
AsyncCallback parameter of BeginInvoke. I may be mistaken, but I think part of your UI updating problem may be that you are calling EndInvoke in the
same method as BeginInvoke and blocking on your main UI thread. Here's a reference to the async delegate patterns:

http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconasynchrono usdesignpattern overview.asp
Look at the example that says "Supply the callback delegate when beginning the asynchronous call."

Joe
--
http://www.csharp-station.com
"Joey Powell" <jo*********@go ldcoinc.com> wrote in message
news:bd******** *************** **@posting.goog le.com...
Re: Original post = Windows forms - how do I get them to render/update
properly? from August 22.

Okay I am making some progress with being able to use delegates to run
my shelled processes on worker threads. Yes I have gotten it to work,
kind-of - that is I have gotten the shelled processes off of the UI
thread (I think?). But the UI still is not updating properly! Still I
have white boxes for forms, label text that does not update, and extra
windows in the taskbar.

Also I am not sure about the shelled apps themselves - they do not
appear to be multi-threaded because they essentially "take over" the
system - it does not respond at all - for short periods of time.

Anyways something is still preventing the UI in my app from updating
properly. What in the world am I doing wrong here?

Below is some code from the app. I have shortened a lot of stuff to
make it more readable...

...

//part I - declare delegates
public class _SomeClass : System.Windows. Forms.Form
{
private delegate bool _CCDelegate();
private delegate bool _URTDelegate();

[other code below...]

...

//part II - instantiate and invoke

[...other code above]

//check communications
this.lblStatus. Text = "Status: Checking communications, please
wait...";
_CCDelegate CCDelegate = new _CCDelegate(CC) ;
IAsyncResult CCResult = CCDelegate.Begi nInvoke(null, null);
this.Activate() ; //try to force form to update
this.lblStatus. Refresh(); //try to force form to update
this.Refresh(); //try to force form to update
//all or part of form remains white and not updated
if(!(CCDelegate .EndInvoke(CCRe sult)))
{
this.lblStatus. Text = "Status: Check failed.";
Environment.Exi t(-1);
}
this.lblStatus. Text = "Status: Check was successful.";

//Upload report totals
this.lblStatus. Text = "Status: Uploading report totals, please
wait...";
_URTDelegate URTDelegate = new _URTDelegate(UR T);
IAsyncResult URTResult = URTDelegate.Beg inInvoke(null, null);
this.Activate() ; //try to force form to update
this.lblStatus. Refresh(); //try to force form to update
this.Refresh(); //try to force form to update
//all or part of form remains white and not updated
if(!(URTDelegat e.EndInvoke(URT Result)))
{
this.lblStatus. Text = "Status: Upload of report totals failed.";
Environment.Exi t(-1);
}
this.lblStatus. Text = "Status: Upload of report totals was
successful.";

[other code below...]

...

//part III - functions

//this entire function should now be on a worker thread, right?
//this entire function should now be separated from the UI thread,
right?
public bool CC()
{
try
{
bool StatusFlag = false;

[...code to launch task on Process class goes here...]
//careful to include no interaction with UI

return StatusFlag;
}
catch(Exception x)
{
MessageBox.Show ("FATAL ERROR - " + x.Message);
bool StatusFlag = false;
return StatusFlag;
}
}

//this entire function should now be on a worker thread, right?
//this entire function should now be separated from the UI thread,
right?
public bool URT()
{
try
{
bool StatusFlag = false;

[...code to launch task on Process class goes here...]
//careful to include no interaction with UI

return StatusFlag;
}
catch(Exception x)
{
MessageBox.Show ("FATAL ERROR - " + x.Message);
bool StatusFlag = false;
return StatusFlag;
}
}

...

Okay now I understand that I am using asynchonous processing here, yet
I am blocking with EndInvoke...but I have to make this stuff process
synchonously. It is very important that one process finishes before
the next one begins. If there is a better way then will someone please
post the code?

At this point all I need is:

1. The ability to get processes off of the UI thread so that the UI
continues to update properly.

2. The ability to run those processes syncronously.

I would really appreciate any help from any of you as I really want to
be able to make this work.

Thanks.

Nov 15 '05 #6
Joshua Coady wrote:
Calling EndInvoke stops the asynchronous processing and blocks until
the function returns. You do not need to call EndInvoke in your class
at all. If you want notification that the process has finsihed, then
pass in an AsyncCallback to the BeginInvoke method. When the process
finishes, the callback you passed in will be called.


I think not calling EndInvoke (or any of the other EndXXX methods) is a bad
idea, as this will silently swallow exceptions.

Regards,

Andreas

Nov 15 '05 #7
Joshua Coady wrote:
|| Calling EndInvoke stops the asynchronous processing and blocks until
|| the function returns. You do not need to call EndInvoke in your
|| class at all. If you want notification that the process has
|| finsihed, then pass in an AsyncCallback to the BeginInvoke method.
|| When the process finishes, the callback you passed in will be called.
||
|| If you want to have a progress report at certain intervals then you
|| have a little more coding to do: it boils down to checking the state
|| of the object you are processing at a certain interval.
||
|| Hope this helps. I will try to reply later with a full example.
||
|| Josh
||
||

With *delegates* you are required to pair each BeginInvoke with an EndInvoke, it's only Control.BeginIn voke which doesn't require a
call to EndInvoke.

Willy.

..
Nov 15 '05 #8
Joshua,
You do not need to call EndInvoke in your class at all.


Actually, afaik, you are required to pair each BeginInvoke with an EndInvoke.
--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com
Nov 15 '05 #9

"Joshua Coady" <jo**@coady.u s> wrote in message
news:eW******** ******@TK2MSFTN GP11.phx.gbl...
How is that requirement enforced? Or will bad things just happen if I dont? (I'm interested not sarcastic: I'm working on a project that calls
BeginInvoke a lot, but seldomly calls EndInvoke.)
It isn't enforced and that's part of the problem. There are many classes
that use the EndInvoke as the equivalent of a call to Dispose, and they use
this call to cleanup. If you don't call EndInvoke then you may get memory
leaks, files not getting closed, transport connections not cleaned up, etc.

Control.Invoke is an exception to this - the .net team at this point has
made assiurances that it is safe to not call EndInvoke on a control.

For example, see the code I posted to this thread. I call begin on the
delegate but not end and it compiles and runs as expected.

I've included the text of that post below so you dont have to go find it.

Josh

-------------------------

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2199
by: Coot | last post by:
I've been running many tests of the Asynchronous Delegate technique and I find that although BeginInvoke() does queue the method delegate onto a worker thread, it always does so on the _same_ thread. So if I call BeginInvoke() three times in a row, the method delegates are queued to the same thread and the second method doesn't begin until the first method completes, and the third doesn't begin until the second completes. This occurs...
2
6555
by: Steve Lutz | last post by:
Hi, I have a simple question that I cannot seem to find the answer to. Let's say my application has 2 threads, a Main thread and a Worker thread. As the worker thread progresses, it send results back to the main thread via a delegate. What thread does the delegate run in?
1
2848
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from remote server and display this info on the UI. From doing some research, I know that the way my implementation works today is not thread-safe, because essentially my worker thread updates the UI, which is BAD. So, here I am trying to figure out how...
12
3179
by: Grant | last post by:
I am having great difficulty understanding this and any code samples I find online are kilometres long and complicated to understand...Please could someone give me a simple exampe of how to get a delegate passed to my worked class so that my main form can update its progress bar? I know it has something to do with begininvoke and endinvoke etc, but I just need a simple sample to understand the convention. Code from my main form:...
3
4514
by: Oscar Thornell | last post by:
Hi, I am thinking about doing all my logging asynchronously using a delegate. The main resaon for this would be performance and responsiveness of the application (an ASP.NET app). //Exampel delegate void LogDelegate(string message); LogDelegate logger = new LogDelegate(Log.Debug); logger.DynamicInvoke("Some message..");
1
1043
by: Howard Weiss | last post by:
I am writing a Managed C++ image processing application which consists of a main window and a number of image processing windows. The main window is a Windows Forms and each image window is an instance of a different Windows Form. The image windows are created by creating New instances of the image form. The image forms become visible when I invoke show method. Initially, I wrote my application as a single threaded application....
1
1463
by: Esteban Felipe | last post by:
Hi, thanks for reading. I hope to find some help here before I commit suicide because this is driving me crazy. Please excuse me if this looks like a long post, but I hope that a complete explanation help you to help me :=) ....Let's start with some background: ..- I'm building an asp.net application that requires users to upload text files in cvs format with data exported from an AS-400. ..- The files will be something between 800kb...
14
3269
by: Lior Amar | last post by:
Quick question about threads and delegates. I have the following scenario Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a callback Thread B Invokes a DelegateB asynchronously (could be a timer but then we get Thread C) Upon completion of DelegateB, Thread B would like to callback ThreadA using DelegateA but as we all know the call to DelegateA is running in ThreadB. Is
9
1071
by: Kelly Adams | last post by:
I am stumped at the moment by something, and the searching I've done hasn't led to a clear answer. Here is what I want to do: I have two threads: thread A and thread B. I want Thread B to raise an event that will be handled within the context of thread A. By default, an event raised by Thread B will be processed within thread B's context. That's not what I want. What is the best / simplest way to accomplish this? I've looked at...
0
9856
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
10916
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10598
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10657
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10299
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.