C# Screen-Share Project

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
-1
down vote

favorite
1












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.







share|improve this question





















  • You're missing the Globals.screensharing = true; assignment in the Pool version.
    – 1201ProgramAlarm
    Feb 11 at 1:01










  • Have you tested both versions?
    – t3chb0t
    Feb 11 at 8:25
















up vote
-1
down vote

favorite
1












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.







share|improve this question





















  • You're missing the Globals.screensharing = true; assignment in the Pool version.
    – 1201ProgramAlarm
    Feb 11 at 1:01










  • Have you tested both versions?
    – t3chb0t
    Feb 11 at 8:25












up vote
-1
down vote

favorite
1









up vote
-1
down vote

favorite
1






1





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.







share|improve this question













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.









share|improve this question












share|improve this question




share|improve this question








edited Feb 10 at 20:41









Jamal♦

30.1k11114225




30.1k11114225









asked Feb 10 at 20:31









lol

1365




1365











  • You're missing the Globals.screensharing = true; assignment in the Pool version.
    – 1201ProgramAlarm
    Feb 11 at 1:01










  • Have you tested both versions?
    – t3chb0t
    Feb 11 at 8:25
















  • You're missing the Globals.screensharing = true; assignment in the Pool version.
    – 1201ProgramAlarm
    Feb 11 at 1:01










  • Have you tested both versions?
    – t3chb0t
    Feb 11 at 8:25















You're missing the Globals.screensharing = true; assignment in the Pool version.
– 1201ProgramAlarm
Feb 11 at 1:01




You're missing the Globals.screensharing = true; assignment in the Pool version.
– 1201ProgramAlarm
Feb 11 at 1:01












Have you tested both versions?
– t3chb0t
Feb 11 at 8:25




Have you tested both versions?
– t3chb0t
Feb 11 at 8:25










1 Answer
1






active

oldest

votes

















up vote
0
down vote













It depends on what you mean by "better". I'd use a Thread since the job is constantly running.



A Thread object will be a foreground thread, which will prevent the application from terminating as long as it is running.



Task.Run will queue the job up to run on a thread pool thread. It may not start running immediately, and it will be a background thread, so it will not prevent the application from terminating if all other foreground threads have terminated.



The number of threads that a thread pool can run is limited, so if your job is always using one of them that increases the possibility that some other task will run late. Or possibly your job will be delayed while some other task that is part of the thread pool runs.






share|improve this answer





















  • Mhmm... but you can also make the tast LongRunning...
    – t3chb0t
    Feb 11 at 8:27










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f187278%2fc-screen-share-project%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













It depends on what you mean by "better". I'd use a Thread since the job is constantly running.



A Thread object will be a foreground thread, which will prevent the application from terminating as long as it is running.



Task.Run will queue the job up to run on a thread pool thread. It may not start running immediately, and it will be a background thread, so it will not prevent the application from terminating if all other foreground threads have terminated.



The number of threads that a thread pool can run is limited, so if your job is always using one of them that increases the possibility that some other task will run late. Or possibly your job will be delayed while some other task that is part of the thread pool runs.






share|improve this answer





















  • Mhmm... but you can also make the tast LongRunning...
    – t3chb0t
    Feb 11 at 8:27














up vote
0
down vote













It depends on what you mean by "better". I'd use a Thread since the job is constantly running.



A Thread object will be a foreground thread, which will prevent the application from terminating as long as it is running.



Task.Run will queue the job up to run on a thread pool thread. It may not start running immediately, and it will be a background thread, so it will not prevent the application from terminating if all other foreground threads have terminated.



The number of threads that a thread pool can run is limited, so if your job is always using one of them that increases the possibility that some other task will run late. Or possibly your job will be delayed while some other task that is part of the thread pool runs.






share|improve this answer





















  • Mhmm... but you can also make the tast LongRunning...
    – t3chb0t
    Feb 11 at 8:27












up vote
0
down vote










up vote
0
down vote









It depends on what you mean by "better". I'd use a Thread since the job is constantly running.



A Thread object will be a foreground thread, which will prevent the application from terminating as long as it is running.



Task.Run will queue the job up to run on a thread pool thread. It may not start running immediately, and it will be a background thread, so it will not prevent the application from terminating if all other foreground threads have terminated.



The number of threads that a thread pool can run is limited, so if your job is always using one of them that increases the possibility that some other task will run late. Or possibly your job will be delayed while some other task that is part of the thread pool runs.






share|improve this answer













It depends on what you mean by "better". I'd use a Thread since the job is constantly running.



A Thread object will be a foreground thread, which will prevent the application from terminating as long as it is running.



Task.Run will queue the job up to run on a thread pool thread. It may not start running immediately, and it will be a background thread, so it will not prevent the application from terminating if all other foreground threads have terminated.



The number of threads that a thread pool can run is limited, so if your job is always using one of them that increases the possibility that some other task will run late. Or possibly your job will be delayed while some other task that is part of the thread pool runs.







share|improve this answer













share|improve this answer



share|improve this answer











answered Feb 11 at 1:01









1201ProgramAlarm

2,5302618




2,5302618











  • Mhmm... but you can also make the tast LongRunning...
    – t3chb0t
    Feb 11 at 8:27
















  • Mhmm... but you can also make the tast LongRunning...
    – t3chb0t
    Feb 11 at 8:27















Mhmm... but you can also make the tast LongRunning...
– t3chb0t
Feb 11 at 8:27




Mhmm... but you can also make the tast LongRunning...
– t3chb0t
Feb 11 at 8:27












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f187278%2fc-screen-share-project%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods