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

E

Edwin

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.
 
M

Marcus Andrén

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;
 
M

Marcus Andrén

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.
 
G

Guest

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);
}

}
 
E

Edwin

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 :)
 
B

Brian Pelton

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
 
J

Jon Skeet [C# MVP]

Edwin said:
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).
 
E

Edwin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top