有什么办法可以立即跳转到下一个语句吗?

发布于 2024-11-28 18:29:23 字数 329 浏览 1 评论 0原文

所以这可能是一个奇怪的问题,但我有一个从控制台运行的 C# 程序,现在除了一件事之外,大多数情况下一切都工作正常。

在我的程序中,我有一段代码,例如...

loadFile();

foreach(var x in imgSet)
{
  //do whatever
}

我遇到的问题是我希望我的整个程序自动化并使用任务调度程序运行,我不想进行人工交互。程序在控制台中很好地调用了 loadFile() 方法,但是在写出该方法的输出后,用户必须按 Enter 才能进入 foreach 循环。我不太清楚为什么会这样,并且想知道是否有办法绕过它并使其完全自动化?

So this might be an odd question, but I have a C# program that runs from the console, and everything works fine for the most part now except one thing.

Within my program, I have a section of code like...

loadFile();

foreach(var x in imgSet)
{
  //do whatever
}

The problem I have is that I want my entire program to be automated and run using task scheduler, I want no human interaction. And the program calls the loadFile() method just fine within the console, but after it writes out the output from that method, the user must press enter to go into that foreach loop. I do not quite know why this is and was wondering if there is a way to by-pass that and make it entirely automated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寻梦旅人 2024-12-05 18:29:23

loadFile(); 方法更改为不再等待用户按 Enter 键,或者根据参数选择执行此操作(以便您可以将其配置为等待或不等待)。

程序执行将尽可能快地逐行运行,没有延迟。如果它正在等待用户交互,那是因为程序明确表示应该发生这种情况。

Change the loadFile(); method to either no longer wait for the user to press enter, or optionally do it, based on a parameter (so you can configure it to either wait, or not).

Program execution will run from line-to-line, as quickly as possible, without delay. If it's waiting for user interaction, it's because the program explicitly says that should happen.

橙幽之幻 2024-12-05 18:29:23

如果是控制台程序,loadFile()可能会调用Console.ReadKey()Console.ReadLine()

如果您可以更改代码在 loadFile() 方法中,您可以添加参数或重载来跳过用户的任何不必要的输入。

If it's a console program, loadFile() may call Console.ReadKey() or Console.ReadLine()

If you can change the code for the loadFile() method, you might be able to add a parameter or an overload that will skip over any unnecessary input from the user.

被翻牌 2024-12-05 18:29:23

您需要查看 loadFile 方法的内部并找到它等待用户输入的部分。

You need to look inside the loadFile method and find the part where it waits for the user's input.

吐个泡泡 2024-12-05 18:29:23

loadFile 方法必须执行等待用户输入的操作。因为该等待正在阻塞线程,所以在该方法调用之后您无法在代码中放入任何内容,从而导致它“跳”过阻塞语句(只要它正在等待用户,执行指针就永远不会到达该行) 。您必须进入 loadFile 并查看是否可以重构它以使用户输入可选(可能取决于您在命令行上指定的参数开关)。

The loadFile method must be doing something that waits for user input. Because that wait is blocking the thread, there's nothing you could put into the code after that method call that would cause it to "jump" past the blocking statement (the execution pointer will never reach that line as long as it's waiting on the user). You'll have to go into loadFile and see if you can refactor it to make the user input optional (perhaps dependent on an argument switch you specify on the command line).

只想待在家 2024-12-05 18:29:23

为什么不简单地向控制台应用程序提供一个参数,以便每当您请求用户输入时,如果提供的参数告诉您不要跳过该行?

示例:

protected bool _skipConsoleInput = false;

public static void Main(string[] args)
{
     if(args != null && args.Count > 0 && args[0] == "SkipConsoleInputYo")
         _skipConsoleInput = true;

     loadFile();

     GetConsoleInput();

     foreach(var x in imgSet) { }
}

protected string GetConsoleInput()
{
    if(_skipConsoleInput)
       return string.Empty;

    return Console.ReadLine();
}

当您不提供参数时,必须按 Enter 键。如果这样做,它会完全跳过它。

Why not simply provide an argument to your console application such that whenever you request input from the user, if the argument supplied tells you not to, skip the line?

Example:

protected bool _skipConsoleInput = false;

public static void Main(string[] args)
{
     if(args != null && args.Count > 0 && args[0] == "SkipConsoleInputYo")
         _skipConsoleInput = true;

     loadFile();

     GetConsoleInput();

     foreach(var x in imgSet) { }
}

protected string GetConsoleInput()
{
    if(_skipConsoleInput)
       return string.Empty;

    return Console.ReadLine();
}

When you dont supply the argument, you must press enter. If you do, it skips it completely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文