473,698 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 CreateStatusFor m that creates the
form and assigns it to my variable _frmStatus, and my code running this
method in a seperate thread:

private void CreateStatusFor m()
{
_frmStatus = new AdwareForm();
fInitializedSta tus = true;
}

private AdwareTrace()
{
_oFormThread = new Thread(new ThreadStart(thi s.CreateStatusF orm));
_oFormThread.Na me = "Thread: StatusForm";
_oFormThread.St art();

while (oFormThread.Is Alive)
{
Thread.Sleep(25 0);
}
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 3319
"Serge" <se**********@h otmail.com> wrote in message
news:3f******** *************** @reader1.news.s kynet.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.Dispatc h 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.Dispatc h 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(o bject sender, System.EventArg s e)
{
FillLBThread fillLB = new FillLBThread(li stBox1);
System.Threadin g.Thread tr = new System.Threadin g.Thread(
new System.Threadin g.ThreadStart(f illLB.Run) );
tr.Start();
}

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

public FillLBThread(Sy stem.Windows.Fo rms.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(Insert LbImpl),
new object[] { "Test" + i.ToString() } );
}
}

public void InsertLbImpl(ob ject 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(o bject sender, System.EventArg s e)
{
FillLBThread fillLB = new FillLBThread(li stBox1);
System.Threadin g.Thread tr = new System.Threadin g.Thread(
new System.Threadin g.ThreadStart(f illLB.Run) );
tr.Start();
}

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

public FillLBThread(Sy stem.Windows.Fo rms.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(Insert LbImpl),
new object[] { "Test" + i.ToString() } );
}
}

public void InsertLbImpl(ob ject 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(o bject sender, System.EventArg s e)
{
FillLBThread fillLB = new FillLBThread(li stBox1);
System.Threadin g.Thread tr = new System.Threadin g.Thread(
new System.Threadin g.ThreadStart(f illLB.Run) );
tr.Start();
}

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

public FillLBThread(Sy stem.Windows.Fo rms.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(Insert LbImpl),
new object[] { "Test" + i.ToString() } );
}
}

public void InsertLbImpl(ob ject 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******** ************@ph obos.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(o bject sender, System.EventArg s e)
{
FillLBThread fillLB = new FillLBThread(li stBox1);
System.Threadin g.Thread tr = new System.Threadin g.Thread(
new System.Threadin g.ThreadStart(f illLB.Run) );
tr.Start();
}

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

public FillLBThread(Sy stem.Windows.Fo rms.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(Insert LbImpl),
new object[] { "Test" + i.ToString() } );
}
}

public void InsertLbImpl(ob ject 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**********@h otmail.com> wrote in message
news:3f******** *************** @reader1.news.s kynet.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******** ************@ph obos.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(o bject sender, System.EventArg s e)
{
FillLBThread fillLB = new FillLBThread(li stBox1);
System.Threadin g.Thread tr = new System.Threadin g.Thread(
new System.Threadin g.ThreadStart(f illLB.Run) );
tr.Start();
}

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

public FillLBThread(Sy stem.Windows.Fo rms.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(Insert LbImpl),
new object[] { "Test" + i.ToString() } );
}
}

public void InsertLbImpl(ob ject 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******** ******@TK2MSFTN GP12.phx.gbl...
wrong link
"Serge" <se**********@h otmail.com> wrote in message
news:3f******** *************** @reader1.news.s kynet.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******** ************@ph obos.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(o bject sender, System.EventArg s e)
{
FillLBThread fillLB = new FillLBThread(li stBox1);
System.Threadin g.Thread tr = new System.Threadin g.Thread(
new System.Threadin g.ThreadStart(f illLB.Run) );
tr.Start();
}

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

public FillLBThread(Sy stem.Windows.Fo rms.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(Insert LbImpl),
new object[] { "Test" + i.ToString() } );
}
}

public void InsertLbImpl(ob ject 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
7978
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 : VBScript and Javascript Client : 1. IE browser. 2. VBForm embedded with WebBrowser control (MS Internet
7
1731
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 containing the form, and when the user makes a selection and hits an 'ok' button in the pop-up, I'd like their selectiont to be inserted into a field on a form in the main window. Is this possible? thanks alex
4
3364
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)); timerThread.Start(); .... private void startTimer() {
2
1145
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 event. The calling window itself is a child window in an mdi app. I'm having a terrible time trying to do this, as the called window never fully opens unless it's called via showdialog, but this is not what I want, because I want it to close...
4
1925
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 good approach? I did the following at present and is not working. In the main form (CAB project) right before base.run(), I started a seperate thread and launched a windows form and it did show up (in the task bar not the window though) and closes...
9
12299
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 messages between the main thread and the gui thread). I'm new to pyqt, so I'm probably doing something very silly, but for the moment I'm just trying to get the first pyqt tutorial example running in a seperate thread: ----8<--------
10
2727
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
5348
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, such as a Form type window wich displays lists of numerical details about objects in the 3d view and allows objects to be selected from a list as an alternative to selecting them on the 3d view as they maybe underneath another object.
0
1358
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 assemblies and initialization is done in a seperate thread from the UI, to keep the UI from being locked up. Unfortuately 1 of the plug in assemblies provides a interface to a 3rd party
0
8680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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 we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.