473,473 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

BackGroundWorker

I have an application that creates a pretty intensive TreeView at startup
time. I had the program running on a single thread sucessfully, but the
startup time was a bit excessive. When the application starts up it is a
requirement for the user to log on, so in my magical wisdom, I thought shove
all this processing on another thread, so it loads while the user logs on. I
have all the code running on the thread OK, but I cannot figure out how to
return the data to the main thread so that I can update the UI from the main
thread.

In the BackgroundWorker task I have created a new TreeView that is not
attached to any UI. I build this in the background, I then attempted to Pass
this back through an MSMQ Queue, but unfortunately the TreeView class in non
Serializable so the Queue.Send fails.

Can someone please explain to the best way to return this data to the main
thread.

Thanks in advance

Jamie
Apr 13 '06 #1
11 2815
IMHO, something like this is easier and a straight forward pattern:
http://channel9.msdn.com/ShowPost.aspx?PostID=171594

--
William Stacey [MVP]

"Jamie Bissett" <ja**********@yahoo.com> wrote in message
news:uU**************@TK2MSFTNGP02.phx.gbl...
|I have an application that creates a pretty intensive TreeView at startup
| time. I had the program running on a single thread sucessfully, but the
| startup time was a bit excessive. When the application starts up it is a
| requirement for the user to log on, so in my magical wisdom, I thought
shove
| all this processing on another thread, so it loads while the user logs on.
I
| have all the code running on the thread OK, but I cannot figure out how to
| return the data to the main thread so that I can update the UI from the
main
| thread.
|
| In the BackgroundWorker task I have created a new TreeView that is not
| attached to any UI. I build this in the background, I then attempted to
Pass
| this back through an MSMQ Queue, but unfortunately the TreeView class in
non
| Serializable so the Queue.Send fails.
|
| Can someone please explain to the best way to return this data to the main
| thread.
|
| Thanks in advance
|
| Jamie
|
|
Apr 13 '06 #2
Thanks for the info William,
I have implemented this model and yes it does work, but it is causing me a
little visual problem.

What I do at startup is start the other thread as described in the model you
provided me, I hide the main form (which has the treeviews on it) and diaply
a login dialog.
What is happening now is that it see,s to delay the the painting o f the
login dialog i.e. the borders draw with a plain white background in the
dilaog then about 2 secs later the dilaog fills.

Any ideas?

Jamie
"Jamie Bissett" <ja**********@yahoo.com> wrote in message
news:uU**************@TK2MSFTNGP02.phx.gbl...
I have an application that creates a pretty intensive TreeView at startup
time. I had the program running on a single thread sucessfully, but the
startup time was a bit excessive. When the application starts up it is a
requirement for the user to log on, so in my magical wisdom, I thought
shove all this processing on another thread, so it loads while the user
logs on. I have all the code running on the thread OK, but I cannot figure
out how to return the data to the main thread so that I can update the UI
from the main thread.

In the BackgroundWorker task I have created a new TreeView that is not
attached to any UI. I build this in the background, I then attempted to
Pass this back through an MSMQ Queue, but unfortunately the TreeView class
in non Serializable so the Queue.Send fails.

Can someone please explain to the best way to return this data to the main
thread.

Thanks in advance

Jamie

Apr 13 '06 #3
Maybe wait to start the thread after your dialog is displayed. Sounds like
maybe the thread is doing much work and not allowing UI events. Also
Suspend updates while building the UI on the hidden form and Resume updates
on the control after updates are done.

--
William Stacey [MVP]

"Jamie Bissett" <ja**********@yahoo.com> wrote in message
news:ey**************@TK2MSFTNGP03.phx.gbl...
| Thanks for the info William,
| I have implemented this model and yes it does work, but it is causing me
a
| little visual problem.
|
| What I do at startup is start the other thread as described in the model
you
| provided me, I hide the main form (which has the treeviews on it) and
diaply
| a login dialog.
| What is happening now is that it see,s to delay the the painting o f the
| login dialog i.e. the borders draw with a plain white background in the
| dilaog then about 2 secs later the dilaog fills.
|
| Any ideas?
|
| Jamie
| "Jamie Bissett" <ja**********@yahoo.com> wrote in message
| news:uU**************@TK2MSFTNGP02.phx.gbl...
| >I have an application that creates a pretty intensive TreeView at startup
| >time. I had the program running on a single thread sucessfully, but the
| >startup time was a bit excessive. When the application starts up it is a
| >requirement for the user to log on, so in my magical wisdom, I thought
| >shove all this processing on another thread, so it loads while the user
| >logs on. I have all the code running on the thread OK, but I cannot
figure
| >out how to return the data to the main thread so that I can update the UI
| >from the main thread.
| >
| > In the BackgroundWorker task I have created a new TreeView that is not
| > attached to any UI. I build this in the background, I then attempted to
| > Pass this back through an MSMQ Queue, but unfortunately the TreeView
class
| > in non Serializable so the Queue.Send fails.
| >
| > Can someone please explain to the best way to return this data to the
main
| > thread.
| >
| > Thanks in advance
| >
| > Jamie
| >
|
|
Apr 13 '06 #4
I already Suspend the Updates on the UI adn Suspend Layout. I cannot move
this thread start to after the dialog display as it is a ShowDailog, which
means the thread will not start until the user has been validated, which
takes away the whole point of running this update on another thread.

