14

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
  • http://msdn.microsoft.com/en-us/library/aa288463(v=vs.71).aspx – Claus Jørgensen Jan 18 '11 at 08:39
  • What are you trying to do? `Application.Run` basically starts the _message pump_ which is responsible of dispatching messages to windows and event handlers. What is the purpose of entering a _message pump_ if no window exists? – Nicolas Repiquet Jan 18 '11 at 08:41
  • I've edited my question. What I am actually trying to do is to launch the first form from a non-static class. So I wanted to instantiate the class and then launch the form. – Paul Michaels Jan 18 '11 at 08:44
  • 1
    ah I left to grab a bite, came back and posted my answer and you edited the question! =P However part of my answer may explain what you want?? – gideon Jan 18 '11 at 09:02

4 Answers4

17

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()

Community
  • 1
  • 1
ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
8

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gideon
  • 19,329
  • 11
  • 72
  • 113
  • The last sentence of your part 1 is wrong. Applicaiton.Run(FORM) does not actually return when the main form is closed. It calls either Exit(int) or a ThreadExit(...), which is horrible because it makes `try{Application.Run(new main_form());}catch(exception e){...}finally{...}` not do what you'd expect. – nategoose Jul 16 '12 at 15:29
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.

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
2
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.

dynamichael
  • 807
  • 9
  • 9