how to run a window on a seperate thread

  • Thread starter Thread starter Serge
  • Start date Start date
S

Serge

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
 
Serge said:
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
 
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.
 
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
 
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
 
thanks very much.
This confirms what i thought, the work should be done on a seperate
workerthread not on th emain thread.


Empire City said:
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
 
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 said:
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
 
wrong link
Serge said:
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 said:
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
 
oeps .. sorry.
Here is the right link ...

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




Alvin Bruney said:
wrong link
Serge said:
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 said:
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
 
Back
Top