How can I control windows form class in the main loop?
Can I still run my code in the main loop like a console program but use windows form to display stuff? I already done that I've got windows form application and console in one but once main calls windows form that's it, it doesn't come back to main.
Can I pass variables to windows form?
You can't.
When you call Application.Run, the method only returns when there is no active Form any more. This is because Application.Run keeps the message loop running. It blocks the current method form returning. See it as a while (mainFormActive). It will never end unless the main form is closed.
If you want to execute code in the mean time, set up a Task, a Thread or similar, before you start the form.
And yes, you can pass variables in, just like in any constructor.
Related
If you are building a windows forms application in C# and whenever the user opens a form from another form, the first form becomes hidden, how would you get the application quit if all forms were hidden?
I would have though of having instances of all the forms in a static class and have the application loop through the status of all the forms each time the user hides a form and if all are hidden then exit the application. Is this a good way to do it?
Usually in this situation you would create a ApplicationContext and you would pass that in to Application.Run( instead of a specific form.
If you look at the MSDN page for ApplicationContext it has a example of starting two forms then only closing the program when both forms get closed. You can make your own program use whatever logic you want, for example using hiding instead closing to trigger the ExitThread() call.
Sorry, this more than likely has been asked at some point but I'm not even sure what I should be searching on...
I have a winform application with multiple forms. Up until this point having a one form open at a time has been fine. But now, I have a new form that I want to add on but have the ability to keep that form open while I work in other forms. I'm not even sure what this is called but I have seen it done before in other applications.
I did find this: Run two winform windows simultaneously
But this new window is a winpipe queue viewer that runs a thread. When I try initializing using the
Application.Run(new QueueViewer());
I get the error:Starting a second message loop on a single thread is not a valid operation. Use the Form.ShowDialog instead.
The problem with that is it locks the program from doing anything else until I close that form.
Thanks for your help!
Add a form to your project (let's call it Form2). Somewhere within your code (maybe in a button click event) use the following code:
Form2 f = new Form2();
f.Show();
The Show method allows you to interact with the originating form, whereas ShowDialog prevents interaction from the original form.
Two windows apps communicate through .net remoting. The host publishes an instance of a class that the client can call methods on through remoting.
The class has a method which pops up a form if not already visible, using form.Show() and performs some functions in a thread.
When the thread completes the form disappears even though I don't explicitely close it. The instance of the class does not go out of scope, and the class instance maintains a reference to the form.
I don't want the form to disappear and am thinking that if I use the Form.Show(System.Windows.Forms.IWin32Window) overload this would work if I pass the applcation main form to the Show method. The problem is my class instance knows nothing about the application. Is there any way to find the applications main form?
The form is being created and shown inside the thread.
I am just starting .Net development (C#) and have come across some code that has me slightly confused....
If I have
Form myForm = new Form();
What does the following line actually do:
Application.Run(myForm);
Does it essentially do the same thing as myForm.ShowDialog() or myForm.Show() (that's what I thought, when running a form will do).....
I always find that the msdn is a poor resource for properly explaining material to new comers
Application.Run(myForm); makes that form visible to user. It is the first form which get loaded in memory. And it runs this form in a message loop, so that you get all user events.
Short Answer:
Application.Run begins running a standard application message loop on the current thread.
Long Answer:
Application.Run causes the windows application enters the message loop within Winmain to process various windows messages the OS posts to a message queue.The message loop, "Loops" until its receives a WM_QUIT message. It uses GetMessage and PeekMessage to retrive messages and PostMessage to sent the retrived messages to Windows procedure.
If you do
Form myForm = new Form();
myForm.Show();
it will show the form and exit out. You will use new Form() & .Show() when you want to launch a new form from existing form.
Hope this answers your question.
to start an application with a main form, so that the application terminates when the main form is closed. it will be associated to the current thread. it runs this form in a message loop.
Message Loop means : They act upon messages that the operating system posts to the main thread of the application. These messages are received from the message queue by the application by repeatedly calling the GetMessage (PeekMessage) function in a section of code called the "event loop."
Application Run()
My application doesn't have a main Form. Initialization is done in Program.Main() and then Application.Run() is called.
When the user is in a different application they press a keyboard shortcut which triggers the following in my application:
Instantiate my form.
Show the form.
Call the win32 API GetForegroundWindow() to find out which window is active.
Do some stuff.
Call form.Close()
Set form = null
I can execute the above a few times and all is well. However, if my application calls MessageBox() at any time all subsequent calls to GetForegroundWindow() will return the form which was closed even if the current focus is in a different application. This also happens if CredUIPromptForCredentials() prompted the user for a login and password. The call ProcessInfo.GetActiveProcess() also returns my process instead of the other app that has the focus.
Any idea what I might be doing wrong? Is there maybe something I can call to remove the focus from my app; kind-of the reverse of SetForeGroundWindow?
EDIT 1
It's actually a non-modal form, not a dialog. I'm using Show() with no argument to show the form. I tried calling Dispose() after Close() but the behavior is the same. I confirmed that the app works correctly if I never call Show() on my form. The form has all the defaults you get when you create it in VS2010 except for changing the title. The form only contains one label.
Per the docs from Form.Close:
The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.
I am presuming your form is a dialog since that is what is mentioned in the title. It looks like you will need to explicitly call Dispose to deallocate the win32 resources.