如何控制vb .net中的响应超时? [英] How to control response timeout in vb .net ?

查看:242
本文介绍了如何控制vb .net中的响应超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个vb .net windows应用程序。在这,我正在调用外部函数并接收响应。大部分情况都是有效的。但在某些客户端计算机中,响应并未在适当的时间到来。这需要更多时间。我的问题是如何设置超时,例如。 30秒,所以我将处理不响应的响应,并应继续下一步。



部分代码如下所示。



'我的回复课

Dim MyResponse as new clsResponse



'调用返回响应的外部函数。

MyResponse = Obj.SendRequest(MyRequest)



''有些代码在这里

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

推荐答案

对于这种要求,您可以使用System.Threading.Tasks.Task课程,在其中您可以提供超时,请查看以下链接



< a href =http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx>任务类 [ ^ ]



以下是我在C#中测试的示例代码段,用于回复您的问题。

使用线程添加第二个逻辑,希望它能帮助

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;
                
            }
        }


这篇关于如何控制vb .net中的响应超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