Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a vb .net windows application. In this, i am calling a external function and receives the response. Most of the case 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 ,eg. 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
Posted
Updated 27-Oct-13 0:01am
v3
Comments
Mahesh Bailwal 27-Oct-13 6:13am    
what do you mean by "external function", are you making WCF/web service/Remoting call?
normalsoft 27-Oct-13 6:25am    
function in another dll. that referrences to the project.
jenitshah 27-Oct-13 10:54am    
You can get global boolean variable and keep checking its value using seprate thread, based on true or false you can exit the while loop and continue to next steps...
normalsoft 28-Oct-13 8:46am    
thanks for the comment. but i need a timeout period criteria.

1 solution

For this kind of requirement you can use "System.Threading.Tasks.Task" class in which you can provide time out, check below link

Task Class[^]

Below is sample code snippet I have tested in C# for replying your question.
Added second logic using thread, hope it will help
//========================Logic using Task================================
  private void button1_Click(object sender, EventArgs e)
        {
            Func<object, object> func1 = new Func<object, object>(CallAction);
            object obj = new object();
            Task<object> task = new Task<object>(func1, obj);
            task.Start();
            //wait for max 30 second
            task.Wait(30 * 1000);
            object result;
            if (task.Status == TaskStatus.RanToCompletion)
            {
                result = task.Result;
            }
        }

        private object CallAction(object obj)
        {
            ClassLibrary1.Class1 class1 = new ClassLibrary1.Class1();
            object rtnObj = class1.fun1(obj);
            return rtnObj;
        }
//==============Second Logic using Thread=======================
   object objReturn = null;
        private void  WorkerThreadMethod(object obj)
        {
            ClassLibrary1.Class1 class1 = new ClassLibrary1.Class1();
            objReturn = class1.fun1(obj);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            ParameterizedThreadStart paraThreadStart = new ParameterizedThreadStart(WorkerThreadMethod);
            Thread workerThread = new Thread(paraThreadStart);
            workerThread.Start();

           //wait for max 30 second
            if (!workerThread.Join(new TimeSpan(0, 0, 30)))
            {
                workerThread.Abort();
            }
            else
            {
                //processs objReturn 
                object gotReturn = objReturn;
                
            }
        }
 
Share this answer
 
v4
Comments
normalsoft 27-Oct-13 7:53am    
actually the application is in .NET 3.5. Task is not available.
Mahesh Bailwal 27-Oct-13 10:02am    
added second logic in my solution using thread, hope it will help
normalsoft 28-Oct-13 8:41am    
its working. but i need to abort the process within the time limit whether response received or not.
Mahesh Bailwal 28-Oct-13 9:00am    
If response will not come with in 30 second then "workerThread.Abort()" will stop thread execution and if response comes before 30 seconds then thread is already finished. So anyway this thread will die with in 30 seconds. I hope its clear to you. Let me know if you need more clarification.

Please also mark it solved if it worked for you.
normalsoft 29-Oct-13 9:44am    
thanks for your great support. It is working fine for me. but, in the client machine, a verifone has been connected. Actually we are trying to implement the credit card swiping and have socket mechanism for handling the verifone events. Previously, the control have been gone to the verifone device when we call the external function in the dll. Now, it is not happening. using thread may cause any issues or problem with my code ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900