I've recently been working on a screen-sharing/remote desktop project in C# .NET Framework 4.5. How it works is when the start button is pressed, the client will send a command to the server to begin sending screenshots until the screen-sharing bool is false (i.e the 'Stop' button is pressed).
All data is prefixed with a byte identifying what command it is, and the following 4 bytes are used to measure the length of the packet.
My question is, should I be using a Thread or a Task for this job? I need one or the other, otherwise the client's UI freezes.
Below I am using a Thread
:
private void ProcessCommand(byte command)
{
if (command == Commands.StartRemoteDesktop && !Globals.screensharing)
{
new Thread(StartSharing).Start();
}
}
private void StartSharing()
{
Globals.screensharing = true;
while (Globals.screensharing)
{
Methods.SendCapture();
}
}
And over here is how it looks with a Task
:
private void ProcessCommand(byte command)
{
if (command == Commands.StartRemoteDesktop && !Globals.screensharing)
{
Task.Run(() =>
{
while (Globals.screensharing)
{
Methods.SendCapture();
}
});
}
}
I'm unsure what would be the better option here.
Globals.screensharing = true;
assignment in the Pool version. \$\endgroup\$ – 1201ProgramAlarm Feb 11 '18 at 1:01