473,461 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is it possible to end the Main() thread while the other threads continue?

Hello,

I would like the Main()-thread to end (because it runs out of the
code), but all started threads should continue. Is this possible.

Eg.

[STAThread]
static void Main(string[] args)
{
System.Threading.Thread threadGUI = new
System.Threading.Thread(new
System.Threading.ThreadStart(Recdump.StartGUI));
threadGUI.Start();
}

So, after I run the program, the thread running Main() should end, but
the thread running Recdump.StartGUI should continue. Is that possible?

Actually, what I really want is an application which initially can
write to the Console, but after a while, when a form has been started,
returns to the command prompt.

I hope somebody can help...

Kind regards,

Edwin.

Nov 17 '05 #1
7 4224
On 5 Nov 2005 13:17:51 -0800, "Edwin" <te*************@hotmail.com>
wrote:
Hello,

I would like the Main()-thread to end (because it runs out of the
code), but all started threads should continue. Is this possible.

Eg.

[STAThread]
static void Main(string[] args)
{
System.Threading.Thread threadGUI = new
System.Threading.Thread(new
System.Threading.ThreadStart(Recdump.StartGUI)) ;
Try threadGUI.IsBackground = false;

threadGUI.Start();
}

So, after I run the program, the thread running Main() should end, but
the thread running Recdump.StartGUI should continue. Is that possible?

Actually, what I really want is an application which initially can
write to the Console, but after a while, when a form has been started,
returns to the command prompt.


--
Marcus Andrén
Nov 17 '05 #2
On Sun, 06 Nov 2005 01:37:18 +0100, Marcus Andrén <a@b.c> wrote:

Try threadGUI.IsBackground = false;


Began to doubt myself after I wrote this. While this is the correct
way to keep a program running when the main thread terminates, it
won't help you.

The program will keep running, but the problem for you is that as long
as the program is running it will keep blocking the console window.

What you want is to disconnect the process from the console window. I
don't know if there exists a way to do that.

--
Marcus Andrén
Nov 17 '05 #3
The main thread wont terminate cuz there are child threads running.
Im not pretty sure wat ya want to do. I put some sample code here which i
think it might help ya a bit since the gui and console running at the same
time.

[STAThread]
static void Main(string[] args) {
System.Threading.Thread gui = new System.Threading.Thread(new
System.Threading.ThreadStart(MainForm.Start));
gui.Start();
CommandProcessor.Processor.Execute();
}

//MainForm GUI procedure
public class MainForm : System.Windows.Forms.Form {
public static void Start() {
MainForm gui = new MainForm ();
Application.Run(gui);
}
//...
}

//console class
public class CommandProcessor {

static CommandProcessor processor = null;

static CommandProcessor() {
processor = new CommandProcessor();
}

public static CommandProcessor Processor {
get {
return processor;
}
}

public void Execute() {
string cmd = null;
do {
cmd = Console.ReadLine();
Console.WriteLine("Command:" + cmd);
} while ("EXIT".CompareTo(cmd.ToUpper()) != 0);
}

}
Nov 17 '05 #4
> The main thread wont terminate cuz there are child threads running.
Im not pretty sure wat ya want to do.


Actually, that is just what I want: terminating the main thread without
terminating the child thread. The main thread should do some simple
stuff (including writing some basic info to the console), then spawn a
Windows Forms GUI and end, so when starting from the commandline,
you'll get the DOS-prompt back after the GUI has started :-)

Nov 17 '05 #5

Sounds like two assemblies (applications) to me. One is your gui.exe
and one is your starter.exe that uses Process.Start to open gui.exe

Not sure how else to do it...

Brian
Edwin wrote:
The main thread wont terminate cuz there are child threads running.
Im not pretty sure wat ya want to do.

Actually, that is just what I want: terminating the main thread without
terminating the child thread. The main thread should do some simple
stuff (including writing some basic info to the console), then spawn a
Windows Forms GUI and end, so when starting from the commandline,
you'll get the DOS-prompt back after the GUI has started :-)

Nov 17 '05 #6
Edwin <te*************@hotmail.com> wrote:
The main thread wont terminate cuz there are child threads running.
Im not pretty sure wat ya want to do.


Actually, that is just what I want: terminating the main thread without
terminating the child thread. The main thread should do some simple
stuff (including writing some basic info to the console), then spawn a
Windows Forms GUI and end, so when starting from the commandline,
you'll get the DOS-prompt back after the GUI has started :-)


You only get the DOS-prompt back when the *process* has terminated, not
when the main thread has finished. As Brian says, you'll need two
processes, or you'll need to detach the console (there are samples
around to do this, but I haven't done it myself).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
Thanks for the replies...

I found a partial solution to solve this, by detaching the
Console-window.

When you compile an executable as a command-line program, you can
include the following in case the GUI should start:

Console.WriteLine("No parameters given. Starting up GUI...");
FreeConsole();
System.Windows.Forms.Application.Run(new GUI());

Thus the FreeConsole() is the one that does the trick. This is a
kernel32.dll function, so you should include the following in the
source:

[System.Runtime.InteropServices.DllImport("kernel32 .dll")]
public static extern int FreeConsole();

Unfortunately, it doesn't do everything I want, but it comes close :-)
* When dubbelclicking the executable, the DOS-windows (which opens
first) closes, so the GUI remains. This is good
* When starting the exe by means of start (start myprogram.exe), it
works the same, so this is good to :-)
* But when running it from the command-line directly (myprogram.exe),
the DOS-window freezes until the GUI exits...

If I have some spare time, I'll look into that last one...

Kind regards,

Edwin van de Burgt.

Nov 17 '05 #8

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

Similar topics

2
by: Michel Schilthuizen | last post by:
Hi, I am working on an application that can use functionlity in some sort of plugins. I have implemented this by using Assembly.LoadFrom and MethodInfo.GetMethods. The calling of the plugins...
7
by: David Elliott | last post by:
I have created an application that will dynamically load other DLLs (plugins). The new plugin is a winform with an embedded IE Browser. I am wanting to have the form to run in its own thread....
11
by: Daniel Billingsley | last post by:
Let's say I'm writing a business app and I want there to be only one instance of the Customer object for each particular customer (representing a database record) being edited. Would it be...
3
by: Kathy Burke | last post by:
Hi, I'm tired, so this question may be silly. I have a fairly long sub procedure. Based on one condition, I load another sub with the following: If Session("GRN") = "complete" Then txtScan.Text...
4
by: Charles Law | last post by:
Hi guys. I have two threads: a main thread and a background thread. Lots of stuff happens in the background thread that means I have to update several (lots) of controls on a form. It is...
10
by: Marty | last post by:
Hi, Does anybody is experiencing a lot of RAM consumption when using many threads ? If yes, how can we reduce that level of used memory? Thanks tou! Marty
11
by: Daniel | last post by:
Hi all I have written a app that gets emails from a DB and checks if it's a correct email (connects to the remote mailserver and checks the response). I would like to improve the performance...
0
by: Lloyd Zusman | last post by:
I have a python-2.5 program running under linux in which I spawn a number of threads. The main thread does nothing while these subsidiary threads are running, and after they all complete, the main...
4
by: Marcus Alves Grando | last post by:
Hello list, I have a strange problem with os.walk and threads in python script. I have one script that create some threads and consume Queue. For every value in Queue this script run os.walk()...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
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,...
0
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...

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.