rkvonline
rkvonline

Reputation: 25

How to control response timeout in vb .net?

I have a vb .NET windows application. In which I'm calling a external function and receive the response.

Most of the time it is working. But in some client machines the response not coming at proper time. It takes more time. My question is how can I set a timeout, e.g. 30 seconds, so that I will handle the response not coming and should continue the next steps.

Part of my code is shown below.

' my response class
Dim MyResponse as new clsResponse 

' calling outside function which returns response.
MyResponse = Obj.SendRequest(MyRequest) 

'' Some code Here

Upvotes: 2

Views: 2442

Answers (1)

Derek
Derek

Reputation: 8763

I'm thinking you could use Task.Wait:

What's the difference between Task.Start/Wait and Async/Await?

and pass in the wait parameter.

http://msdn.microsoft.com/en-us/library/dd235606.aspx

It returns true if the task finished in the desired time.

Also see this: the task will still be running in the background, although your function will continue

Does Task.Wait(int) stop the task if the timeout elapses without the task finishing?

Task t = new Task(() =>
        {
            MyResponse = Obj.SendRequest(MyRequest);
        });
t.Start();
bool finished = t.Wait(3000);

I think in VB .NET that will be:

Dim t as Task = new Task( Sub() MyResponse = Obj.SendRequest(MyRequest) )
t.Start()
Dim finished as Boolean = t.Wait(3000)

Upvotes: 2

Related Questions