I am making an online communication application and I would like to process messages asynchronously. I found the asynchronous wait pattern useful when implementing a message loop.
Below I guess:
CancellationTokenSource cts=new CancellationTokenSource();
public Action<Member> OnNewMember;
async void NewMemberCallback(ConnectionController c, Member m, Stream stream){
MessageLoop(c, m,stream,cts.Token);
if(OnNewMember!=null)OnNewMember(m);
}
async Task MessageLoop(ConnectionController c, Member m, Stream stream, CancellationToken ct){
MemoryStream msgbuffer=new MemoryStream();
MemoryStream buffer2=new MemoryStream();
while(true){
try{
await ReceiveandSplitMessage(stream, msgbuffer,buffer2,ct);
DecodeandProcessMessage(msgbuffer);
catch( ...Exception ex){
c.ClientDisconnected(m);
return;
}
}
}
Then I got some warning that MessageLoop is not expected in NewMemberCallback. I really don't have to wait for the MessageLoop method, because the method does not return until the connection is disconnected. Is it good practice to use async like this? I heard that not expecting an asynchronous method is not good, but I also heard that I have to eliminate unnecessary expectations. Or is it even considered incorrect to use an asynchronous template for a message loop?