C# Application.Run without Form

只谈情不闲聊 提交于 2019-11-26 21:36:31

问题


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

The Run method doesn’t seem to have any overloads that don’t accept a form.

For example, if I wanted to instantiate a class first, and then have that call the form, is there way to do the equivalent of:

Application.Run(myClass);

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

This is relating to the compact framework initially. I assume that's why the Run method doesn't have the overload I was looking for.


回答1:


I'm not clear whether you want to do:

  1. You want to load your form somewhere else other than the Main()
  2. Or Run a console or service application with no UI.

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 is your program starts from the first line of the main and ends at the last. However, when you call Application.Run(FORM) it loads up a windows message loop for that form. Its a special loop that keeps the program still in the main and waits for events (they're called Windows Messages in win32 API)

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

(2) So now if you just want a pure console app with no 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 that helped.




回答2:


The Run method doesn’t seem to have any overloads that don’t accept a form.

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

Application.Run Method

Begins running a standard application message loop on the current thread, without a form.

public static void Run()




回答3:


You can use the overload of Application.Run that accepts an application context as its only parameter. An ApplicationContext is basically just a class that you can inherit from and add any functionality you like. See the example in the link for more information.




回答4:


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, though, is that something will have to call this instance of myClass and tell it to exit or else the program will just keep running even after all forms are closed. And calling ExitThread() in the constructor is ignored.



来源:https://stackoverflow.com/questions/4721962/c-sharp-application-run-without-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!