【问题标题】:C# how to stop the program after a certain time?C#如何在一段时间后停止程序?
【发布时间】:2012-01-28 15:02:15
【问题描述】:

我有一个大问题,但可能对我来说只是大问题:)。 “终端。绑定(客户端);”如果 IP 错误,此行会导致我的程序挂起。我想在 5 秒后停止这个程序,因为如果 10 秒后 IP 错误,所有程序都会挂起.. :(

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rebex.TerminalEmulation;
using Rebex.Security;
using Rebex.Net;

namespace Routers_info_v._1
{
    class Program
    {
        static void Main(string[] args)
        {
            Telnet client = new Telnet("192.168.1.1");  
            VirtualTerminal terminal = new VirtualTerminal(80, 25);

            terminal.Bind(client);     

            terminal.SendToServer("pass\r");
            terminal.SendToServer("sys ver\r");

            TerminalState state;
            do
            {           
                state = terminal.Process(2000);
            } while (state == TerminalState.DataReceived);

            terminal.Save("terminal.txt", TerminalCaptureFormat.Text, TerminalCaptureOptions.DoNotHideCursor);
            terminal.Unbind();
            terminal.Dispose();
        }
    }
}

【问题讨论】:

  • 你会想把它扔到一个线程中并在一段时间后终止线程。
  • 你等了 30 秒吗?我认为这是建立连接的一般超时时间。换句话说,您的代码可能会阻止尝试连接到错误的 IP。我认为它最终应该返回并可能导致异常。只是猜测。

标签: c# timeout telnet


【解决方案1】:

您可以使用 Task.Wait。这是一个需要 10 秒的操作的小模拟,您正在等待它完成 5 秒:)

using System;
using System.Linq;
using System.Data.Linq;
using System.Data;
using System.Threading.Tasks;


namespace ConsoleApplication5
{
  class VirtualTerminal
  {
    public VirtualTerminal(int a, int b) { }
    public bool Bind() { System.Threading.Thread.Sleep(10000); return true; }
  }
  class Program
  {

    static void Main(string[] args)
    {
      VirtualTerminal terminal = new VirtualTerminal(80, 25);

      Func<bool> func = () => terminal.Bind() ;
      Task<bool> task = new Task<bool>(func);
      task.Start();
      if (task.Wait(5*1000))
      {
        // you got connected
      }
      else
      {
        //failed to connect
      }


      Console.ReadLine();

    }
  }
}

【讨论】:

  • 我复制了您的部分代码,它工作正常,但后来我尝试编写一些命令,如“terminal.SendToServer”或任何带有终端的命令。**** 程序说“不包含定义"
【解决方案2】:

我建议将网络内容放入第二个线程,然后可能会被主线程中止。

class Program {
    static void Main(string[] args) {
        Thread thread = new Thread(threadFunc);
        thread.Start();
        Stopwatch watch = new Stopwatch();
        watch.Start();
        while (watch.ElapsedMilliseconds < 5000 && thread.IsAlive)
            ;
        if (!thread.IsAlive) {
            thread.Abort();
            Console.WriteLine("Unable to connect");
        }
    }

    private static void threadFunc() {
        Telnet client = new Telnet("192.168.1.1");  
        VirtualTerminal terminal = new VirtualTerminal(80, 25);

        terminal.Bind(client);     
        // ...
        terminal.Dispose();
    }
}

【讨论】:

    【解决方案3】:

    尝试将调用包装在 try catch 中(假设抛出了一些异常):

    try 
    {
        terminal.Bind(client);     
    }
    catch(Exception ex)
    {
        return;
    }
    

    【讨论】:

    • 这并没有解决强制超时的目标。
    • @RQDQ - 除了它。如果 IP 地址无效,则没有理由等待。当前进程只尝试绑定一次ip地址,等待是没有意义的。
    【解决方案4】:

    您可以在线程中启动绑定,并启动一个计时器,如果线程花费 X 秒的时间太长才能完成,您可以终止线程或您的应用程序,无论您选择哪个。

    【讨论】:

    • 嘿!我在评论中说了几乎同样的话:) +1 反正。
    • @DanAndrews - 那为什么不把它作为答案发布呢?
    • @DanAndrews,我们发帖的时间肯定差不多,我发帖的时候这里什么都没有
    • 是的,我们好像休息了 10 秒之类的。 @Oded,嗯,我认为它不值得回答,只是评论......我错了......我失败了......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2019-08-08
    • 2013-09-20
    相关资源
    最近更新 更多