Csharp/C#教程:C#Application.Run没有表格分享


C#Application.Run没有表格

是否可以调用Application.Run,​​但是不传递表单参数,或者如果没有表单可以调用,是否有替代方法?

Run方法似乎没有任何不接受表单的重载。

例如,如果我想首先实例化一个类,然后调用该表单,那么是否可以执行以下操作:

Application.Run(myClass); 

为了澄清,我仍然想要.Run()提供的function。 也就是说,建立一个循环来保持应用程序运行,但不是跟踪表单,而是跟踪类或其他对象。

这最初与紧凑框架有关。 我假设这就是为什么Run方法没有我正在寻找的重载。

我不清楚你是否愿意这样做:

  1. 您希望将表单加载到Main()以外的其他位置
  2. 或者运行没有 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 

你需要知道的是你的程序从main的第一行开始,到最后一行结束。 但是 ,当您调用Application.Run(FORM)它会为该表单加载一个Windows消息循环 。 它是一个特殊的循环,可以使程序保持在主程序中并等待事件(它们在win32 API中称为Windows消息)

因此,在用户单击关闭按钮之前,程序不会结束。 当这种情况发生时,那就是当你的程序实际上将从Main return

(2)所以现在如果你只想要一个没有表格的纯控制台应用程序:

 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 } } 

我希望有所帮助。

Run方法似乎没有任何不接受表单的重载。

呃… https://msdn.microsoft.com/en-us/library/ms157900.aspx

Application.Run方法

开始在当前线程上运行标准应用程序消息循环,没有表单。

public static void Run()

您可以使用Application.Run的重载,该重载接受应用程序上下文作为其唯一参数。 ApplicationContext基本上只是一个可以inheritance的类,并添加您喜欢的任何function。 有关详细信息,请参阅链接中的示例。

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

但问题是,某些东西必须调用myClass的这个实例并告诉它退出,否则程序将在所有表单关闭后继续运行。 并忽略在构造函数中调用ExitThread()。

上述就是C#学习教程:C#Application.Run没有表格分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1030062.html

(0)
上一篇 2022年1月13日
下一篇 2022年1月13日

精彩推荐