2011-12-27 57 views
2

我有一個大問題,但可能它只對我很大:)。 「terminal.Bind(客戶端);」如果IP不好,這行會導致我的程序掛起。我想停下來,因爲工作5S後該計劃,如果IP是錯誤的10S畢竟程序掛.. :(C#如何在一段時間後停止程序?

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

你會想要把它扔在一個線程和一段時間後終止線程。 – SQLMason 2011-12-27 16:19:55

+0

你等了30秒?我認爲這是建立連接的一般暫停時間。換句話說,您的代碼可能在嘗試連接到不良IP時阻塞。我認爲它最終應該會回來,可能會導致例外。只是一個猜測。 – Bengie 2011-12-27 17:18:47

回答

2

嘗試包在嘗試捕捉呼叫(如果有的拋出異常)。

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

這並沒有解決執行超時的目標。 – RQDQ 2011-12-27 16:24:36

+0

這是格柵和簡單的非常感謝... – zee 2011-12-27 17:51:51

+0

@RQDQ - 除了它。如果IP地址無效,則沒有理由等待。當前進程只嘗試綁定一次IP地址,因此等待將毫無意義。 – 2011-12-27 18:18:57

2

你可以在一個線程揭開序幕的綁定,並開始計時,如果線程需要X秒就可以完成,你可以殺死線程或應用程序,無論你選擇

+0

嘿!無論如何,我在評論中說過幾乎相同的東西。 – SQLMason 2011-12-27 16:21:03

+1

@DanAndrews - 那爲什麼不把它作爲答案呢? – Oded 2011-12-27 16:24:27

+0

@DanAndrews,我們必須在幾乎那麼確切時間公佈,沒有什麼在這裏的時候,我貼 – 2011-12-27 16:28:34

0

我會建議將網絡資料放入第二個線程,然後可能會被m中止ain線程。

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

    } 
    } 
} 
+0

我複製你的代碼的一部分,它工作正常,但後來我試着寫像「terminal.SendToServer」或任何與終端的一些命令。****項目說「不包含定義」 – zee 2011-12-27 17:57:33

相關問題