I'm new to this area of C# and, frankly, struggling to grok the paradigm. It seems I'm not alone (Where does async and await end? Confusion, http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html)
In my case I am writing a small TCP server in a C# library, in essence the TCP server should run in its own thread and post data back to the application via a provided callback. So we might have an entrypoint into the library:
class MyServer
{
void StartServerRunningAsync(Callback callback)
{
this.callback = callback; //calls back into unmanaged via 'magic' COM interop each time a TCP client posts interesting data
StartRunningThread(); //this creates a thread to run the server and returns
}
void StartRunningThread()
{
new Thread(new ThreadStart(Run)).Start();
}
void Run()
{
//do standard TCP async stuff treating `Run` like a local `Main`
}
}
This library will be used from an unmanaged C++ application (specifically via COM in this case) which runs the server in the background. So I don't think StartServerRunning
can/should be async
but then that means I'm stuck/confused how I can use async
/await
at all since it propagates through your whole stack based on the links above.
Is this actually an issue or have I misunderstood something fundamental? How can TPL be encapsulated in this way?