如何调用main方法或重新启动app控制台
本文关键字:方法 重新启动 app 控制台 main 何调用 调用 | 更新日期: 2023-09-27 17:50:24
当发生异常时,我想重新启动所有处理或启动Main方法,在此其他方法之后:
public void DisplayMessage(string message) {
Console.WriteLine(message, "Rebuild Log Files");
Console.WriteLine(" Press Enter to finish, or R to restar the program...");
string restart = Console.ReadLine();
if(restart.ToUpper() == "R") {
//Call the Main method or restart the app
}
Console.ReadKey();
}
注意:main方法包含一些用户写入的数据。
我该怎么做?
好了,这里有一个main
void main(...)
{
some code
}
你所需要做的就是…
void main()
{
runStartUpCode();
}
void runStartUpCode()
{
some code
}
当需要重新启动代码时,再次调用runStartUpCode()
if(restart.ToUpper() == "R") {
Close();
System.Diagnostics.Process.Start(Application.ExecutablePath);
}
static void Main(string[] args) {
try {
// code here
} catch /* or finally */ {
DisplayMessage(/* pass in any state from Main() here */);
}
}
static void DisplayMessage(/* passed in state from Main() */) {
// original DisplayMessage() code
// if R was pressed
Main(/* put any args to pass to Main() in here */);
}
其实在你读这篇文章的时候我已经解决了这个问题。我复制了ORIGINAL主方法,改变了原始的几个选项,并让副本调用新的主方法。这就是我的意思
static void Main(string[] args)
{
Program CallingTheRealMain = new Program();
CallingTheRealMain.Main2();
}
public void Main2()
{
//Any code here
}
我最初需要这样做是因为我需要循环回main方法,但由于它是静态的,所以不能这样做。这段代码对我来说很好,希望它也为你做,如果你选择实现它。希望我帮到你。编码快乐!
DialogResult result = MessageBox.Show("Do You Really Want To Logout/Exit?", "Confirmation!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
this.Close();
System.Diagnostics.Process.Start(Application.ExecutablePath);
}
我认为你的设计方法应该改变,如果你的应用程序自动重启....这里有一个伪代码
main (){
errorObj = null;
internalFunct(errorObj);
if(errorObj != null) return;
secondINternalFunction();
}
Running the app…
while(!errorObj){
main();
}
为什么我更喜欢这种方法,因为Main或函数调用被避免了…如果你的工作用的是小内存,没有点回调主就栈上有限的空间…你别无选择,只能迭代…
c#新手,我为错误道歉。使用:
static void Restart() {
String[] n = new String[10];
n[0] = "hi";
Main(n);
}
Main接受一个字符串数组,原因我不知道。对我来说似乎没用,因为我试着把它移走,但似乎没有任何改变。所以,也许这也有效:试着删除Main()的string[] args
部分,这样你就可以输入Main();没有问题。