c# windows form app - c#

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

Related

How do I run other windows forms while enabling a reference? Once I connect my reference, the application gets stuck on only one form

visual studio problem
I am trying to run different windows forms but once I enable my reference to TrackerLibrary, my Application.Run gets stuck on only being able to run the last form it was on before enabling my reference. Is there some way to fix this? (new to coding so I don't know if I'm missing anything)
The Application.Run method is what starts your application and the program will not go past that method call unless the user exits the application.
Using Application.Run(Form mainForm) will only allow you to start your application with 1 open form and the application will exit when that form is closed.
If you'd like to open more than 1 Form on start, consider using Application.Run(ApplicationContext context).
Application.Run Method documentation
Your application doesn't get "stuck on one form". You can open as many other forms as you want. What you can't do is close that first form without the application exiting. When you call Application.Run and pass a form, that method will return if and only if that form closes, at which point your Main method will also complete, if you have no more code after the Application.Run call.
If you want to be able to close a form without the application exiting then you cannot pass that form to Application.Run. What you can do instead is derive your own class from ApplicationContext and then create an instance of that class and pass it to Application.Run instead. You can then put whatever logic you want in that class to open and close as many forms as you like.
I'm not going to provide any code here because I don't know specifically what it is you want to do. Better that you follow the link above and work through the example it provides in order to understand the concept, then implement it as you need. You might also like to check out my own example of using a custom ApplicationContext here.

How to close the app when I make click on X button in a child form?

I have a form app with two Forms. In the second form I have in the right corner the x button. How I can make when I make click on this button to close the app, not just hide the Form2 window?
First you need to catch the event. To do that, set an event handler on the child form's FormClosing event.
Then there are several options:
"Brute force" termination using Process.Kill().
This will terminate the process without letting any cleanup code to run. It has an effect like ending a process through the task manager. You can get the current process with Process.GetCurrentProcess. Use like this:
Process.GetCurrentProcess().Kill();
"Gentle" termination by way of closing all windows using Application.Exit().
This will close all message pumps and windows, but will do so while allowing normal cleanup code to run. It does not however guarantee the process will be terminated, for example if a forgound thread is still active after message loops are done. Use like this:
Application.Exit();
Communicate intentions to the main thread.
This is a design solution, not a "line of code" you put somewhere. The idea is that the 2 classes (of the 2 forms) have some communication mechanism (via message, events or whatever you see fit and probably already use), and the child form notifies the parent form the user wants the exit the application. Then it's up to the main form to cleanup everything, close all forms (itself and others), and exit the process normally. This is the cleanest and preferred method, but requires a proper design and more code.
If you are using form the simplest is using
this.Close(); or Application.Exit();

How I can close a 1st form without closing the full application? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I close a login form and show the main form without my application closing?
How i can close a 1st form with out closing full the application?
C#. as a example if my 1st form is logging form after enter username and password if they are correct that form should be close and other form need to be open.
I tried this.close() but my full application is exiting :(. After that I tried this.Hide() it works but my form is still running just hidden from UI.
ty
It is actually quite simple. On your Program.cs Main static method use the following code to launch your LoginForm.
var myLoginForm = new LoginForm();
myLoginForm.Show();
Application.Run();
Then from your LoginForm, simply remember to launch your next Form before closing it like this:
var myNextForm = new NextForm();
myNextForm .Show();
this.Close();
If you rather just want to close the application after login fails just do the following:
this.close();
Application.Exit();
Hope it helps!
In your Program.cs, you should have it create an instance of the form you wish to be long running instead of just the login form.
You can then have the other form display the login form to get that information from the user and when it exists you'll be back to your other form.
That is because the window you are closing is the main application's window. Closing that window will shut down the whole application.
Now here is the fix:
Create a second Form, LoginForm for example
On the main form load event handler (add one if there isn't any), show the LoginForm to ask the user to enter his/her credentials.
Once you check those credentials you can either return from the event handler if the credentials are good and in this case the main window will show up and the application will run normally or close the main window and the whole application will shut down.
When working with forms, what you have to realize is that calling Application.Run() just switches the applications main thread to the forms UI thread. When the form calls Close(), it will finish up whatever processing it needs to do and continues from the Application.Run() call. What you can do after this is run another Application.Run() on the next form if some property of your login form is set (frmLogin.LoginSuccessful or some such).
However, this is not best practice (IMO). What you should do is instead have the login form opened from within your main form, and then the main form will continue to run after the login form closes.

When using form.ShowDialog() a thread in mainform doesn't work

I have a form I'm showing using form.showDialog() and in the main form I have a thread that is sending a message every x seconds. I notice that while I'm in the window that I just opened the new form that I opened using ShowDialog the thread doesn't run. How can I make it continue running even when a ShowDialog is used?
Code:
codeshowAllScriptsWindow window = new showAllScriptsWindow(this);
window.Show();
and in the mainform I have a thread that keeps sending a message but is stopped because of this showdialog. Note that when I use show() it doesn't happen.
ShowDialog() is a blocking call. The thread actually runs, it is busy pumping the message loop for the dialog. This is no different from what happens on the main thread of your program.
Doing this is very unwise, the dialog has no Z-order relationship with the rest of the windows in your app. One classic mishap is that it can disappear behind another window but no good way for the user to find it back. Use Control.BeginInvoke to create the dialog on the UI thread instead. This also ensures that your thread keeps 'running'.
The message pump is "stolen" by your modal dialog you are displaying.
You have 2 options:
Don't use modal dialogs (use formShow())
Use a new thread to do the work and use the main thread for UI.
Although it seems unrelated at first glance, you might take a look at this question. The problem you're having is that the form.showDialog() call creates a modal dialog, halting the progression of code on that thread. If you spin another thread and fire that call there, your first thread will continue running as the dialog displays.
Use a System.Threading.Timer not a System.Windows.Form.Timer.

Can we and how to open a new window and closes the previous window in the same thread?

I am asking this, because i want to know if when we are running an app, for start if we have an window to authenticate like a Log In window, after validating the user, can we open the Main Window in the same Thread without creating a new one?
I am trying to do this in WPF, but i think that is same thing in WPF or in Windows Forms.
Yes, you can.
Just do it.
When you generate a Windows Forms application via the IDE, it will generate the code for one form, as well as a Main function that displays the form at runtime. You can rewrite the Main method so it displays one form modally then displays the next form.
But there's a simpler way to achieve your objectives:
Have two windows: your Main window, where most of the work is done, and the login screen.
In the OnLoad event of your main window, create an instance of your login window and call ShowModal() on this instance.
If the login fails, then exit the application.
This question does not offer enough context to tell you how to do this in your specific case. In general you can just Close() a window, construct a new one and call Show() on it.
You should make sure the Application.ShutdownMode does not kill off your application when the window is closed though.

Categories

Resources