Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何验证异步调用的某些回调(按顺序)_C#_.net_Asynchronous - Fatal编程技术网

C# 如何验证异步调用的某些回调(按顺序)

C# 如何验证异步调用的某些回调(按顺序),c#,.net,asynchronous,C#,.net,Asynchronous,我正在开发一些系统,包括从一些服务器读取消息、注册事件处理程序以及依次调用处理程序 我使用的是.NET3.5,所以请注意,来自.NET4或async的解决方案不可用 代码与此类似(为了简洁起见删除了一些片段): 目前,我有一个同步(阻塞)实现,其中有一个读取消息并触发正确处理程序的循环 这样,我可以轻松地验证事件是否按顺序触发 我想切换到异步实现,其中事件将异步触发,而不是在循环中读取它们 如何在此实现中验证事件是否按顺序接收?是否有解决此问题的标准解决方案?或其他解决方案,请与普通解决方案一样

我正在开发一些系统,包括从一些服务器读取消息、注册事件处理程序以及依次调用处理程序

我使用的是.NET3.5,所以请注意,来自.NET4或async的解决方案不可用

代码与此类似(为了简洁起见删除了一些片段):

目前,我有一个同步(阻塞)实现,其中有一个读取消息并触发正确处理程序的循环

这样,我可以轻松地验证事件是否按顺序触发

我想切换到异步实现,其中事件将异步触发,而不是在循环中读取它们

如何在此实现中验证事件是否按顺序接收?是否有解决此问题的标准解决方案?

或其他解决方案,请与普通解决方案一样,使用
ForEach
循环中的async calls集合


有关示例,请参阅和在线文档。

我认为一般设计应该是这样的(伪代码):

int currentIndex=0;
List lst=getList();
executor.BeginAsyncTask(lst[currentIndex],AsyncTaskCallBack)
函数AsyncTaskCallBack(结果)
{
如果(result.IsOK)
{
currentIndex+=1;
如果(当前索引<第一计数)
executor.BeginAsyncTask(lst[currentIndex],AsyncTaskCallBack)
}
}
使用该设计,您将仅在上一个对象完成时处理下一个对象,并且它不会阻止您的代码。
如果您想在ASP.NET中执行,执行过程可能会有所不同……当您等待AsyncTaskCallBack执行时,请求处理可能已完成并已发送到浏览器。

在这种情况下,这将不起作用。我只向服务器提交一条消息。服务器用几条消息响应,我想从中验证一些特定的消息,这些消息将按特定顺序接收。@liortal Ok,看起来我误解了您的需要:)试试看。使用ManualResetEvent创建一个与同步事件异步工作的类并不难。
        // Loop until receiving timeout.
        while (!timedOut)
        {
            if ((message = connection.Receive) != null)
            {                    
                if (message.Id == Error.MessageId)
                {
                    throw new Exception("Something wrong happened!!");
                }

                // This calls the event handler (if registered).
                HandleEvent(message);

                if (finishedEvents)
                {
                    // Finished.
                }
                else
                {
                    // If message matches the requested message.
                    if (message.Id == expectedEvents[index].MessageId)
                    {
                        index++;
                    }

                    // Finished processing all messages.
                    if (index == expectedEvents.Length)
                    {
                        finishedEvents = true;

                        continue;
                    }
            }
            else
            {
                 break;
            }
      }
int currentIndex = 0;
List<myObject> lst = getList();
executor.BeginAsyncTask(lst[currentIndex], AsyncTaskCallBack)
function AsyncTaskCallBack(result)
{
   if(result.IsOK)
   {
      currentIndex+=1;
      if(currentIndex < lst.Count)
         executor.BeginAsyncTask(lst[currentIndex], AsyncTaskCallBack)
   }
}