473,804 Members | 3,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tread Problem


I hvae three thread and a thread that is used to show a winform.

The code:

Thread ThreadDialog= new Thread (new ThreadStart (ShowDialog));
//Show a winform Dialog
ThreadDialog.St art ();
Thread Thread1= new Thread (new ThreadStart (Thread1Func));
Thread1.Start ();
Thread Thread2= new Thread (new ThreadStart (Thread2Func));
Thread2.Start ();
Thread Thread3= new Thread (new ThreadStart (Thread3Func));
Thread3.Start ();
Now I want to close the dialog when the three thread all closed.How did I
do.
Jun 15 '07 #1
4 2519

"cyrous xiao" <cy*********@ma esinfo.com.cnwr ote in message
news:em******** ******@TK2MSFTN GP02.phx.gbl...
>
I hvae three thread and a thread that is used to show a winform.

The code:

Thread ThreadDialog= new Thread (new ThreadStart (ShowDialog));
//Show a winform Dialog
ThreadDialog.St art ();
Thread Thread1= new Thread (new ThreadStart (Thread1Func));
Thread1.Start ();
Thread Thread2= new Thread (new ThreadStart (Thread2Func));
Thread2.Start ();
Thread Thread3= new Thread (new ThreadStart (Thread3Func));
Thread3.Start ();
Now I want to close the dialog when the three thread all closed.How did I
do.

What are you talking about? Do want stop the threads when you exit the
program?

Jun 15 '07 #2

"Mr. Arnold" <MR. Ar****@Arnold.c om>
??????:u7****** ********@TK2MSF TNGP02.phx.gbl. ..
>
"cyrous xiao" <cy*********@ma esinfo.com.cnwr ote in message
news:em******** ******@TK2MSFTN GP02.phx.gbl...
>>
I hvae three thread and a thread that is used to show a winform.

The code:

Thread ThreadDialog= new Thread (new ThreadStart (ShowDialog));
//Show a winform Dialog
ThreadDialog.St art ();
Thread Thread1= new Thread (new ThreadStart (Thread1Func));
Thread1.Start ();
Thread Thread2= new Thread (new ThreadStart (Thread2Func));
Thread2.Start ();
Thread Thread3= new Thread (new ThreadStart (Thread3Func));
Thread3.Start ();
Now I want to close the dialog when the three thread all closed.How did I
do.


What are you talking about? Do want stop the threads when you exit the
program?
------------------------------------------------------------------------------------------------------------------------------------------

No. I want to close the dialog when the three thread finished thears job. It
is not stop the thread.
Jun 15 '07 #3
What you want to do is in your thread functions, call Invoke on the
dialog, passing a delegate to a method which you would use to indicate that
the thread is done. In that method, if all the threads are done, then you
would close the dialog.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"cyrous xiao" <cy*********@ma esinfo.com.cnwr ote in message
news:uL******** ******@TK2MSFTN GP02.phx.gbl...
>
"Mr. Arnold" <MR. Ar****@Arnold.c om>
??????:u7****** ********@TK2MSF TNGP02.phx.gbl. ..
>>
"cyrous xiao" <cy*********@ma esinfo.com.cnwr ote in message
news:em******* *******@TK2MSFT NGP02.phx.gbl.. .
>>>
I hvae three thread and a thread that is used to show a winform.

The code:

Thread ThreadDialog= new Thread (new ThreadStart (ShowDialog));
//Show a winform Dialog
ThreadDialog.St art ();
Thread Thread1= new Thread (new ThreadStart (Thread1Func));
Thread1.Start ();
Thread Thread2= new Thread (new ThreadStart (Thread2Func));
Thread2.Start ();
Thread Thread3= new Thread (new ThreadStart (Thread3Func));
Thread3.Start ();
Now I want to close the dialog when the three thread all closed.How did
I do.


What are you talking about? Do want stop the threads when you exit the
program?
------------------------------------------------------------------------------------------------------------------------------------------

No. I want to close the dialog when the three thread finished thears job.
It is not stop the thread.
Jun 15 '07 #4
On Thu, 14 Jun 2007 17:54:49 -0700, cyrous xiao
<cy*********@ma esinfo.com.cnwr ote:
[...]
Thread ThreadDialog= new Thread (new ThreadStart (ShowDialog));
//Show a winform Dialog
ThreadDialog.St art ();
Thread Thread1= new Thread (new ThreadStart (Thread1Func));
Thread1.Start ();
Thread Thread2= new Thread (new ThreadStart (Thread2Func));
Thread2.Start ();
Thread Thread3= new Thread (new ThreadStart (Thread3Func));
Thread3.Start ();
Now I want to close the dialog when the three thread all closed.How did I
do.
Well, the first step is to not show your dialog in a new thread. Keep all
your form instances in the same thread, mainly because it's simpler and
there's no good reason to do it otherwise.

