im starting using async taks to create the simulation of a client and server responses, i made it work but now i want to have more control over the messages that every client sends, i got it by making a custom printLine to print in the console with colors and tags, but now, because the server and client runs asynchronously the messagges on the console overlaps:
[Server]> Wating for a conection...
[Client #0]> [Client #1]> Socket connected to -> [SomeIP]:11111
Socket connected to -> [SomeIP]:11111
[Server]> Text received -> Test Client<EOF>
[Client #1]> Message from server -> Test Server
[Server]> Wating for a conection...
[Server]> Text received -> Test Client<EOF>
[Client #0]> [Server]> Message from server -> Test Server
This is because my own printLine method is executed not in a single line of code, how can i do to run this method synchronously to wait until the execution completition of this method? Thanks and sorry for my english.
This is the method that runs the taks:
static async Task Start(){
Server.Server server = new Server.Server();
Client.Client client0 = new Client.Client(0);
Client.Client client1 = new Client.Client(1);
var serverTask = Task.Run(() => server.ExecuteServer());
var clientTask0 = Task.Run(() => client0.ExecuteClient());
var clientTask1 = Task.Run(() => client1.ExecuteClient());
await clientTask1;
await clientTask0;
Console.WriteLine("Done");
}
and this is my ownn PrintLine method
void printLine(string line){
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("[Client #" + clientID + "]>\t");
Console.ResetColor();
Console.WriteLine(line + "\n");
}