问题
Im making a server for a new game proyect and im using C# (.NET Framework v4.5) for both client and server.
Im currently using something like this to manage my players.
new Thread(() =>
{
connection.ListenAndSend();
}).Start();
public void ListenAndSend()
{
while(working)
{
if(someThingToRead)
{
//Listening logic here
//Here i call my event for dataReceived with the data i just read.
}
else if (queue.Count > 0)
{
Send(queue.Dequeue().Data); //Send data from the Queue
}
}
The use of a Queue is because i should not access the stream from different threads (it throws InvalidOperationException i think) so i implemented the queue to send data from the same thread.
I think this is mostly OK but im worried about the events.
The events are firing and working fine but i have not tested yet with multiple clients so...
- Is the event being executed in the same thread that the listener thread?
- Could i have problems with fields being modified by multiple threads at the same time? (Im more experienced in java for example and i remember something about Syncronize interface?)
- Its not done right now but later its possible that some event will end up calling the send method so adding data to the send Queue (But this should not be an issue right?)
This game will never be for a large number of players (probably between 2 and 8) if that matters.
Is there some serious problem im not seeing?
As im using the Queue im thinking it does not matter what thread do add the data but will my listening be stopped while it is doing the event code? (If its really on the same thread) and while that could not really be an issue will it be simultaneously accesing fields from multiple threads?
UPDATE:
Can i make all of this just with async/await?? i cant see how to make the listen loop that way but if its really possible please expand in an answer.
Also, this answer says the opposite.
UPDATE 2:
Now that i have tested my code and i know its working fine.
As pointed out in comments i could have done this using async/await with a lot less threads, but:
In which it would be better to use async/await instead of threads, keep in mind my game server should be able to take as much cpu/memory as needed and its fine if it does (Of course the threads are calling sleep when not doing job). And i would like my server to run the closer to real time as possible.
I also would like to point, the first time i made it to start processing my logic the serializing of the game map (around 60k objects) took around 600ms (and that will be for each client) so i ended up moving the serialize job to the socket thread.
With this in mind, who thinks it would be better to use Async/Await instead of new Threads
PD: Im only talking about my server, the game client is indeed using async/await to communicate with the server.
回答1:
You have 2 options , ether lock the variable (which is not recommended) How to lock a variable used in multiple threads
or use dispatcher for calling a method from another thread Using the C# Dispatcher
if you are not using WPF, you might take a look to this link : how do I convert wpf dispatcher to winforms
来源:https://stackoverflow.com/questions/46808853/using-events-on-multithreading-socket-server