2016-05-16 21 views
0

これは私のコードです。私は、実行のこの行でCOMポートが応答しないときに次のコード行を終了する

Thread tParent = new Thread(new ThreadStart(() => 
       { 
        Thread t = new Thread(master.ReadCoils(slaveId, 13, numRegisters)); 
        t.Start(); 
        Thread.Sleep(5000); 
        if (t.IsAlive) t.Abort(); 
       })); 

       coilsstatus = master.ReadCoils(slaveId, 13, numRegisters); 

私のアプリケーションがフリーズ

coilsstatus = master.ReadCoils(slaveId, 13, numRegisters); 

にコードの次の行をプロセスを終了し、実行したい、私は5秒後に実行を終了し、次の行を実行したいですコードの

+0

「tParent.Start()」の最初のスレッドを開始していないようです。 ThreadStart引数の中でブレークポイントに当たっていますか? –

+0

スレッドt =新しいスレッド(master.ReadCoils(slaveId、13、numRegisters));私がエラーを返すこの行はactoolでbool []からsystem.threading.startに変換することはできません –

+0

あなたは本当に必要はありません: 'new ThreadStart(' –

答えて

0

固定コードは以下のとおりです。 ReadCoils()は、ThreadAbortExceptionをキャッチして途中で終了したスレッドにいることを認識できます。

// This creates the thread that calls ReadCoils() or, in my case, 
// waits for 10 seconds. 
Thread newThread = new System.Threading.Thread(() => 
{ 
    // I replaced the following with "Thread.Sleep(10 * 1000);" to test 
    master.ReadCoils(slaveId, 13, numRegisters);   
}); 
// Run the thread 
newThread.Start(); 
System.Threading.Thread.Sleep(5); 
if (newThread.IsAlive) 
{ 
    // Stop the thread if it is still running.   
    newThread.Abort(); 
} 
+0

ここにどこに配置しますか私のこのコードはcoilstatus = master.ReadCoils(slaveId、13、numRegisters); –

+0

私の編集された回答を参照してください - あなたは上記のコードをそのまま取ることができるはずです – Loring

+0

しかしh owを値master.ReadCoils(slaveId、13、numRegisters)に変換する。これは私もretrun値を –

関連する問題