2

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
Florian
  • 1,827
  • 4
  • 30
  • 62
rkvonline
  • 25
  • 1
  • 10

1 Answers1

2

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)
Community
  • 1
  • 1
Derek
  • 7,615
  • 5
  • 33
  • 58
  • Since OP has not given the framework, it should be noted that this method will only work in .NET 4.0 or higher. For .NET3.5 or lower this must be implemented with threads directly. – J... Oct 27 '13 at 10:40
  • actually im using .NET 3.5. Task is not available ? If not possible please show me implementing thread ? – rkvonline Oct 27 '13 at 10:58
  • .NET 4 has been available for three and a half years already, easily a dog life in software engineering. Time to move on, you've found a good reason. – Hans Passant Oct 27 '13 at 13:55
  • exactly. now i'm working in .NET 4. this is some renovation for the older project. – rkvonline Oct 27 '13 at 14:22