Jamie
"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:OR*************@TK2MSFTNGP02.phx.gbl...
Maybe wait to start the thread after your dialog is displayed. Sounds
like
maybe the thread is doing much work and not allowing UI events. Also
Suspend updates while building the UI on the hidden form and Resume
updates
on the control after updates are done.

--
William Stacey [MVP]

"Jamie Bissett" <ja**********@yahoo.com> wrote in message
news:ey**************@TK2MSFTNGP03.phx.gbl...
| Thanks for the info William,
| I have implemented this model and yes it does work, but it is causing
me
a
| little visual problem.
|
| What I do at startup is start the other thread as described in the model
you
| provided me, I hide the main form (which has the treeviews on it) and
diaply
| a login dialog.
| What is happening now is that it see,s to delay the the painting o f the
| login dialog i.e. the borders draw with a plain white background in the
| dilaog then about 2 secs later the dilaog fills.
|
| Any ideas?
|
| Jamie
| "Jamie Bissett" <ja**********@yahoo.com> wrote in message
| news:uU**************@TK2MSFTNGP02.phx.gbl...
| >I have an application that creates a pretty intensive TreeView at
startup
| >time. I had the program running on a single thread sucessfully, but the
| >startup time was a bit excessive. When the application starts up it is
a
| >requirement for the user to log on, so in my magical wisdom, I thought
| >shove all this processing on another thread, so it loads while the user
| >logs on. I have all the code running on the thread OK, but I cannot
figure
| >out how to return the data to the main thread so that I can update the
UI
| >from the main thread.
| >
| > In the BackgroundWorker task I have created a new TreeView that is not
| > attached to any UI. I build this in the background, I then attempted
to
| > Pass this back through an MSMQ Queue, but unfortunately the TreeView
class
| > in non Serializable so the Queue.Send fails.
| >
| > Can someone please explain to the best way to return this data to the
main
| > thread.
| >
| > Thanks in advance
| >
| > Jamie
| >
|
|

Apr 13 '06 #5
Without seeing any code, maybe a sleep(50) before the thread starts the loop
to give the dialog a chance to display.

--
William Stacey [MVP]

"Jamie Bissett" <ja**********@yahoo.com> wrote in message
news:eY**************@TK2MSFTNGP02.phx.gbl...
|I already Suspend the Updates on the UI adn Suspend Layout. I cannot move
| this thread start to after the dialog display as it is a ShowDailog, which
| means the thread will not start until the user has been validated, which
| takes away the whole point of running this update on another thread.
|
| Jamie
| "William Stacey [MVP]" <wi************@gmail.com> wrote in message
| news:OR*************@TK2MSFTNGP02.phx.gbl...
| > Maybe wait to start the thread after your dialog is displayed. Sounds
| > like
| > maybe the thread is doing much work and not allowing UI events. Also
| > Suspend updates while building the UI on the hidden form and Resume
| > updates
| > on the control after updates are done.
| >
| > --
| > William Stacey [MVP]
| >
| > "Jamie Bissett" <ja**********@yahoo.com> wrote in message
| > news:ey**************@TK2MSFTNGP03.phx.gbl...
| > | Thanks for the info William,
| > | I have implemented this model and yes it does work, but it is causing
| > me
| > a
| > | little visual problem.
| > |
| > | What I do at startup is start the other thread as described in the
model
| > you
| > | provided me, I hide the main form (which has the treeviews on it) and
| > diaply
| > | a login dialog.
| > | What is happening now is that it see,s to delay the the painting o f
the
| > | login dialog i.e. the borders draw with a plain white background in
the
| > | dilaog then about 2 secs later the dilaog fills.
| > |
| > | Any ideas?
| > |
| > | Jamie
| > | "Jamie Bissett" <ja**********@yahoo.com> wrote in message
| > | news:uU**************@TK2MSFTNGP02.phx.gbl...
| > | >I have an application that creates a pretty intensive TreeView at
| > startup
| > | >time. I had the program running on a single thread sucessfully, but
the
| > | >startup time was a bit excessive. When the application starts up it
is
| > a
| > | >requirement for the user to log on, so in my magical wisdom, I
thought
| > | >shove all this processing on another thread, so it loads while the
user
| > | >logs on. I have all the code running on the thread OK, but I cannot
| > figure
| > | >out how to return the data to the main thread so that I can update
the
| > UI
| > | >from the main thread.
| > | >
| > | > In the BackgroundWorker task I have created a new TreeView that is
not
| > | > attached to any UI. I build this in the background, I then attempted
| > to
| > | > Pass this back through an MSMQ Queue, but unfortunately the TreeView
| > class
| > | > in non Serializable so the Queue.Send fails.
| > | >
| > | > Can someone please explain to the best way to return this data to
the
| > main
| > | > thread.
| > | >
| > | > Thanks in advance
| > | >
| > | > Jamie
| > | >
| > |
| > |
| >
| >
|
|
Apr 13 '06 #6
This is the Form Load Code

private void frmMenuMaint_Load(object sender, EventArgs e)
{
// Hide the Main form (this has the Treeviews on this form
this.Visible = false;
// Create an instance of the Login Form
using (Utilties.frmLogin frm = new
Utilties.frmLogin(ConfigurationManager.ConnectionS trings["Eden_POS_Management.Properties.Settings.HotelConn ectionString"].ConnectionString))
{
new Thread(delegate()
{
this.Invoke((ThreadStart)delegate()
{
// This class performs the intense loading of the
tree view
// It initialy perofms a
// tvMenus.SuspendLayout();
// tv.BeginUpdate();
// and resumes them on completion
LoadTreeViews lt = new LoadTreeViews();
lt.loadMenuTree(Plans, tvMenus);
});
}).Start();
Thread.Sleep(150); // Doesn;t seem to make any
difference
// Validate the User, the Dialog will only return the OK
result if the User Credentials are vaidated
// This is the dialog that is Painting Slowly, it paints
immediatley if I dont have the Load Treeview
// therefor there is nothing slowing it down in this
dialog
frm.ShowDialog();
if (frm.DialogResult == DialogResult.OK)
{
this.Visible = true;
}
else
{
MessageBox.Show("Invalid Login Attempt\nPlease Contact
you're System Administrator", "Data Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
Application.Exit();
}
}
}

Thank you very much for you're continued input, I'll buy you a beer or 2 if
we ever get that conference together in Thailand .......

Jamie

"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Without seeing any code, maybe a sleep(50) before the thread starts the
loop
to give the dialog a chance to display.

--
William Stacey [MVP]

"Jamie Bissett" <ja**********@yahoo.com> wrote in message
news:eY**************@TK2MSFTNGP02.phx.gbl...
|I already Suspend the Updates on the UI adn Suspend Layout. I cannot move
| this thread start to after the dialog display as it is a ShowDailog,
which
| means the thread will not start until the user has been validated, which
| takes away the whole point of running this update on another thread.
|
| Jamie
| "William Stacey [MVP]" <wi************@gmail.com> wrote in message
| news:OR*************@TK2MSFTNGP02.phx.gbl...
| > Maybe wait to start the thread after your dialog is displayed.
Sounds
| > like
| > maybe the thread is doing much work and not allowing UI events. Also
| > Suspend updates while building the UI on the hidden form and Resume
| > updates
| > on the control after updates are done.
| >
| > --
| > William Stacey [MVP]
| >
| > "Jamie Bissett" <ja**********@yahoo.com> wrote in message
| > news:ey**************@TK2MSFTNGP03.phx.gbl...
| > | Thanks for the info William,
| > | I have implemented this model and yes it does work, but it is
causing
| > me
| > a
| > | little visual problem.
| > |
| > | What I do at startup is start the other thread as described in the
model
| > you
| > | provided me, I hide the main form (which has the treeviews on it)
and
| > diaply
| > | a login dialog.
| > | What is happening now is that it see,s to delay the the painting o f
the
| > | login dialog i.e. the borders draw with a plain white background in
the
| > | dilaog then about 2 secs later the dilaog fills.
| > |
| > | Any ideas?
| > |
| > | Jamie
| > | "Jamie Bissett" <ja**********@yahoo.com> wrote in message
| > | news:uU**************@TK2MSFTNGP02.phx.gbl...
| > | >I have an application that creates a pretty intensive TreeView at
| > startup
| > | >time. I had the program running on a single thread sucessfully, but
the
| > | >startup time was a bit excessive. When the application starts up it
is
| > a
| > | >requirement for the user to log on, so in my magical wisdom, I
thought
| > | >shove all this processing on another thread, so it loads while the
user
| > | >logs on. I have all the code running on the thread OK, but I cannot
| > figure
| > | >out how to return the data to the main thread so that I can update
the
| > UI
| > | >from the main thread.
| > | >
| > | > In the BackgroundWorker task I have created a new TreeView that is
not
| > | > attached to any UI. I build this in the background, I then
attempted
| > to
| > | > Pass this back through an MSMQ Queue, but unfortunately the
TreeView
| > class
| > | > in non Serializable so the Queue.Send fails.
| > | >
| > | > Can someone please explain to the best way to return this data to
the
| > main
| > | > thread.
| > | >
| > | > Thanks in advance
| > | >
| > | > Jamie
| > | >
| > |
| > |
| >
| >
|
|

Apr 13 '06 #7
Couple issues here I think I see. Your worker thread is not actually doing
much here as you turn around and Invoke right way. Everything in the
Invoke{} is being done on the UI thread so it is blocking other UI events -
that must be the issue you see.
If possible, you want to refactor your work out from your UI work. In your
Thread, do the work loop and Invoke only the UI updates.
Such as

new Thread(delegate
{
while(true)
{
// Read line 1.
// Calc
if ( done )
break;

// Update UI
this.Invoke((ThreadStart)delegate()
{
this.treeview.update(xyz)/
}
}
}).Start();

frm.ShowDialog();
....
--
William Stacey [MVP]

"Jamie Bissett" <ja**********@yahoo.com> wrote in message
news:uV**************@TK2MSFTNGP04.phx.gbl...
| This is the Form Load Code
|
| private void frmMenuMaint_Load(object sender, EventArgs e)
| {
| // Hide the Main form (this has the Treeviews on this form
| this.Visible = false;
| // Create an instance of the Login Form
| using (Utilties.frmLogin frm = new
|
Utilties.frmLogin(ConfigurationManager.ConnectionS trings["Eden_POS_Management.Properties.Settings.HotelConn ectionString"].ConnectionString))
| {
| new Thread(delegate()
| {
| this.Invoke((ThreadStart)delegate()
| {
| // This class performs the intense loading of the
| tree view
| // It initialy perofms a
| // tvMenus.SuspendLayout();
| // tv.BeginUpdate();
| // and resumes them on completion
| LoadTreeViews lt = new LoadTreeViews();
| lt.loadMenuTree(Plans, tvMenus);
| });
| }).Start();
| Thread.Sleep(150); // Doesn;t seem to make any
| difference
| // Validate the User, the Dialog will only return the OK
| result if the User Credentials are vaidated
| // This is the dialog that is Painting Slowly, it paints
| immediatley if I dont have the Load Treeview
| // therefor there is nothing slowing it down in this
| dialog
| frm.ShowDialog();
| if (frm.DialogResult == DialogResult.OK)
| {
| this.Visible = true;
| }
| else
| {
| MessageBox.Show("Invalid Login Attempt\nPlease Contact
| you're System Administrator", "Data Error", MessageBoxButtons.OK,
| MessageBoxIcon.Error);
| Application.Exit();
| }
| }
| }
|
| Thank you very much for you're continued input, I'll buy you a beer or 2
if
| we ever get that conference together in Thailand .......
|
| Jamie
|
| "William Stacey [MVP]" <wi************@gmail.com> wrote in message
| news:%2****************@TK2MSFTNGP03.phx.gbl...
| > Without seeing any code, maybe a sleep(50) before the thread starts the
| > loop
| > to give the dialog a chance to display.
| >
| > --
| > William Stacey [MVP]
| >
| > "Jamie Bissett" <ja**********@yahoo.com> wrote in message
| > news:eY**************@TK2MSFTNGP02.phx.gbl...
| > |I already Suspend the Updates on the UI adn Suspend Layout. I cannot
move
| > | this thread start to after the dialog display as it is a ShowDailog,
| > which
| > | means the thread will not start until the user has been validated,
which
| > | takes away the whole point of running this update on another thread.
| > |
| > | Jamie
| > | "William Stacey [MVP]" <wi************@gmail.com> wrote in message
| > | news:OR*************@TK2MSFTNGP02.phx.gbl...
| > | > Maybe wait to start the thread after your dialog is displayed.
| > Sounds
| > | > like
| > | > maybe the thread is doing much work and not allowing UI events.
Also
| > | > Suspend updates while building the UI on the hidden form and Resume
| > | > updates
| > | > on the control after updates are done.
| > | >
| > | > --
| > | > William Stacey [MVP]
| > | >
| > | > "Jamie Bissett" <ja**********@yahoo.com> wrote in message
| > | > news:ey**************@TK2MSFTNGP03.phx.gbl...
| > | > | Thanks for the info William,
| > | > | I have implemented this model and yes it does work, but it is
| > causing
| > | > me
| > | > a
| > | > | little visual problem.
| > | > |
| > | > | What I do at startup is start the other thread as described in the
| > model
| > | > you
| > | > | provided me, I hide the main form (which has the treeviews on it)
| > and
| > | > diaply
| > | > | a login dialog.
| > | > | What is happening now is that it see,s to delay the the painting o
f
| > the
| > | > | login dialog i.e. the borders draw with a plain white background
in
| > the
| > | > | dilaog then about 2 secs later the dilaog fills.
| > | > |
| > | > | Any ideas?
| > | > |
| > | > | Jamie
| > | > | "Jamie Bissett" <ja**********@yahoo.com> wrote in message
| > | > | news:uU**************@TK2MSFTNGP02.phx.gbl...
| > | > | >I have an application that creates a pretty intensive TreeView at
| > | > startup
| > | > | >time. I had the program running on a single thread sucessfully,
but
| > the
| > | > | >startup time was a bit excessive. When the application starts up
it
| > is
| > | > a
| > | > | >requirement for the user to log on, so in my magical wisdom, I
| > thought
| > | > | >shove all this processing on another thread, so it loads while
the
| > user
| > | > | >logs on. I have all the code running on the thread OK, but I
cannot
| > | > figure
| > | > | >out how to return the data to the main thread so that I can
update
| > the
| > | > UI
| > | > | >from the main thread.
| > | > | >
| > | > | > In the BackgroundWorker task I have created a new TreeView that
is
| > not
| > | > | > attached to any UI. I build this in the background, I then
| > attempted
| > | > to
| > | > | > Pass this back through an MSMQ Queue, but unfortunately the
| > TreeView
| > | > class
| > | > | > in non Serializable so the Queue.Send fails.
| > | > | >
| > | > | > Can someone please explain to the best way to return this data
to
| > the
| > | > main
| > | > | > thread.
| > | > | >
| > | > | > Thanks in advance
| > | > | >
| > | > | > Jamie
| > | > | >
| > | > |
| > | > |
| > | >
| > | >
| > |
| > |
| >
| >
|
|
Apr 13 '06 #8
That would be pretty hard as there is a single function called recursively
to x depth depending on the depth of the tree. This function adds nodes to
the tree at the respective depth. Thats why I was trying to load a
collection I could just assign to the treeview when I completed the worker
task, but I couldn't figure out how to, since
a) Cannot Serialize a treeview control
b) No constructors for a TreeNodeCollection
c) I couldn't figure out what to use in it's place (as it would defeat the
purpose if I had to recursively march through another tree upon return)
d) I couldn't figure out how to return the data to the Parent Thread

Thanks again for you;re assistance, I really apreciate it,

Jamie
"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:OH****************@TK2MSFTNGP04.phx.gbl...
Couple issues here I think I see. Your worker thread is not actually
doing
much here as you turn around and Invoke right way. Everything in the
Invoke{} is being done on the UI thread so it is blocking other UI
events -
that must be the issue you see.
If possible, you want to refactor your work out from your UI work. In
your
Thread, do the work loop and Invoke only the UI updates.
Such as

new Thread(delegate
{
while(true)
{
// Read line 1.
// Calc
if ( done )
break;

// Update UI
this.Invoke((ThreadStart)delegate()
{
this.treeview.update(xyz)/
}
}
}).Start();

frm.ShowDialog();
...
--
William Stacey [MVP]

"Jamie Bissett" <ja**********@yahoo.com> wrote in message
news:uV**************@TK2MSFTNGP04.phx.gbl...
| This is the Form Load Code
|
| private void frmMenuMaint_Load(object sender, EventArgs e)
| {
| // Hide the Main form (this has the Treeviews on this form
| this.Visible = false;
| // Create an instance of the Login Form
| using (Utilties.frmLogin frm = new
|
Utilties.frmLogin(ConfigurationManager.ConnectionS trings["Eden_POS_Management.Properties.Settings.HotelConn ectionString"].ConnectionString))
| {
| new Thread(delegate()
| {
| this.Invoke((ThreadStart)delegate()
| {
| // This class performs the intense loading of the
| tree view
| // It initialy perofms a
| // tvMenus.SuspendLayout();
| // tv.BeginUpdate();
| // and resumes them on completion
| LoadTreeViews lt = new LoadTreeViews();
| lt.loadMenuTree(Plans, tvMenus);
| });
| }).Start();
| Thread.Sleep(150); // Doesn;t seem to make any
| difference
| // Validate the User, the Dialog will only return the OK
| result if the User Credentials are vaidated
| // This is the dialog that is Painting Slowly, it paints
| immediatley if I dont have the Load Treeview
| // therefor there is nothing slowing it down in this
| dialog
| frm.ShowDialog();
| if (frm.DialogResult == DialogResult.OK)
| {
| this.Visible = true;
| }
| else
| {
| MessageBox.Show("Invalid Login Attempt\nPlease
Contact
| you're System Administrator", "Data Error", MessageBoxButtons.OK,
| MessageBoxIcon.Error);
| Application.Exit();
| }
| }
| }
|
| Thank you very much for you're continued input, I'll buy you a beer or 2
if
| we ever get that conference together in Thailand .......
|
| Jamie
|
| "William Stacey [MVP]" <wi************@gmail.com> wrote in message
| news:%2****************@TK2MSFTNGP03.phx.gbl...
| > Without seeing any code, maybe a sleep(50) before the thread starts
the
| > loop
| > to give the dialog a chance to display.
| >
| > --
| > William Stacey [MVP]
| >
| > "Jamie Bissett" <ja**********@yahoo.com> wrote in message
| > news:eY**************@TK2MSFTNGP02.phx.gbl...
| > |I already Suspend the Updates on the UI adn Suspend Layout. I cannot
move
| > | this thread start to after the dialog display as it is a ShowDailog,
| > which
| > | means the thread will not start until the user has been validated,
which
| > | takes away the whole point of running this update on another thread.
| > |
| > | Jamie
| > | "William Stacey [MVP]" <wi************@gmail.com> wrote in message
| > | news:OR*************@TK2MSFTNGP02.phx.gbl...
| > | > Maybe wait to start the thread after your dialog is displayed.
| > Sounds
| > | > like
| > | > maybe the thread is doing much work and not allowing UI events.
Also
| > | > Suspend updates while building the UI on the hidden form and
Resume
| > | > updates
| > | > on the control after updates are done.
| > | >
| > | > --
| > | > William Stacey [MVP]
| > | >
| > | > "Jamie Bissett" <ja**********@yahoo.com> wrote in message
| > | > news:ey**************@TK2MSFTNGP03.phx.gbl...
| > | > | Thanks for the info William,
| > | > | I have implemented this model and yes it does work, but it is
| > causing
| > | > me
| > | > a
| > | > | little visual problem.
| > | > |
| > | > | What I do at startup is start the other thread as described in
the
| > model
| > | > you
| > | > | provided me, I hide the main form (which has the treeviews on
it)
| > and
| > | > diaply
| > | > | a login dialog.
| > | > | What is happening now is that it see,s to delay the the painting
o
f
| > the
| > | > | login dialog i.e. the borders draw with a plain white background
in
| > the
| > | > | dilaog then about 2 secs later the dilaog fills.
| > | > |
| > | > | Any ideas?
| > | > |
| > | > | Jamie
| > | > | "Jamie Bissett" <ja**********@yahoo.com> wrote in message
| > | > | news:uU**************@TK2MSFTNGP02.phx.gbl...
| > | > | >I have an application that creates a pretty intensive TreeView
at
| > | > startup
| > | > | >time. I had the program running on a single thread sucessfully,
but
| > the
| > | > | >startup time was a bit excessive. When the application starts
up
it
| > is
| > | > a
| > | > | >requirement for the user to log on, so in my magical wisdom, I
| > thought
| > | > | >shove all this processing on another thread, so it loads while
the
| > user
| > | > | >logs on. I have all the code running on the thread OK, but I
cannot
| > | > figure
| > | > | >out how to return the data to the main thread so that I can
update
| > the
| > | > UI
| > | > | >from the main thread.
| > | > | >
| > | > | > In the BackgroundWorker task I have created a new TreeView
that
is
| > not
| > | > | > attached to any UI. I build this in the background, I then
| > attempted
| > | > to
| > | > | > Pass this back through an MSMQ Queue, but unfortunately the
| > TreeView
| > | > class
| > | > | > in non Serializable so the Queue.Send fails.
| > | > | >
| > | > | > Can someone please explain to the best way to return this data
to
| > the
| > | > main
| > | > | > thread.
| > | > | >
| > | > | > Thanks in advance
| > | > | >
| > | > | > Jamie
| > | > | >
| > | > |
| > | > |
| > | >
| > | >
| > |
| > |
| >
| >
|
|

Apr 13 '06 #9

Jamie Bissett wrote:
That would be pretty hard as there is a single function called recursively
to x depth depending on the depth of the tree. This function adds nodes to
the tree at the respective depth. Thats why I was trying to load a
collection I could just assign to the treeview when I completed the worker
task, but I couldn't figure out how to, since
a) Cannot Serialize a treeview control
b) No constructors for a TreeNodeCollection
c) I couldn't figure out what to use in it's place (as it would defeat the
purpose if I had to recursively march through another tree upon return)


