J
Joey Powell
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.BeginInvoke(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(CCResult)))
{
this.lblStatus.Text = "Status: Check failed.";
Environment.Exit(-1);
}
this.lblStatus.Text = "Status: Check was successful.";
//Upload report totals
this.lblStatus.Text = "Status: Uploading report totals, please
wait...";
_URTDelegate URTDelegate = new _URTDelegate(URT);
IAsyncResult URTResult = URTDelegate.BeginInvoke(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(!(URTDelegate.EndInvoke(URTResult)))
{
this.lblStatus.Text = "Status: Upload of report totals failed.";
Environment.Exit(-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.
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.BeginInvoke(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(CCResult)))
{
this.lblStatus.Text = "Status: Check failed.";
Environment.Exit(-1);
}
this.lblStatus.Text = "Status: Check was successful.";
//Upload report totals
this.lblStatus.Text = "Status: Uploading report totals, please
wait...";
_URTDelegate URTDelegate = new _URTDelegate(URT);
IAsyncResult URTResult = URTDelegate.BeginInvoke(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(!(URTDelegate.EndInvoke(URTResult)))
{
this.lblStatus.Text = "Status: Upload of report totals failed.";
Environment.Exit(-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.