C#没有形式Application.Run [英] C# Application.Run without Form

查看:733
本文介绍了C#没有形式Application.Run的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能调用Application.Run,​​但不传递表单参数,或者是有一个选择,如果有任何形式的称呼?

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?

Run方法似乎并不具有不接受任何形式的重载。

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

只是为了澄清,我仍然希望这.RUN()提供的功能。即,建立一个循环来保持运行的应用程序,但是,代替跟踪一个形式中,跟踪类或其他对象。

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.

这是与紧凑的框架开始。我想这就是为什么Run方法没有我要找的过载。

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.

推荐答案

我不清楚你是否想做的事:

I'm not clear whether you want to do:

  1. 您想加载形式大于主()
  2. 其他地方其他
  3. 或运行是没有 UI控制台或服务应用程序。

对于(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

你需要知道什么是你的程序开始从主要的第一行,并结束于最后。 然而,当你调用 Application.Run(FORM),它加载了的窗消息循环以这种形式。它是一种特殊的循环,仍保持该方案的主要及等待事件(他们称为Win32 API的Windows消息)

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)所以,现在如果你只是想,没有形成一个纯粹的控制台应用程序:

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

这篇关于C#没有形式Application.Run的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