TreeNodeCollection.AddRange Method

Adds an array of previously created tree nodes to the collection.

public virtual void AddRange(
TreeNode[] nodes
);

Parameters
nodes
An array of TreeNode objects representing the tree nodes to add to the
collection.

Remarks
The TreeNode objects contained in the nodes array are appended to the
end of the collection.

You can use the AddRange method to quickly add a group of previously
created TreeNode objects to the collection instead of manually adding
each TreeNode to the collection using the Add method.
--
Larry Lard
Replies to group please

Apr 13 '06 #10
Larry,
thanks for you're response. I am aware of addRange in nearly all
Collection based controls. I cannot use this as I onbly determine each
single node as it is found from the database, you then end up with the
following structure (this is rather simplified but illustrative)

Root Node ----- NodeCollection ---
|
|----->
ChildNode -----> nodeCollection ---

|

| ---> ChildNode ---->
NodeCollection ----- etc etc
|
|----->
ChildNode----->nodeCollection ----

|

|

etc

etc
RootNode2-----NodeCollection --- etc, etc

I cannot see how addrange would help in this scenario as evenryting is built
dynamically.

FYI -- the actual adding of the nodes is less than half of the run time.

Honestly this is not a huge issue for me, the total load time is about 2
secs, but i am a purist at times, it was also a very good excersise in
brushing up on multi threaded applications that need to feed back to the UI,
which I was keen on learning as this could be quite helpful in may
circumstances. Even more so in the days to come, when Dual COre processors
become the Norm rather than the exception.

