C# Code example - Question

  • Thread starter Thread starter Tom Rahav
  • Start date Start date
T

Tom Rahav

Hello All,

I developed a client-server file transfer application using C# for my
studies.
Everything works fine, but I use the follwing code (which I've found
somewhere over the net) and I'm wondering why does this code contain the
Sleep method. I know what this method does, but don't find the reason why to
use it here (althoght it works :-)
Just to make it clearer, the server is waiting for client request, accept it
and handle the file transfer over new thread.

while(ClientForm.booServerActive)
{

if(oServerListener.Pending())

{

oTcpServer = oServerListener.AcceptTcpClient();

Thread oSendingThread = new Thread(new ThreadStart(SendingHandler));

oSendingThread.Start();

}

Thread.Sleep(60);

}

Thanks!

Tom.
 
Hi Tom,

In the loop, you have created another thread to send using Thread
oSendingThread = new Thread(new ThreadStart(SendingHandler));. The sleep
method makes the main thread sleep, so that the newly create thread will
get a chance to execute on the CPU.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
If I understand your statement correctly, you are saying that if there was
no Thread.Sleep on the main thread, the newly spawned thread would have no
chance to execute on CPU?
Something must had gone really bad with windows' context switching if it
were so :-)
 
Lebesgue,

I didn't say if if there was no Thread.Sleep on the main thread, the newly
spawned thread would have no chance to execute on CPU. It just makes the
new thread execute more quickly.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top