I am trying to learn networking by making my own little network library. Currently I have a list of new clients that a code like this can use. The NewClientConnected() method returns true if there's anything in the list and removes the first element.
ConnectedClient newClient; // ConnectedClient is my class storing it's socket and stream and all that.
if(NewClientConnected(out newClient)
{
...handling new client, notification, etc...
}
Same goes for checking for new packets (prefixed slice of stream). I sought to take it to the next level and attempt to raise events when things like this happen and started with new connections. Problem is, the event is raised from another thread, causing an exception as the event is handled in the form1.cs.
How can I make sure the event is handled by the main thread from a static non-control class? Should I just keep doing what I do? (snippet above) I hear the idea of a consumer-produces relationship, it would still require a timer (uses its own thread) or another thread if I recall correctly.
I have tried to google it and look on here, but everyone seems to have a different problems. It is in a library project while another projects (my server and client test) is referring to it.
Thanks in advance.