2015-04-08 71 views
1

我有一個vb.net應用程序,它與一些外部硬件(一組馬達控制器)相連接。爲此,我使用硬件供應商提供的CANOpen庫。然而,圖書館內建的超時過於坦率,導致應用程序在特定條件下痛苦地掛起。如果可能的話,我寧願不需要編輯庫。在VB.NET中設置手動超時

在另一個設計中最明智的方法是什麼,在vb.net中更短的超時時間?有問題的函數是一個阻塞函數,所以大概在線程定時器不會有幫助。這裏有一個優雅的解決方案嗎?

+0

不知道庫的結構,目前尚不清楚,但我不知道是否可以創建庫的子類並更改超時的持續時間? – ChicagoMike

+0

如果可以的話,這將是可愛的,但可悲的是,我的專業水平,圖書館本身是相同的部分奧術和莫名其妙。超時時間似乎不是用戶可以更改的參數,並且看起來硬編碼。 – user3896248

+0

我感到你的痛苦。試圖在VB中處理任何特定的供應商硬件在很大程度上是一個巨大的痛苦,我試圖與AB ControlLogix處理器交談,我最終編寫了我自己的以太網/ IP通信驅動程序,我感到非常惱火。出於興趣,你試圖與哪些硬件通信? –

回答

0

試試這個,這是迄今爲止我能想到的最好的。我之所以使用後臺工作人員,是因爲他們易於使用。

基本上這是一個線程中的一個線程,這將至少讓你的UI響應,由你說,你應該使用線程的所有驅動器通訊科職能反正如果一個驅動器失去通訊科的,而任何理由什麼判斷應用正在運行。

這並不美觀,但它至少可以讓你在CAN功能本身超時之前退出。

Private connected As Boolean 

Private Sub bwTryConnect_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwTryConnect.DoWork 
    Dim timeout As Boolean 
    Dim timeoutCount As Integer 
    Dim timeoutValue As Integer = 5 ' timeout value 
    bwConnect.RunWorkerAsync() ' start worker to try connection 
    While bwConnect.IsBusy And Not timeout 
     Thread.Sleep(1000) ' wait a second 
     timeoutCount += 1 ' increment timeout value 
     If timeoutCount = timeoutValue Then timeout = True ' connection timed out 
    End While 
    ' connected will be true if the try connection worker completed (connection to drive ok) before the timeout flag was set, otherwise false 
    connected = Not timeout 
End Sub 

Private Sub bwConnect_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwConnect.DoWork 
    ' use your CAN library function here - either a simple connect command or just try reading an arbitary value from the drive 
    ' if you want to test this, uncomment one of the following lines: 
    'Thread.Sleep(20000) ' simulate timeout 
    'Thread.Sleep(2000) ' simulate connect 
End Sub 

很明顯,您然後打電話給bwTryConnect.RunWorkerAsync()

+0

乾杯 - 我實際上沒有考慮過像以前那樣鋪設工人,這更優雅一些。最終的解決方案很好。 – user3896248

+0

沒問題,很高興我能幫到你。 –