服务器端的 VB.NET 定时器
我遇到一种情况,我试图从客户端部分调用服务器端部分。
我需要同步执行此操作。不过,在我的服务器端代码中,我正在调用另一段代码,我希望该代码最多有 10 秒的时间来返回值。
如果我在 10 秒内没有响应,我想强制我自己的服务器将结果设置为 0,然后返回给客户端。
到目前为止,我的服务器端代码如下:
Public Function GetWebResult(ByVal inputParameter As Object) As WebReturnObject Implements IWebInterface.GetWebResult
Dim result As New WebReturnObject
Dim webItem As WebItem = Nothing
_timer = New Timers.Timer
AddHandler _timer.Elapsed, AddressOf HandleTimerElapsed
_timer.Interval = 10000 '10 Seconds'
_timer.AutoReset = False
_timer.Start()
webItem = GetAWebItem(inputParameter)
_timer.Stop()
result = webItem
Return result
End Function
Private Sub HandleTimerElapsed(ByVal sender as Object, ByVal e As System.Timers.ElapsedEventArgs) Handles _timer.Elapsed
'result.HasTimedOut = True'
'result.Value = 0'
'How do I then get back into GetWebResult?'
End Sub
我意识到这段代码不起作用。问题是我不确定如何在将返回值发送回客户端之前返回到 GetWebResult 来设置适当的返回值。
我已经用谷歌搜索过这个问题,但结果很少。我能找到的最好的资源是 Timers.Timer 类,但考虑到我的情况,即使这样也不是很有用。
如果有人有任何链接、建议等,我将非常感激。
I have a situation where I'm trying to, from a client-side piece, call off to a server-side piece.
I need to do this synchronously. On my server-side code, though, I'm calling off to another piece of code, which I want to have a maximum of, say, 10 seconds to return a value.
If I don't have a response within 10 seconds, I want to force my own server piece to set my result equal to 0, and return to the client.
Here's what I have on my server-side code so far:
Public Function GetWebResult(ByVal inputParameter As Object) As WebReturnObject Implements IWebInterface.GetWebResult
Dim result As New WebReturnObject
Dim webItem As WebItem = Nothing
_timer = New Timers.Timer
AddHandler _timer.Elapsed, AddressOf HandleTimerElapsed
_timer.Interval = 10000 '10 Seconds'
_timer.AutoReset = False
_timer.Start()
webItem = GetAWebItem(inputParameter)
_timer.Stop()
result = webItem
Return result
End Function
Private Sub HandleTimerElapsed(ByVal sender as Object, ByVal e As System.Timers.ElapsedEventArgs) Handles _timer.Elapsed
'result.HasTimedOut = True'
'result.Value = 0'
'How do I then get back into GetWebResult?'
End Sub
I realize this code is not functional. The problem is that I'm unsure how I would go about getting back into GetWebResult
to set the appropriate return values before it is sent back to the client.
I've already Google'd this problem, and results were scant. The best resources I was able to find were the MSDN documentation on the Timers.Timer class, but even that isn't terribly useful given my situation.
If anyone has any links, suggestions, etc., I would very much appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在超时(10 秒)内异步调用该方法,无论方法调用是否完成,您都会返回
GetWebResult
,然后您可以从那里返回。 示例Call the method asynchronously with a timeout (10 seconds) you'll be back in
GetWebResult
irrespective of the method call completion, then you could return from there. Sample here