循环中的异步操作 [英] Async operations in loop

查看:84
本文介绍了循环中的异步操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用数据报套接字从两个ip地址从我的c ++ metro应用程序进行多播。 ip地址存储在向量中。以下是发送组播请求的代码。 StartMulticast()函数启动多播。

 std :: vector< String ^> IpArray; 
IpArray.push_back(ip1);
IpArray.push_back(ip2);

std :: for_each(begin(IpArray),end(IpArray),[this](String ^ ips)
{
create_task([this,ips]()
{

IAsyncAction ^ Action = StartMulticast(ips);
task< void> Task = create_task(Action);
Task.then([this]()
{
})。wait();
});
});

下面是实际的StartMulticast函数。

 Windows :: Foundation :: IAsyncAction ^ MainPage :: StartAsync1(String ^ ip)
{
return create_async([this,ip] ()
{

DatagramSocket ^ Socket = ref new DatagramSocket();
Socket-> Control-> QualityOfService = SocketQualityOfService :: Normal;
Socket- > MessageReceived + = ref new TypedEventHandler< DatagramSocket ^,DatagramSocketMessageReceivedEventArgs ^>(this,& MainPage :: MessageReceived);
HostName ^ hst = ref new HostName(ip);
task< void> (Socket-> BindEndpointAsync(hst,""))。然后([this,Socket](任务< void> previousTask)
{
try
{
previousTask.get();
HostName ^ MultiCastHost = ref new HostName(" mulicast ip");
Socket-> JoinMulticastGroup(MultiCastHost);
task< IOutputStream ^>(Socket-> GetOutputStreamAsync(MultiCastHost," port"))。then([this,Socket](task< IOutputStream ^> stream)
{
尝试
{
IOutputStream ^ MyStrem = stream.get();
String ^ MesageIDString ="" ;;
//要在网络上发送的Fille消息。
DataWriter ^ Writer = ref new DataWriter(MyStrem);
Writer-> WriteString(MesageIDString);
try
{
task< unsigned int>(Writer-> StoreAsync())。then([this,Socket](task< unsigned int> WriteTask)
{
try
{
WriteTask.get();
task< ; bool>(Socket-> OutputStream-> FlushAsync())。
then([this](task< bool> bVal)
{
try
{
bVal.get();
}
catch(Exception ^ e)
{
String ^ ErrorMsg = e-> Message;
}
})。wait();

}
catch(Exception ^ Exp)
{
String ^ ErrorMsg = Exp-> Message;
}
})。wait();
}
catch(Exception ^ Exp)
{
String ^ ErrorMsg = Exp-> Message;
}
}
catch(Exception ^ Exp)
{
String ^ ErrorMsg = Exp-> Message;
}
})。wait();
}
catch(...)
{
}
})。wait();

});
}




问题是当我运行应用程序时,MessageReceived()回调没有被调用,所以我没有收到任何回复。但它在循环中使用单个ip而不是两个ips。可能是因为本地套接字对象超出了范围。看起来好像是
的时间问题。可能有什么不对?在这种情况下,在循环中调用异步操作的最佳做​​法是什么,特别是当在另一个回调中接收到对请求的响应时,例如M essageReceived()
函数在这里。



解决方案

< blockquote>

HI,


如何为不同的套接字使用不同的回调函数。


在循环中,两个套接字将使用一个回调函数。

Socket-> MessageReceived + =


我认为这会引起问题。


祝你好运,

Jesse


I am doing multicast from my c++ metro app from two ip addresses using datagram socket. The ip addresses are stored in a vector .The below is the code of sending the multicast request. The StartMulticast() function starts the multicast.

	std::vector<String^>IpArray;
	IpArray.push_back( ip1);
	IpArray.push_back( ip2 );

	std::for_each( begin( IpArray), end( IpArray),[this]( String^ ips )
	{		
		create_task([this,ips]()
		{
			
				IAsyncAction^ Action = StartMulticast( ips );
				task<void> Task = create_task( Action );
				Task.then([this]()
				{
				}).wait();
		});
	});

Below is the actual StartMulticast function.

Windows::Foundation::IAsyncAction^ MainPage::StartAsync1( String^ ip )
{
	return create_async([this,ip]()
	{

					DatagramSocket^ Socket = ref new DatagramSocket();
					Socket->Control->QualityOfService = SocketQualityOfService::Normal;
					Socket->MessageReceived += ref new TypedEventHandler<DatagramSocket^, DatagramSocketMessageReceivedEventArgs^>(this, &MainPage::MessageReceived);
					HostName^ hst = ref new HostName( ip );
					task<void>(Socket->BindEndpointAsync( hst,"" )).then([this,Socket] (task<void> previousTask)
					{		
						try
						{		
							previousTask.get();
							HostName^ MultiCastHost = ref new HostName( "mulicast ip" );
							Socket->JoinMulticastGroup( MultiCastHost );
							task<IOutputStream^>(Socket->GetOutputStreamAsync( MultiCastHost, "port")).then([this,Socket] ( task<IOutputStream^> stream )
							{
								try  
								{
									IOutputStream^ MyStrem = stream.get();					
									String^ MesageIDString = ""; 
									// Fille  message to be sent on network.
									DataWriter^ Writer = ref new DataWriter( MyStrem );
									Writer->WriteString( MesageIDString );
									try
									{
										task<unsigned int>(Writer->StoreAsync()).then([this,Socket]( task<unsigned int> WriteTask )						
										{
											try
											{
												WriteTask.get();
												task<bool>(Socket->OutputStream->FlushAsync()).
												then([this](task<bool> bVal)
												{
													try
													{
														bVal.get();
													}
													catch(Exception^ e)
													{
														String^ ErrorMsg = e->Message;
													}
												}).wait();

											}
											catch( Exception^ Exp )
											{
												String^ ErrorMsg = Exp->Message;
											}
										}).wait();
									}
									catch( Exception^ Exp )
									{
									String^ ErrorMsg = Exp->Message;
									}
								}
								catch(  Exception^ Exp )
								{
								String^ ErrorMsg = Exp->Message;
								}			
								}).wait();
						}
						catch( ... )
						{
						}
					}).wait();

	});
}


The problem is when I run the app the , MessageReceived() callback is not getting called so that I am not getting any replies. But it works single ip instead of two ips in a loop. May be because the local socket object goes out of scope. It looks like a timing issue. What could be wrong? What is the best practice of invoking async operation in a loop in this type of situations, especilly when response to the request is recevived in another callback like MessageReceived() function here.

解决方案

HI,

How about use different callback functions for different sockets.

In the loop two socket will use one callback function.
Socket->MessageReceived +=

I think it will be caused problem.

Best regards,
Jesse


这篇关于循环中的异步操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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