简体   繁体   English

C#控制台应用程序将无法运行

[英]C# Console application wont run

Im trying to change my application to a console application. 我试图将我的应用程序更改为控制台应用程序。 I want it to work so that if one parameter is passed method 1 is executed and if no parameter is passed method 2 is executed. 我希望它起作用,以便如果传递了一个参数,则执行方法1,如果没有传递参数,则执行方法2。

From the code I have, when I try run it nothing happens. 根据我的代码,当我尝试运行它时,什么也没有发生。

Here is my main code: 这是我的主要代码:

[STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run();

        RunTestCases runTestCases = new RunTestCases();
        DataIntegration dataIntegration = new DataIntegration();



        if (args != null)
        {           
            runTestCases.RunTestCaseForSelectedField(args);
        }
        else
        {
            runTestCases.RunTestCaseForAllFields();
        }

    }

Any ideas? 有任何想法吗?

Thanks 谢谢

Application.Run starts a message loop, which remains until your app is closed (receives quit message). Application.Run启动消息循环,该循环一直持续到您的应用程序关闭(接收退出消息)。 Hence nothing after Application.Run() is executed until that quit message. 因此,在执行Application.Run()之后直到退出消息之前什么都没有执行。

The question is if you really need those lines referring to Application. 问题是您是否真的需要引用Application的那些行。 If you don't have a Window, chances are, a message loop is superfluous. 如果没有Window,则很可能没有消息循环。 The message loop could be necessary if you want to act on windows messages. 如果要对Windows消息进行操作,则可能需要消息循环。 However, then you would run this loop in a seperate thread. 但是,那么您将在单独的线程中运行此循环。

You would find out the real problem if you debug it, as in put a breakpoint in the args != null line and see exactly what's inside args . 如果您对其进行调试,则会发现真正的问题,例如在args != null行中放置一个断点,并确切地查看args内部的内容。

However, it's probably because args is never null, so make it: 但是,这可能是因为args永远不会为null,所以请使其:

if (args.Any())

(Edit: JeffRSon also caught another thing wrong with your code, so his answer is right too, however, you'll hit the args problem I describe once you fix it the original problem. So I'll keep my answer here too. (编辑: JeffRSon还发现您的代码有另一处错误,因此他的答案也是正确的,但是,一旦解决了原始问题,您将遇到我描述的args问题。因此,我也将答案保留在这里。

When launching a console app with no parameters, the args will be an empty array, not a null value) 启动不带参数的控制台应用程序时, args将为空数组, 而不是空值)

Remove first three lines in main method. 删除main方法中的前三行。 Check in project properties if the Output type is Console Application Set entry point to your method 检入项目属性,如果“ Output type是“ Console Application设置方法的入口点

I don't have the rep to comment but one problem I see is that your first condition will always be true because args with no command line input is a zero length string array rather than a null value. 我没有代表对此发表评论,但是我看到的一个问题是,您的第一个条件将始终为真,因为没有命令行输入的args是长度为零的字符串数组,而不是空值。 You want to check to see what is in args rather than assume it is null. 您要检查以查看args中的内容,而不是假定它为null。

在控制台应用程序中不需要与应用程序相关的代码,它仅在Windows applicattion(wpf)中使用,其他人对“ args”的解释也有效。

if (args.Length > 0)
        {
            runTestCases.RunTestCaseForSelectedField(args[0]);
        }
        else
        {
            //runTestCases.RunTestCaseForAllFields();
            Console.WriteLine("No Parameter");
        }

This is what I have now , whenever I run it, it runs the else statement. 这就是我现在拥有的,只要我运行它,它就会运行else语句。 Why arent my parameters being picked up? 为什么不选择我的参数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM