F
Frank Dietrich
Daniel,
I am having a guided dialog consisting of a collection of panels.
Normally I am loading each "page" as it's needed. My idea now was to
employ the backgroundworker and (pre)load pages in the background.
As I am not supposed to touch the GUI from the DoWork, DoWork actually
does nothing more than a for-loop that calls ReportProgress with the
panel that needs to be added as an additional Parameter.
This is all there is:
private void Loader_DoWork(object sender, DoWorkEventArgs e)
{
// let's get the Backgroundworker from the "Sender"
BackgroundWorker oLoader = (BackgroundWorker)sender;
// and check how many pages need to be added
int nPMax = (int)e.Argument;
if (nPMax > 0)
{
for (int nPage = 1; nPage <= nPMax; nPage++)
{
oLoader.ReportProgress(nPage/nPMax * 100, nPage);
}
}
}
In my sample nPMax is 4 and if I use the debugger ReportProgress gets
called with 1, 2, 3 and finally 4 ( surprisingly ;-) ).
In the ProgressChanged I am actually adding the Panel as I have read
that it's safe to touch the GUI here.
As You can see, no rocket-science there either:
private void Loader_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
// witch Page should be added
int nI = (int)e.UserState;
this.AddPage(nI);
// MessageBox.Show(
"ProgressChanged "+ e.ProgressPercentage.ToString() +" - "+
e.UserState.ToString() );
// If we added the first page, display it to the user
// so that he can start to work
if ( nI == 1 )
{
this.NavigateTo(nI);
}
}
The weird thing is: the for-loop runs through calling ReportProgress
with the correct values but Progresschanged does not even fire once
UNTIL the loop and thus the DoWork-Job is done. Then ProgressChanged
is fired 4 times but always receiving 100% and the Value 4.
I am definitely standing on the line somewhere here. Maybe I should
try to give the worker some idle-Time (sleep)?
Any helb will be appreciated.
Regards from Berlin
Frank
I am having a guided dialog consisting of a collection of panels.
Normally I am loading each "page" as it's needed. My idea now was to
employ the backgroundworker and (pre)load pages in the background.
As I am not supposed to touch the GUI from the DoWork, DoWork actually
does nothing more than a for-loop that calls ReportProgress with the
panel that needs to be added as an additional Parameter.
This is all there is:
private void Loader_DoWork(object sender, DoWorkEventArgs e)
{
// let's get the Backgroundworker from the "Sender"
BackgroundWorker oLoader = (BackgroundWorker)sender;
// and check how many pages need to be added
int nPMax = (int)e.Argument;
if (nPMax > 0)
{
for (int nPage = 1; nPage <= nPMax; nPage++)
{
oLoader.ReportProgress(nPage/nPMax * 100, nPage);
}
}
}
In my sample nPMax is 4 and if I use the debugger ReportProgress gets
called with 1, 2, 3 and finally 4 ( surprisingly ;-) ).
In the ProgressChanged I am actually adding the Panel as I have read
that it's safe to touch the GUI here.
As You can see, no rocket-science there either:
private void Loader_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
// witch Page should be added
int nI = (int)e.UserState;
this.AddPage(nI);
// MessageBox.Show(
"ProgressChanged "+ e.ProgressPercentage.ToString() +" - "+
e.UserState.ToString() );
// If we added the first page, display it to the user
// so that he can start to work
if ( nI == 1 )
{
this.NavigateTo(nI);
}
}
The weird thing is: the for-loop runs through calling ReportProgress
with the correct values but Progresschanged does not even fire once
UNTIL the loop and thus the DoWork-Job is done. Then ProgressChanged
is fired 4 times but always receiving 100% and the Value 4.
I am definitely standing on the line somewhere here. Maybe I should
try to give the worker some idle-Time (sleep)?
Any helb will be appreciated.
Regards from Berlin
Frank