I really thank everybody for assisting me in my dilema.

Cheers guys

jamie


"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...

Jamie Bissett wrote:
That would be pretty hard as there is a single function called
recursively
to x depth depending on the depth of the tree. This function adds nodes
to
the tree at the respective depth. Thats why I was trying to load a
collection I could just assign to the treeview when I completed the
worker
task, but I couldn't figure out how to, since
a) Cannot Serialize a treeview control
b) No constructors for a TreeNodeCollection
c) I couldn't figure out what to use in it's place (as it would defeat
the
purpose if I had to recursively march through another tree upon return)


TreeNodeCollection.AddRange Method

Adds an array of previously created tree nodes to the collection.

public virtual void AddRange(
TreeNode[] nodes
);

Parameters
nodes
An array of TreeNode objects representing the tree nodes to add to the
collection.

Remarks
The TreeNode objects contained in the nodes array are appended to the
end of the collection.

You can use the AddRange method to quickly add a group of previously
created TreeNode objects to the collection instead of manually adding
each TreeNode to the collection using the Add method.
--
Larry Lard
Replies to group please

Apr 13 '06 #11
William,
I want to thank you for all you're help, you managed to get me on the
right track. I have solved the problem by foing the following :

-----------------------------------------------------------------------------------------------------------------------
// Used to store the nodes as they are populated
private Stack<TreeMenuNode> tn = new Stack<TreeMenuNode>();

