我正在尝试为即时消息实现一些协议,所以我使用的是Socket类。当Socket从服务器接收任何数据包时,我正在尝试启动一个事件,通知我的其他类有一些数据可以使用。但是当我试图从事件处理程序的方法调用方法时,我得到一个错误:“.NET Compact Framework不支持异步调用委托”
以下是我正在使用的一些代码。我在发生异常的地方留下了评论。
public class ClientSocket3
{
Socket _socket;
private const int ConnectionTimeout = 5000;
private static ManualResetEvent connectedDone = new ManualResetEvent(false);
public OnReceiveHandler3 OnReceive
{
get;
set;
}
public void Connect(IPEndPoint endPointAddress)
{
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += new EventHandler<SocketAsyncEventArgs>(args_Completed);
args.RemoteEndPoint = endPointAddress;
args.UserToken = _socket;
_socket.ConnectAsync(args);
if (connectedDone.WaitOne(ConnectionTimeout))
{
}
}
void args_Completed(object sender, SocketAsyncEventArgs e)
{
if (e.LastOperation == SocketAsyncOperation.Connect)
{
connectedDone.Set();
Debug.WriteLine("Connected");
}
else if (e.LastOperation == SocketAsyncOperation.Receive)
{
if (OnReceive != null)
{
OnReceive(this, null);
}
}
else if (e.LastOperation == SocketAsyncOperation.Send)
{
Debug.WriteLine("Sent... Waiting for answer");
_socket.ReceiveAsync(e);
}
}
public void Send(byte[] data)
{
SocketAsyncEventArgs sendArgs = new SocketAsyncEventArgs();
sendArgs.Completed += new EventHandler<SocketAsyncEventArgs>(args_Completed);
sendArgs.SetBuffer(data, 0, data.Length);
_socket.SendAsync(sendArgs);
}
public void Close()
{
_socket.Close();
}
}
public delegate void OnReceiveHandler3(object sender, byte[] receivedData);
class XYZ
{
private ClientSocket3 client;
public void Connect(IPEndPoint ipep)
{
try
{
client = new ClientSocket3();
client.Connect(ipep);
client.OnReceive += new OnReceiveHandler3(testReceived);
}
catch (SocketException se)
{
// todo
}
catch
{
throw;
}
}
private void Send(byte[] data)
{
_client.Send(data);
}
private void testReceived(object sender, byte[] e)
{
RecvData(e); **// here exception is thrown: Exception: .NET Compact Framework does not support invoking delegates asynchronously**
}
private void RecvData(byte[] data)
{
//....
}
}
从XYZ类调用Send()方法后立即抛出异常。当然在发送任何东西之前,我正在调用Connect方法,所以我已连接到服务器。
我尝试使用ThreadPool.QueueUserWorkItem()和System.Windows.Deployment.Current.Dispatcher.BeginInvoke()但没有运气 - 也许我正在以错误的方式使用它们。
任何想法如何解决这个问题? 提前谢谢。
更新
异常 - 堆栈:
at Test.IM.XYZ.testReceived(Object sender, Byte[] e)
at Test.Net.ClientSocket3.args_Completed(Object sender, SocketAsyncEventArgs e)
at System.Net.Sockets.SocketAsyncEventArgs.OnCompleted(SocketAsyncEventArgs e)
at System.Net.Sockets.SocketAsyncEventArgs.OnCompletedInternal(SocketAsyncEventArgs e)
at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSuccess(SocketError socketError, Int32 bytesTransferred, SocketFlags flags)
at System.Net.Sockets.Socket.AsyncCallback(IAsyncResult ar)
at System.Net.LazyAsyncResult.InvokeCallback(Boolean asyncCompletion)
at System.Net.Sockets.Socket.SocketAsyncRequestManager.WorkerThread.doWork()
at System.Net.Sockets.Socket.SocketAsyncRequestManager.WorkerThread.doWorkI(Object o)
at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
异常类:System.NotSupportedException。
在Data中是System.Collections.ListDictionaryInternal但count是0
答案 0 :(得分:0)
我不确定如何解决这个特定问题,但是出于您的目的,您可能需要查看UdpSingleSourceMulticastClient,它与lib一样是7.1的一部分。
祝你好运!