473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

C# Code example - Question

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.

Nov 22 '05 #1
3 1527
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."

Nov 22 '05 #2
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 :-)

"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:dX*************@TK2MSFTNGXA01.phx.gbl...
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."

Nov 22 '05 #3
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."

Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
by: grün | last post by:
The MSDN techdocs are somewhat limited on this and I wanted more information. Is there any resource that says definitively which is faster /O2 or /Ox and by how much?
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
13
by: Skybuck Flying | last post by:
Hi, I would like to split main.c which contains all code into the following Step 1. Machine.h Step 2. drand.h and drand.c Step 3. nrand.h and nrand.c Main.c then only contains some test...
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
66
by: Jon Skeet [C# MVP] | last post by:
I'm sure the net will be buzzing with this news fairly soon, but just in case anyone hasn't seen it yet: Microsoft are going to make the source code for the .NET framework (parts of it,...
8
by: joshapalooza | last post by:
Hi all, What I'm trying to do is two-fold. We'll tackle one and then the other, or whichever is easiest. First Question/Problem: Is it possible to write VBA code that would change a text...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.