private void frmMenuMaint_Load(object sender, EventArgs e)
{
this.Visible = false;
Thread _workerThread = new Thread(loadMenuTree);
_workerThread.Start();
using (Utilties.frmLogin frm = new Utilties.frmLogin(......)
{
frm.ShowDialog();
if (frm.DialogResult == DialogResult.OK)
{
this.Visible = true;
}
else
{
MessageBox.Show(.......);
Application.Exit();
}
}
try
{
// If the thread has not finished yet
// wait up to 30 secs for it to complete
if (_workerThread.IsAlive)
_workerThread.Join(30000);
} catch (Exception ex)
{
// Something has gone wrong
MessageBox.Show(......);
ev.WriteEntry(.......);
Application.Exit();
}
// Now add the nodes to the UI
lock (typeof(Stack<TreeMenuNode>))
{
while (tn.Count > 0)
tvMenus.Nodes.Add((TreeMenuNode)tn.Pop());
}

In essece the started Background thread finds all the top level nodes
(TreeMenuNodes in my case -- Inherited from TreeNodes), the task builds each
node and all its siblings attached to it, it then locks the stack var and
pushes the node onto it. This will repeat for each top level node defined
(in my case only 6 at the moment). When the user is logged on, I check to
see of the task has finished, if not block on the task for up to 30sec
(ample time). When the task is done, i just pop the nodes of the stack and
add them to the UI on the UI thread. I think this is the simplest safest
solution.

If you are interested I also got good info from the following 3 URLs

http://weblogs.asp.net/justin_rogers...es/126345.aspx
http://code.snapstream.com/mediawiki...kground_Thread
http://msevents.microsoft.com/CUI/Ev...&Culture=en-US

Thank you again for you're invaluable assistance,

Jamie

"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:OH****************@TK2MSFTNGP04.phx.gbl...
Couple issues here I think I see. Your worker thread is not actually
doing
much here as you turn around and Invoke right way. Everything in the
Invoke{} is being done on the UI thread so it is blocking other UI
events -
that must be the issue you see.
If possible, you want to refactor your work out from your UI work. In
your
Thread, do the work loop and Invoke only the UI updates.
Such as

new Thread(delegate
{
while(true)
{
// Read line 1.
// Calc
if ( done )
break;

// Update UI
this.Invoke((ThreadStart)delegate()
{
this.treeview.update(xyz)/
}
}
}).Start();

frm.ShowDialog();
...
--
William Stacey [MVP]

"Jamie Bissett" <ja**********@yahoo.com> wrote in message
news:uV**************@TK2MSFTNGP04.phx.gbl...
| This is the Form Load Code
|
| private void frmMenuMaint_Load(object sender, EventArgs e)
| {
| // Hide the Main form (this has the Treeviews on this form
| this.Visible = false;
| // Create an instance of the Login Form
| using (Utilties.frmLogin frm = new
|
Utilties.frmLogin(ConfigurationManager.ConnectionS trings["Eden_POS_Management.Properties.Settings.HotelConn ectionString"].ConnectionString))
| {
| new Thread(delegate()
| {
| this.Invoke((ThreadStart)delegate()
| {
| // This class performs the intense loading of the
| tree view
| // It initialy perofms a
| // tvMenus.SuspendLayout();
| // tv.BeginUpdate();
| // and resumes them on completion
| LoadTreeViews lt = new LoadTreeViews();
| lt.loadMenuTree(Plans, tvMenus);
| });
| }).Start();
| Thread.Sleep(150); // Doesn;t seem to make any
| difference
| // Validate the User, the Dialog will only return the OK
| result if the User Credentials are vaidated
| // This is the dialog that is Painting Slowly, it paints
| immediatley if I dont have the Load Treeview
| // therefor there is nothing slowing it down in this
| dialog
| frm.ShowDialog();
| if (frm.DialogResult == DialogResult.OK)
| {
| this.Visible = true;
| }
| else
| {
| MessageBox.Show("Invalid Login Attempt\nPlease
Contact
| you're System Administrator", "Data Error", MessageBoxButtons.OK,
| MessageBoxIcon.Error);
| Application.Exit();
| }
| }
| }
|
| Thank you very much for you're continued input, I'll buy you a beer or 2
if
| we ever get that conference together in Thailand .......
|
| Jamie
|
| "William Stacey [MVP]" <wi************@gmail.com> wrote in message
| news:%2****************@TK2MSFTNGP03.phx.gbl...
| > Without seeing any code, maybe a sleep(50) before the thread starts
the
| > loop
| > to give the dialog a chance to display.
| >
| > --
| > William Stacey [MVP]
| >
| > "Jamie Bissett" <ja**********@yahoo.com> wrote in message
| > news:eY**************@TK2MSFTNGP02.phx.gbl...
| > |I already Suspend the Updates on the UI adn Suspend Layout. I cannot
move
| > | this thread start to after the dialog display as it is a ShowDailog,
| > which
| > | means the thread will not start until the user has been validated,
which
| > | takes away the whole point of running this update on another thread.
| > |
| > | Jamie
| > | "William Stacey [MVP]" <wi************@gmail.com> wrote in message
| > | news:OR*************@TK2MSFTNGP02.phx.gbl...
| > | > Maybe wait to start the thread after your dialog is displayed.
| > Sounds
| > | > like
| > | > maybe the thread is doing much work and not allowing UI events.
Also
| > | > Suspend updates while building the UI on the hidden form and
Resume
| > | > updates
| > | > on the control after updates are done.
| > | >
| > | > --
| > | > William Stacey [MVP]
| > | >
| > | > "Jamie Bissett" <ja**********@yahoo.com> wrote in message
| > | > news:ey**************@TK2MSFTNGP03.phx.gbl...
| > | > | Thanks for the info William,
| > | > | I have implemented this model and yes it does work, but it is
| > causing
| > | > me
| > | > a
| > | > | little visual problem.
| > | > |
| > | > | What I do at startup is start the other thread as described in
the
| > model
| > | > you
| > | > | provided me, I hide the main form (which has the treeviews on
it)
| > and
| > | > diaply
| > | > | a login dialog.
| > | > | What is happening now is that it see,s to delay the the painting
o
f
| > the
| > | > | login dialog i.e. the borders draw with a plain white background
in
| > the
| > | > | dilaog then about 2 secs later the dilaog fills.
| > | > |
| > | > | Any ideas?
| > | > |
| > | > | Jamie
| > | > | "Jamie Bissett" <ja**********@yahoo.com> wrote in message
| > | > | news:uU**************@TK2MSFTNGP02.phx.gbl...
| > | > | >I have an application that creates a pretty intensive TreeView
at
| > | > startup
| > | > | >time. I had the program running on a single thread sucessfully,
but
| > the
| > | > | >startup time was a bit excessive. When the application starts
up
it
| > is
| > | > a
| > | > | >requirement for the user to log on, so in my magical wisdom, I
| > thought
| > | > | >shove all this processing on another thread, so it loads while
the
| > user
| > | > | >logs on. I have all the code running on the thread OK, but I
cannot
| > | > figure
| > | > | >out how to return the data to the main thread so that I can
update
| > the
| > | > UI
| > | > | >from the main thread.
| > | > | >
| > | > | > In the BackgroundWorker task I have created a new TreeView
that
is
| > not
| > | > | > attached to any UI. I build this in the background, I then
| > attempted
| > | > to
| > | > | > Pass this back through an MSMQ Queue, but unfortunately the
| > TreeView
| > | > class
| > | > | > in non Serializable so the Queue.Send fails.
| > | > | >
| > | > | > Can someone please explain to the best way to return this data
to
| > the
| > | > main
| > | > | > thread.
| > | > | >
| > | > | > Thanks in advance
| > | > | >
| > | > | > Jamie
| > | > | >
| > | > |
| > | > |
| > | >
| > | >
| > |
| > |
| >
| >
|
|

Apr 14 '06 #12

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

Similar topics

2
by: dm1608 | last post by:
Hi -- I have a C# application that basically has a button that executes a SQL Reader to loop thru a rather large resul set. Thru each interation of the reader object, I check to see if a file...
5
by: Rob R. Ainscough | last post by:
I'm using a BackgroundWorker to perform a file download from an ftp site. Per good code design practices where I separate my UI code from my core logic code (in this case my Download file method in...
1
by: Bob | last post by:
Hi, I am having trouble seeing how this bolts together. The UI starts a process which involves a long running database update. All Database activity is handled by a class called DT. DT has a...
5
by: redear | last post by:
Is there a way to immediately terminate a BackgroundWorker thread? My problem is that the BackgroundWorker starts with a call to My.Computer.FileSystem.GetFiles that can run for a very long time if...
14
by: =?Utf-8?B?SXNobWFlbA==?= | last post by:
Hi, I have a form with a progress bar on it and wanted to use the BackgroundWorker to be able to update the progress. I looked at examples, run some of them, but in debug, when the code gets to...
0
by: Chris | last post by:
I would like to be able to pass the BackgroundWorker object and DoWork Event Args to a second function (third function?) and be able to still report the progress. I'm getting the following...
2
by: Chris | last post by:
When I try to access the backgroundWorker.CancellationPending property I get the following exception: An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in...
9
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
4
by: Sin Jeong-hun | last post by:
This is what I've always been wondered. Suppose I've created a class named Agent, and the Agent does some lengthy job. Of course I don't want to block the main window, so the Agent does the job in...
2
by: =?Utf-8?B?SGFycnkgS2Vjaw==?= | last post by:
I have introduced a component to my solution that is throwing an exception about needing to be run in single threaded apartment mode. This component is created in an async call by a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
1
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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 ...

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.