Then you need a way for each of the three threads to signal their
completion to the form you've shown. There are a variety of ways to do
this, but it seems to me that a reasonable way is to maintain a counter
representing the threads and have each thread decrement the counter. If
and when it reaches 0, then close the form (warning: uncompiled code,
could have errors, but hopefully you get the basic idea):

class MyDialog : Form
{
private int _cthread;

protected override void OnLoad(object sender, EventArgs a)
{
InitThreads();
}

// Here we initialize our counter and start our threads
void InitThreads()
{
_cthread = 3;

(new Thread(new ThreadStart(Thr ead1Func))).Sta rt();
(new Thread(new ThreadStart(Thr ead2Func))).Sta rt();
(new Thread(new ThreadStart(Thr ead3Func))).Sta rt();
}

// This is the method that each thread uses Invoke() with to
decrement
// the counter. When the counter reaches 0, that's the last
thread and
// it's time to close the form.
void ThreadFinished( )
{
if (--_cthread == 0)
{
Close();
}
}

void Thread1Func()
{
// Do some work

// Here's the magic:
// The Control.Invoke( ) method causes a delegate to be
executed on
// the same thread that owns the Control instance (this
form, in this
// case). This has a couple of advantages for this
purpose: first,
// access to the _cthread field is restricted to a single
thread, allowing
// synchronized access; also, it allows the thread thatis
the last to
// finish to actually close the the form (the Close()
method must be
// called from within the thread that owns the Control)..

Invoke(new MethodInvoker(T hreadFinished)) ;
}

// Thread2Func and Thread3Func are implemented similarly
}

Then in whatever code, you just have to display the dialog:

MyDialog dlg = new MyDialog();

dlg.ShowDialog( );

Note: the above code puts the thread control code into the dialog class.
It's nice to put it there so that the presentation of the dialog is
synchronized with the execution of the threads. Because Form.ShowDialog ()
doesn't return until the dialog has been closed, you can't start the
threads after calling ShowDialog(). But if you start the threads before
calling ShowDialog() there's a risk that one or more threads might
complete before the dialog even is shown (a very slight risk, to be sure,
and maybe not possible in practice, but even a theoretical possibility is
worth designing for). IMHO, the most straight-forward way to deal with
this is to just put the thread initialization into the dialog itself.

Hope that helps.

Pete
Jun 15 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3079
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in the context of a main window. The problem does not occur on HPUX or Linux. The following simple example code illustrates the problem and the work around I've come up with; However, I'd like, very much, to get rid of the kludgy work around....
11
3766
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class (ClassB* b; b->func(); ) the base-class function is called instead of the new function in the derived class. All other similar functions (virtual in the base class and overwritten in the the derived class) work fine, it's just this one function. ...
0
2026
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent users So I designed the project as master Node that has all administration
117
7277
by: Peter Olcott | last post by:
www.halting-problem.com
28
5228
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass(); .... and then call the virtual method, why is it that the base class's method is called instead of the overridden method? How do I fix this if I don't know at runtime what the child class is? I'm using Activator.CreateInstance() to load the...
6
3818
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length is more that 1249 bytes, then the progress bar of the browser will move too slow and then displaying that the page not found!!!! If the message is less than or equal to 1249 then no problem.
9
1245
by: Li Pang | last post by:
Hi, I have two buttons, one is to start a process, another is to cancel the process. Wehn I canceled a process, there is an alert message popped up. I'd like to know how to avoid this popup. thanks in advance Private Shared tdSearch As Thread
16
4932
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by Microsoft must be installed on their servers. Now german Umlaute (ä, ü, ö) and quotes are returned incorrectly in SOAP fault responses. This can be easily verified: Implement the following in a web service method (just raises a SOAPException with a...
2
4558
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was set to...it is set to false. Can someone show me what I am doing wrong and tell me the correct way? Thank you. In the page load event, I am doing the following:
5
2304
by: Ryan Liu | last post by:
Hi, I read Microsoft SDK, ms-help://MS.NETFrameworkSDKv1.1/cpguidenf/html/cpovrasynchronousprogramming overview.htm there are 4 ways to call EndInvoke: The code in this topic demonstrates four common ways to use BeginInvoke and EndInvoke to make asynchronous calls. After calling BeginInvoke you can:
0
9707
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
10586
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
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
9161
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
7622
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
5525
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
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3823
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.