在 VB.NET 中设置手动超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29516181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:59:31  来源:igfitidea点击:

Setting a manual timeout in VB.NET

vb.netmultithreadingtimeout

提问by user3896248

I have a vb.net application that interfaces with some external hardware - an array of motor controllers. To do this, I'm using a CANOpen library provided by the hardware supplier. However, the timeouts built into the library are frankly excessive, and cause the application to hang painfully under specific conditions. I'd prefer not to need to edit the library if possible.

我有一个与一些外部硬件接口的 vb.net 应用程序 - 一组电机控制器。为此,我使用了硬件供应商提供的 CANOpen 库。但是,坦率地说,库中内置的超时时间过多,会导致应用程序在特定条件下痛苦地挂起。如果可能,我宁愿不需要编辑库。

What's the most sensible way to design in another, shorter timeout within vb.net? The function in question is a blocking function, so presumably in-thread timers won't help. Is there an elegant solution here?

在 vb.net 中设计另一个更短的超时的最明智的方法是什么?有问题的函数是一个阻塞函数,所以大概线程内计时器无济于事。这里有优雅的解决方案吗?

采纳答案by The Blue Dog

Give this a try, it's is the best I could come up with so far. I've used background workers just because they are easy to use.

试试这个,这是迄今为止我能想到的最好的。我使用后台工作者只是因为它们易于使用。

Basically it's a thread within a thread which will at least keep your UI responsive, judging by what you've said you should probably use threading for all the drive comms functions anyway in case a drive loses comms for any reason while the app is running.

基本上,它是一个线程中的一个线程,它至少可以使您的 UI 保持响应,根据您所说的判断,您应该对所有驱动器通信功能使用线程,以防万一驱动器在应用程序运行时因任何原因丢失通信。

This ain't pretty, but it will at least allow you to exit out early before the CAN function itself times out.

这不是很好,但它至少可以让您在 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

Obviously you then call with bwTryConnect.RunWorkerAsync().

显然,你然后用 调用bwTryConnect.RunWorkerAsync()