Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am trying to create a multi-thread application in c# where each thread has its own Window, so it can be used to interact with each of these threads. The interaction can be either via a Console window or a Dialog box.

What I have tried:

I tried adding Console.WriteLine and Console.ReadLine to each thread's function. Probably not the best approach...
Posted
Updated 27-Jun-20 4:08am
Comments
[no name] 27-Jun-20 10:42am    
Have a look here: Creating Windows in Threads - Win32 apps | Microsoft Docs[^] uups, that is W32, sorry
Dave Kreskowiak 27-Jun-20 10:56am    
An app can only ever have a single console attached to it, so you're idea (i think!) of having a console per thread isn't going to work.

It sounds like you're going to need to complete redesign your app.
Michael Haephrati 27-Jun-20 15:05pm    
Dave, yes. That's my idea but had it been a Win32 c++ program, I would have been able to add my own Console window for each thread. You can even add a Console window to a Dialog based application. I don't know the answer when it comes to c#.
Dave Kreskowiak 27-Jun-20 18:23pm    
Actually, no you can't. Not in the same process anyway. Windows prevents this.

You can, even in C#, however, create another "helper app", that just exposes a named pipe and outputs to its own console window. You can then launch an many of these helpers as you want, communicating with each through the pipe, telling them what to display in their consoles.

From the docs on AllocConsole:
A process can be associated with only one console, so the AllocConsole function fails if the calling process already has a console.
Michael Haephrati 27-Jun-20 18:33pm    
So my code https://www.codeproject.com/Articles/3135812/Creating-a-Console-Based-Window-for-Debugging won't work even from a Dialog Box application with several threads?

1 solution

Pretty much, you can't - or at least not directly. The problem is that Threads don't have windows, and there is only one UI thread in an application. Once you get to Windows rather than using a Console, only that single UI thread can access any controls, so it gets complicated to update windows from secondary threads (you have to move the access code back to the UI thread by a process called Invoking).

However, there is one way to do it quite easily: Use a BackgroundWorker [^] to run each thread, and associate that worker with a particular Window. Background workers have an event for reporting progress which allows you to pass info from the worker thread to the UI thread for update, and if the worker is "fired" by the Form itself, then the Progress event will get the form to display on as this in the usual way.

Be aware that increasing the number of threads may not speed up an application, quite the contrary it is possible to slow down an app dramatically by adding too many threads!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900