Ok I'm creating an application with a plugin architecture and the application would be able to run without a GUI in other words the GUI is really optional... and if the user decides to use the GUI the console is just hidden.
I can create the form in the console by calling one of the plugins method, but as soon the Window is created the console program keeps waiting until the window is closed... is there a way to create the form so that the console doesn't have to wait, it should keep working on it's own stuff and just notify the gui with some info?
Why don't you keep the windows forms application as a seperate executable and call process.start() ?
For example
Process.Start("c:\yourwindowsformsapplication.exe");
You can quit your console application or continue other work within the console application once you start the windowsapplication.exe.
And use remoting to communicate between both the applications.
or....
Create a new thread and call your form.show() within a new thread
Example :
Form frm=new form();
Thread th=new Thread(frm.show);
th.start();
Are you creating and then showing the form using ShowDialog()? If so, that's why the console app is waiting for the form to close. Try showing the form using Show() instead - Show() is a non-blocking call that will return program execution to the next line.
Related
Discovering Winforms, I was wondering why code written in the Main(), after the function that launches the form (Application.Run(new Form1());), is never reached while Form is running ?
Does a Winform necessarily need threads (backgroundWorker, etc...) to execute functions outside of UI event handlers ? Why can't we simply write these functions in the Main(), instead of having to create threads ?
A Winform is "only" some event handlers, so why does it "block" the Main() ?
Thank you for your enlightenment !
Application.Run() starts a message pump to process the Windows API that displays forms on the screen and processes windows messages, executing the UI event handlers. You can think of this message pump as loop that never returns until the application is exited. Thus, code following Application.Run() will not execute until your main form is closed.
I created app that open every time that I start pc. So its so annoying to close it every time so I'm wondering if its some code that will hide my console app. I saw videos and tutorials on forms but idk how to do it with console app.
The easiest way to do this is change your console app to a windows app.
Console apps get a console made for them by Windows. But if you change it to a windows forms app, then windows expect the application to make a window, so if you never make a window, then it will never show
The other way is to turn your application into a service. This has some additional requirements in terms of programming
Option 1
You can use this run command:
start /min "SomeTitle" MyConsoleApp.exe myarg1 myarg2
Thus it will be on the taskbar minimized.
Option 2
If you use a file link in the start menu, select the start minimized option for the exe.
Option 3
Using a WinForm app you will be able to use a tray icon by setting the main form as not visible, to say it simply because it can be complex according to the expected behavior, and it will not be in the taskbar too.
Option 4
If you don't want a main form, create a win form app, delete the form file and the code in the main method, and you're done, without GUI nor console, no main input and no main output but you can show MessageBox and some forms when necessary, just a background process only visible in the Task Manager.
With that you can add a tray icon to to offer exit and some status information for example.
Option 5
Also you can create a windows service:
.NET console application as Windows service
Note
In all cases, if you don't use an internal message events dispatcher like the WinForms Application pattern or WPF and so on, be carefull to not saturate the CPU with the processings like with loops and use Thread.Sleep() between iterations or any thread idleing pattern or some timer if necessary.
i have a console application project. it has gui (winforms) and console window which is used as debug console (application's run log).
now, if i have got debug disabled i hide console window with ShowWindow native call.
the problem is: after i close application window by pressin the X button or by quitting it (Application.Exit()) i have a zombie console process.
if i close console on the very begining by Environment.Exit(0) the form is not been showed.
i have tried to modify forms Closed event by adding there Environment.Exit(0), but that did not work which means that there still remains a zombie console process.
how can i close console window on application exit?
If it is a console application, the console will not "close" until the process terminates. So you just simply need to close the form normally... assuming you "opened" it like you should.
To "open" the form, you need to run it. Run it by calling Application.Run(Form) (the same way you would initialize an actual form project). Since a console is allocated to your process, your form may write to Console.Out and you should see what you write.
we have this application which uses cross app domain (2 app domains in the same process).
we need to mimic the Dialog/Model window, which will wait for the result from the 2nd app domain before it can continue further. 2nd App Domain loads up WPF form (while 1st app domain is still on .Net 2 forms). we will have to use this Plugins approach so that we can leverage our new WPF without breaking our old app.
at the moment I am using ManualEventReset to singal when the 2nd app domain is done, but this is freezing up the GUI so that when I move the Dialog/Model window, it is not repainting the background. Only happens on Windows XP (Windows 7 works fine)
I was wondering if there is a way to implement Model window so that it will still allow messages to go through so that background can repaint itself. Let me know if you need more specifics
you could open a regular modal form, that immediately hides itself and open the desired winforms form on another thread ... so you can "deadlock" one thread until your operation is complete without blocking the message processing of your UI ... to exit the modal state after your locked thread is released, invoke your hidden forms close() (Invoke() call to your UI thread)
I have created 2 windows form applications in a c# program. I did this:
Application.Run(new Form1());
Application.Run(new Form2());
The second form is not loading.Why? how can i resolve this problem? i would like to activate both these forms in the same time. Each of this form application loades a different method. I would like to somehow create a paralel process for each of these forms.
solution and problem:
i've created 2 threads - for the 2 forms. This forms call the parent form. The thing is that the parent form has a vector in which i would like to continuously add elements from the form. I've noticed that the vector is reinitialized every time the thread is reactivated. how to change this thing?
Application.Run is used to start your application and indicate which window should be loaded on application start (i.e. which window should be the main window). To display second form you need to create it and show it by hand, not using Application.Run.
var form = new Form2();
form.Show();
This code can be placed in Form1's constructor or in response to some event. It may not be created in the same method, where you call Application.Run, because this method will not run further, until you will close Form1 window. Probably Forms1's constructor is best place to do this.
Because Application.Run is blocking. It's running the application on the current thread. You'd have to start 2 separate threads with one of the Application.Run commands in each of them.
The best course of action would be to open one of the two in a separate thread.
Application.Run(new Form1());
This will invoke Form1 and it will be waiting till this form is closed. After that the second Run method will be invoked.
Application.Run is synchronous method,
See adrianbanks' answer here:
The recommended way to start WinForms applications is using Application.Run, but I suspect this is more of a convention than a rule. The biggest reason to use Application.Run is if you want to open multiple non-modal forms. You can do this using:
new Form().Show();
new Form().Show();
Application.Run();
I'm really can't understand what you are trying to accomplish, but close first form and the second will displayed :-)
You didnt see the second one because of Run method it didnt return control till you close working object at your case this is Forms1