在返回值的函数上实现超时
我有一个函数,它在串行端口上调用读或写请求,然后返回读取的值。 我正在使用Commstudio express(我正在实现Commstudio中的一个类),但它的超时function似乎根本不起作用,所以我正在尝试实现自己的超时。 目前我有一个定时器,根据请求设置读取或写入端口,如果定时器关闭,回调将关闭连接导致exception。 我试图让计时器的回调抛出一个exception,但exception需要通过调用原始读/写函数的线程传播,所以这样,它可以工作,但我觉得它很混乱,那里必须是一个更好的方式来做我想要的。
这是一个通用的解决方案,允许您在超时中包装任何方法:
https://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/
它使用有用的Thread.Join重载,它接受超时(以毫秒为单位)而不是手动使用定时器。 我唯一不同的做法是交换成功标志和结果值以匹配TryParse模式,如下所示:
public static T Execute(Func func, int timeout) { T result; TryExecute(func, timeout, out result); return result; } public static bool TryExecute (Func func, int timeout, out T result) { var t = default(T); var thread = new Thread(() => t = func()); thread.Start(); var completed = thread.Join(timeout); if (!completed) thread.Abort(); result = t; return completed; }
这就是你如何使用它:
var func = new Func(() => { Thread.Sleep(200); return "success"; }); string result; Debug.Assert(!TryExecute(func, 100, out result)); Debug.Assert(result == null); Debug.Assert(TryExecute(func, 300, out result)); Debug.Assert(result == "success");
如果要执行不返回值的方法,还可以添加接受Action而不是Func的重载。
听起来你正在进行阻塞读/写。 你想要做的是非阻塞读/写。
有可能告诉com端口你想要非阻塞。
你确定超时不适用于commstudio吗? 也许你必须做一些特殊的事情来初始化它们。
在任何情况下,您都希望尽可能多地读取数据,如果没有可用超时(取决于超时的值)。 您将希望在没有可用数据且没有错误的情况下保持循环,然后在没有任何可用的情况下返回超时条件。
使读取函数返回整数。 负值=错误值例如-1 =超时,正读取的字节数…至少就是我这样做的方式。
对于comport你可以测试是否有任何可用的东西然后做一个读取而不是做阻塞读取而不知道还有什么。 就像是:
Int32 timeout=1000; String result = String.Empty'; while (timeout!=0) { if (Serial.BytesToRead>0) { while (Serial.BytesToRead>0) { result+=Serial.ReadChar(); } break; } Thread.Sleep(1); timeout--; }
如果有人想在VB.Net中这样做,不要听那些说不能做的人! 您可能需要更改通用参数以适合您的用例。
Public Shared Function Execute(Of I, R)(Func As Func(Of I, R), Input As I, TimeOut As Integer) As R Dim Result As R TryExecute(Func, Input, TimeOut, Result) Return Result End Function Public Shared Function TryExecute(Of I, R)(Func As Func(Of I, R), Input As I, TimeOut As Integer, ByRef Result As R) As Boolean Dim OutParam As R = Nothing Dim Thread As New System.Threading.Thread(Sub() InlineAssignHelper(OutParam, Func(Input))) Thread.IsBackground = True Thread.Start() Dim Completed As Boolean = Thread.Join(TimeOut) If Not Completed Then Thread.Abort() Result = OutParam Return Completed End Function Private Shared Function InlineAssignHelper(Of T)(ByRef Target As T, ByVal Value As T) As T Target = Value Return Value End Function
以及如何使用它的一个例子(我的是Regex.Match
,如果模式包含太多的外卡,它有时会永远不会落地:
上述就是C#学习教程:在返回值的函数上实现超时分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
Public Function Match(Input As String) As Match If Regex Is Nothing Then Return Nothing Dim RegexMatch As System.Text.RegularExpressions.Match = Nothing Dim Func As New Func(Of String, System.Text.RegularExpressions.Match)(Function(x As String) Regex.Match(x)) If Runtime.TryExecute(Of String, System.Text.RegularExpressions.Match)(Func, Input, 2000, RegexMatch) Then Return (New Match(Me, Regex.Match(Input), Input)) Else Return Nothing End If End Function
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1032013.html