472,328 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 3235
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vetrivel | last post by:
Application architecture : Develop interface between two existing systems, a. Enterprise CRM system b. Web based intranet system. Environment :...
7
by: Alex Hunsley | last post by:
I'm aware of how to access the document model in Javascript in order to do things like change the values in the fields forms. However, I'd like to...
4
by: redneon | last post by:
I've set up a seperate thread and put a timer in it but for some reason it's tick event is never fired. This is what I have... .... Thread...
2
by: Bernie Yaeger | last post by:
I want to open a window (the called window) - and perhaps run a progressbar, etc - from a calling window's load event. The calling window is...
4
by: DBC User | last post by:
Hi, I have windows form program. While that form is building to show, I want to show another window (kinda splash screen so I can show the user...
9
by: anders | last post by:
I am writing a plugin for a piece of software in python, and I want to start up a PyQt GUI in the plugin, without stalling the main thread while...
10
by: Bill Nguyen | last post by:
I would like to be able to get an active browser window to refresh the URL (reload) every 5 minutes. Is it possible in VB.NET? Thanks Bill
5
by: colin | last post by:
Hi, Ive got a 3d model editor wich im developing with XNA c# development environment, using the game window to display the wireframe mesh in 3d....
0
by: =?Utf-8?B?QmFydE1hbg==?= | last post by:
Greetings, To begin with we have an archetecture which has a manager Asssembly which loads "Plug-in" assemblies using reflection, when a the UI...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.