473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 3296
"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 : Intranet Server : IIS and ASP. Script :...
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 pop up a helper window seperate from the page...
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 timerThread = new Thread(new ThreadStart(startTimer));...
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 loading a large datatable before it completes its load...
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 the program is loading..) Can someone suggest a...
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 the gui is running (later i will want to pass...
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. however I need to use some other windows too,...
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 prompts the manager. This loading of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.