简体   繁体   中英

C# how to stop the program after a certain time?

I have a big problem, but probably it's only big for me :). "terminal.Bind(client);" this line causes my program to hang if IP is bad. I want to stop this program after 5s working because if IP is wrong after 10s all program is hang.. :(

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

您可以在线程中启动Bind,然后启动计时器,如果该线程花费X秒太长时间才能完成,则可以终止该线程或您的应用程序,无论选择哪种方法。

Try to wrap the call in a try catch (assuming some exception is thrown):

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

You can use Task.Wait. Here is little simulation for an operation which will take 10 sec and you are waiting it for 5 sec to finish :)

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

    }
  }
}

I would suggest to put the network stuff into a second thread, which then may be aborted by the main thread.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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