C # Application.Run without form

Is it possible to call Application.Run, but not pass the form parameter, or is there an alternative if there is no form to call?

In the execution method, there are no overloads that do not take shape.

For example, if I wanted to create an instance of the class first and then call this call, is there a way to make an equivalent:

Application.Run(myClass); 

Just to clarify, I still want the functionality that .Run () provides. That is, set the loop to keep the application running, but instead of tracking the form, keep track of the class or other object.

This refers to the compact structure initially. I assume that the Run method does not have the overload I was looking for.

+8
c # compact-framework
source share
4 answers

I do not understand if you want:

  • You want to upload your form somewhere else besides Main ()
  • Or launch a console or service application using no interface.

For (1) :

 static void main() { //Your program starts running here<<< //Do some stuff... FormRunner a = new FormRunner(); a.RunForm(); } // << And ends here class FormRunner { public void RunForm() { Application.Run(new Form()); } //You could call which ever form you want from here? } // << And ends here 

What you need to know, your program starts on the first line of the main line and ends on the last. However , when you call Application.Run(FORM) , it loads the Windows message loop for this form. Its special loop, which maintains the program is still mostly and waiting for events (they are called Windows messages in the win32 API)

And so the program does not end until the user clicks the close button. When this happens, it means that your program will actually return from its Main.

(2) So now, if you just want to use a clean console application without forms:

 static void main() { AcceptInputs() DrawScreen() //Do something else. //Make sure your flow stays within the main } // << Once you come here you're done. void AcceptInputs() { while(true) { //Keep accepting input break; // Call break when you're done. You'll be back in the main } } 

I hope this helps.

+6
source share

In the execution method, there are no overloads that do not take shape.

Uh ... http://msdn.microsoft.com/en-us/library/ms157900.aspx

Application.Run Method

Begins a standard application message loop in the current formless stream.

public static void Run()

+14
source share

You can use Application.Run overload, which takes the application context as a single parameter. ApplicationContext is just a class to which you can inherit and add any functionality that you like. See the example in the link for more information.

+3
source share
 using System; using System.Windows.Forms; static class Program [STAThread] static void Main() { Application.Run(new myClass()); } internal class myClass : ApplicationContext { public myClass() { Application.Run(new myWindow()); } } } 

The problem here is that something will have to call this instance of myClass and tell it to exit, otherwise the program will continue to work even after closing all forms. And the call to ExitThread () in the constructor is ignored.

0
source share

Source: https://habr.com/ru/post/651195/


All Articles