469,579 Members | 1,145 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

how to run a window on a seperate thread

Hi,

I have some intensive code that is running on my main thread. I try to show
a status update on a 'status form'. The problem that i have is that because
it is running in the same thread the window is not responding to the user.

The user is now able to minimize, move the window because the code is too
busy on it's own work.
(and they are all running on the same thread)

I thought the solution would be to run the window on a seperate thread. so I
added the following code: 1 method called CreateStatusForm that creates the
form and assigns it to my variable _frmStatus, and my code running this
method in a seperate thread:

private void CreateStatusForm()
{
_frmStatus = new AdwareForm();
fInitializedStatus = true;
}

private AdwareTrace()
{
_oFormThread = new Thread(new ThreadStart(this.CreateStatusForm));
_oFormThread.Name = "Thread: StatusForm";
_oFormThread.Start();

while (oFormThread.IsAlive)
{
Thread.Sleep(250);
}
and so on ...

the problem is that on the initialization of the window runs on the seperate
thread.
After the initialization the window does not respond to the user again.

Can anybody tell me how to run forms on seperate threads so that the users
can continue to interact with it ?
Thanks very much in advance for your help,

Regards, SharpKnight

SharpKnight
Nov 15 '05 #1
8 3145
"Serge" <se**********@hotmail.com> wrote in message
news:3f***********************@reader1.news.skynet .be...
Hi,

I have some intensive code that is running on my main thread. I try to show a status update on a 'status form'. The problem that i have is that because it is running in the same thread the window is not responding to the user.

The user is now able to minimize, move the window because the code is too
busy on it's own work.
(and they are all running on the same thread)

<snipped>

Hi,

Usualy there is only one UI thread and possible one or more worker thread.
The worker threads perform intensive work, they do not create windows, they
can update the UI.

It is however not safe to call form/control functions from another thread.
From a workerthread call form.Dispatch or control.Dispatch with an
appropriote delegate which performs the update. The code attached to the
delegate should contain some UI update code and will be executed in UI
thread.

This way the UI is only busy with updating some status, like a progressbar
or something like that.
HTH
greetings
Nov 15 '05 #2
> It is however not safe to call form/control functions from another thread.
From a workerthread call form.Dispatch or control.Dispatch with an
appropriote delegate which performs the update. The code attached to the
delegate should contain some UI update code and will be executed in UI
thread.


I didn't see any Dispatch method of a form. I see this is all proably
covered under multi-threaded asynchronous delegates. A classic use would be
to maintain a form responsiveness for example while a list box is being
populated with a big database search. If you could post some code from your
answer above that would be great. Or, point to some simple sample in the
documentation would really help my learning curve on this.

Nov 15 '05 #3
> I didn't see any Dispatch method of a form. I see this is all proably

My mistake, should be Invoke.
covered under multi-threaded asynchronous delegates. A classic use would be to maintain a form responsiveness for example while a list box is being
populated with a big database search. If you could post some code from your answer above that would be great. Or, point to some simple sample in the
documentation would really help my learning curve on this.


A very basic example, but it depends mostly on how much information you want
to exchange between threads and if the worker thread must be re-usable,
etc...

Form1
-------
+ has a listbox listBox1
+ has a button button1
private void button1_Click(object sender, System.EventArgs e)
{
FillLBThread fillLB = new FillLBThread(listBox1);
System.Threading.Thread tr = new System.Threading.Thread(
new System.Threading.ThreadStart(fillLB.Run) );
tr.Start();
}

FillLBThread
-------------
public class FillLBThread
{
private delegate void InsertLb (object item);
private System.Windows.Forms.ListBox lb_; //store reference

public FillLBThread(System.Windows.Forms.ListBox lb/*,other stuff...*/)
{
lb_ = lb;
/* other stuff eg. needed for calculation*/
}

public void Run ()
{
// so some lenghty operation, update the listbox with new items
for (int i=0; i<5000; ++i)
{
lb_.Invoke(
new InsertLb(InsertLbImpl),
new object[] { "Test" + i.ToString() } );
}
}

public void InsertLbImpl(object item)
{
// this will run in UI thread
lb_.Items.Add ( item );
lb_.Update()
}
}

you could also store a few items, and update the list every 10 or so,

HTH
greetings


Nov 15 '05 #4
Hi HTH

Very cool, thanks! Now when I get do the updating of that list box with
FillLBThread being on a different remote machine. I'll really feel like a
genius, errr I mean expert. I guess a web service comes but I think I'm
looking at all that component RealProxy business.

Thanks again,

Richard

Form1
-------
+ has a listbox listBox1
+ has a button button1
private void button1_Click(object sender, System.EventArgs e)
{
FillLBThread fillLB = new FillLBThread(listBox1);
System.Threading.Thread tr = new System.Threading.Thread(
new System.Threading.ThreadStart(fillLB.Run) );
tr.Start();
}

FillLBThread
-------------
public class FillLBThread
{
private delegate void InsertLb (object item);
private System.Windows.Forms.ListBox lb_; //store reference

public FillLBThread(System.Windows.Forms.ListBox lb/*,other stuff...*/) {
lb_ = lb;
/* other stuff eg. needed for calculation*/
}

public void Run ()
{
// so some lenghty operation, update the listbox with new items
for (int i=0; i<5000; ++i)
{
lb_.Invoke(
new InsertLb(InsertLbImpl),
new object[] { "Test" + i.ToString() } );
}
}

public void InsertLbImpl(object item)
{
// this will run in UI thread
lb_.Items.Add ( item );
lb_.Update()
}
}

you could also store a few items, and update the list every 10 or so,

HTH
greetings

Nov 15 '05 #5
thanks very much.
This confirms what i thought, the work should be done on a seperate
workerthread not on th emain thread.
"Empire City" <a@b.com> wrote in message
news:oh**********************@twister.nyc.rr.com.. .
Hi HTH

Very cool, thanks! Now when I get do the updating of that list box with
FillLBThread being on a different remote machine. I'll really feel like a
genius, errr I mean expert. I guess a web service comes but I think I'm
looking at all that component RealProxy business.

Thanks again,

Richard

Form1
-------
+ has a listbox listBox1
+ has a button button1
private void button1_Click(object sender, System.EventArgs e)
{
FillLBThread fillLB = new FillLBThread(listBox1);
System.Threading.Thread tr = new System.Threading.Thread(
new System.Threading.ThreadStart(fillLB.Run) );
tr.Start();
}

FillLBThread
-------------
public class FillLBThread
{
private delegate void InsertLb (object item);
private System.Windows.Forms.ListBox lb_; //store reference

public FillLBThread(System.Windows.Forms.ListBox lb/*,other

stuff...*/)
{
lb_ = lb;
/* other stuff eg. needed for calculation*/
}

public void Run ()
{
// so some lenghty operation, update the listbox with new items
for (int i=0; i<5000; ++i)
{
lb_.Invoke(
new InsertLb(InsertLbImpl),
new object[] { "Test" + i.ToString() } );
}
}

public void InsertLbImpl(object item)
{
// this will run in UI thread
lb_.Items.Add ( item );
lb_.Update()
}
}

you could also store a few items, and update the list every 10 or so,

HTH
greetings


Nov 15 '05 #6
thanks John.
An interesting article.

Also, FYI, I found another good article at http://www.vbcafe.com
it has 2 articles on threads which are very well explained.

Regards,

Serge

"Jhon" <Jh**@a.a> wrote in message
news:4Y********************@phobos.telenet-ops.be...
I didn't see any Dispatch method of a form. I see this is all proably
My mistake, should be Invoke.
covered under multi-threaded asynchronous delegates. A classic use would

be
to maintain a form responsiveness for example while a list box is being
populated with a big database search. If you could post some code from

your
answer above that would be great. Or, point to some simple sample in the
documentation would really help my learning curve on this.


A very basic example, but it depends mostly on how much information you

want to exchange between threads and if the worker thread must be re-usable,
etc...

Form1
-------
+ has a listbox listBox1
+ has a button button1
private void button1_Click(object sender, System.EventArgs e)
{
FillLBThread fillLB = new FillLBThread(listBox1);
System.Threading.Thread tr = new System.Threading.Thread(
new System.Threading.ThreadStart(fillLB.Run) );
tr.Start();
}

FillLBThread
-------------
public class FillLBThread
{
private delegate void InsertLb (object item);
private System.Windows.Forms.ListBox lb_; //store reference

public FillLBThread(System.Windows.Forms.ListBox lb/*,other stuff...*/) {
lb_ = lb;
/* other stuff eg. needed for calculation*/
}

public void Run ()
{
// so some lenghty operation, update the listbox with new items
for (int i=0; i<5000; ++i)
{
lb_.Invoke(
new InsertLb(InsertLbImpl),
new object[] { "Test" + i.ToString() } );
}
}

public void InsertLbImpl(object item)
{
// this will run in UI thread
lb_.Items.Add ( item );
lb_.Update()
}
}

you could also store a few items, and update the list every 10 or so,

HTH
greetings

Nov 15 '05 #7
wrong link
"Serge" <se**********@hotmail.com> wrote in message
news:3f***********************@reader1.news.skynet .be...
thanks John.
An interesting article.

Also, FYI, I found another good article at http://www.vbcafe.com
it has 2 articles on threads which are very well explained.

Regards,

Serge

"Jhon" <Jh**@a.a> wrote in message
news:4Y********************@phobos.telenet-ops.be...
I didn't see any Dispatch method of a form. I see this is all proably


My mistake, should be Invoke.
covered under multi-threaded asynchronous delegates. A classic use would
be
to maintain a form responsiveness for example while a list box is

being populated with a big database search. If you could post some code from

your
answer above that would be great. Or, point to some simple sample in the documentation would really help my learning curve on this.


A very basic example, but it depends mostly on how much information you

want
to exchange between threads and if the worker thread must be re-usable,
etc...

Form1
-------
+ has a listbox listBox1
+ has a button button1
private void button1_Click(object sender, System.EventArgs e)
{
FillLBThread fillLB = new FillLBThread(listBox1);
System.Threading.Thread tr = new System.Threading.Thread(
new System.Threading.ThreadStart(fillLB.Run) );
tr.Start();
}

FillLBThread
-------------
public class FillLBThread
{
private delegate void InsertLb (object item);
private System.Windows.Forms.ListBox lb_; //store reference

public FillLBThread(System.Windows.Forms.ListBox lb/*,other

stuff...*/)
{
lb_ = lb;
/* other stuff eg. needed for calculation*/
}

public void Run ()
{
// so some lenghty operation, update the listbox with new items
for (int i=0; i<5000; ++i)
{
lb_.Invoke(
new InsertLb(InsertLbImpl),
new object[] { "Test" + i.ToString() } );
}
}

public void InsertLbImpl(object item)
{
// this will run in UI thread
lb_.Items.Add ( item );
lb_.Update()
}
}

you could also store a few items, and update the list every 10 or so,

HTH
greetings


Nov 15 '05 #8
oeps .. sorry.
Here is the right link ...

http://www.kbcafe.com/articles.xml


"Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in
message news:O4**************@TK2MSFTNGP12.phx.gbl...
wrong link
"Serge" <se**********@hotmail.com> wrote in message
news:3f***********************@reader1.news.skynet .be...
thanks John.
An interesting article.

Also, FYI, I found another good article at http://www.vbcafe.com
it has 2 articles on threads which are very well explained.

Regards,

Serge

"Jhon" <Jh**@a.a> wrote in message
news:4Y********************@phobos.telenet-ops.be...
> I didn't see any Dispatch method of a form. I see this is all proably
My mistake, should be Invoke.

> covered under multi-threaded asynchronous delegates. A classic use would be
> to maintain a form responsiveness for example while a list box is being > populated with a big database search. If you could post some code from your
> answer above that would be great. Or, point to some simple sample in the > documentation would really help my learning curve on this.

A very basic example, but it depends mostly on how much information you
want
to exchange between threads and if the worker thread must be

re-usable, etc...

Form1
-------
+ has a listbox listBox1
+ has a button button1
private void button1_Click(object sender, System.EventArgs e)
{
FillLBThread fillLB = new FillLBThread(listBox1);
System.Threading.Thread tr = new System.Threading.Thread(
new System.Threading.ThreadStart(fillLB.Run) );
tr.Start();
}

FillLBThread
-------------
public class FillLBThread
{
private delegate void InsertLb (object item);
private System.Windows.Forms.ListBox lb_; //store reference

public FillLBThread(System.Windows.Forms.ListBox lb/*,other

stuff...*/)
{
lb_ = lb;
/* other stuff eg. needed for calculation*/
}

public void Run ()
{
// so some lenghty operation, update the listbox with new items for (int i=0; i<5000; ++i)
{
lb_.Invoke(
new InsertLb(InsertLbImpl),
new object[] { "Test" + i.ToString() } );
}
}

public void InsertLbImpl(object item)
{
// this will run in UI thread
lb_.Items.Add ( item );
lb_.Update()
}
}

you could also store a few items, and update the list every 10 or so,

HTH
greetings



Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Alex Hunsley | last post: by
4 posts views Thread by redneon | last post: by
2 posts views Thread by Bernie Yaeger | last post: by
4 posts views Thread by DBC User | last post: by
9 posts views Thread by anders | last post: by
5 posts views Thread by colin | last post: by
reply views Thread by =?Utf-8?B?QmFydE1hbg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.