|
I's probably use a ManualResetEvent instead of sleeping a thousand times per second - it's more accurate and less of a hack.
ManualResetEvent m = new ManualResetEvent(true);
while(true)
{
m.WaitOne();
if(numberOfConnections < 100)
{
Socket s = listenerSocket.Accept();
...
}
else
{
m.Reset();
}
}
And somewhere at a place where you close the connections, do m.Set() if the number of connections is lower than 100 again.
But beware, I just woke up, so don't trust this code or even this idea
|
|
|
|
|
Hi Harold,
harold aptroot wrote: sleeping a thousand times per second
you are an optimist then. You may be interested in my article on timers.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 8:59 AM
|
|
|
|
|
Lol yea I exaggerated - how about: Up to a thousand times per second but it will never be that much due to the bad timer resolution.
But the idea is the same.. sleeping "many" times per second isn't a good idea.
|
|
|
|
|
Hmm I think I'll have to be carefull with the m.Set()
but worth trying...
|
|
|
|
|
Use the asynchronous fashioned methods. Like BeginXXX() and EndXXX(). There is a Socket.BeginAccept[^] available.
|
|
|
|
|
I don't understand how that would spare the cpu
or would help me to get rid of Sleep(x) ;?
I just want my loop to block, so it won't accept any new connections
until the older ones are gone... without using any cpu time.
|
|
|
|
|
See MSDN[^] got a sample.
|
|
|
|
|
Hi,
invader82 wrote: Has anyone a better idea how to keep the cpu down?
Use an event-driven scheme if one is available: getting a signal something must be done, is more efficient than having a loop polling whether somethind should be done all the time. (BTW: Thread.Sleep(1) will take 10 or more msec on average, see my timers article).
If you can't use an event-driven scheme (because no event is available, or you don't trust it sufficiently), implement a polling loop, however give it a delay that fits with your needs, e.g. if the client is a human typing/clicking in a browser, 50 msec would be good enough.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 8:59 AM
|
|
|
|
|
I rather wanted something that would really block that loop until
the number of connections drops back below a certain value.
I'll try the ManualResetEvent that harold proposes...
I think it's a "cleaner" solution for the problem
than sleep()
|
|
|
|
|
Yes, ManualResetEvent supports one of several event-driven schemes .NET provides.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 9:00 AM
|
|
|
|
|
Hi,
I'll have to prepare a software in C# that using the software we can call from pc and receive call by pc. obviously a modem is connected with the pc.
I want some idea about this project. if possible then send me some sample code or url for reference..
Please help me...
Thanks
Rizvi.
|
|
|
|
|
rizvi_codeproject wrote: url for reference..
Oh, you should have said that right away.
Here: http://www.google.com/[^]
Kristian Sixhoej
"You can always become better." - Tiger Woods
[ My blog ]
|
|
|
|
|
hi,i'm trying to get the details of each process in c# but some of its attributes didnt return anything, (i made object of class Process --> p):
1) p.ExitTime;
the ERROR: process was not started by this object
2) p.startInfo.WorkingDirectory;
but it didnt return the directory of the process,just a empty string
3) p.username;
empty string, i searched the net and tried many codes but nothing helped me ,they all return the usename of the computer, i want something like task manager's username:system, local service ....
And one more thing :how does taskmanager calculate cpu usage?
|
|
|
|
|
Hi,
the MSDN documentation on the Process class includes this paragraph, it may be relevant to you:
"The process component obtains information about a group of properties all at once. After the Process component has obtained information about one member of any group, it will cache the values for the other properties in that group and not obtain new information about the other members of the group until you call the Refresh method. Therefore, a property value is not guaranteed to be any newer than the last call to the Refresh method. The group breakdowns are operating-system dependent."
shefa' isied wrote: how does taskmanager calculate cpu usage?
the Microsoft guy who wrote Task Manager is good friends with the Microsoft guy who wrote the scheduler, so he is allowed a peek in some system tables we mortals are not supposed to access.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 9:01 AM
|
|
|
|
|
Hi
I would like to drag my contact(in icon view) to any location point by mouse. The behavior is same as icon view, without auto arrange and without align to grid in window explorer. I found all drag and drop example use index to represent location and the drop is to nearest location(by index).
Anyone know how to implement it?
Thanks first.
Regards,
PeiYang
|
|
|
|
|
As no-one has answered your question, I'll have a go.
Take a look at Control.MousePosition and see if that helps you.
Good Luck.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Thanks. I already have a look on Control.MousePosition.
I can get the mouse position but i can't drop it at the specific location.
All example that i found is:
1. Get the drag item detail and index when drag.
2. Get the mouse position when drop.
3. Base on mouse position, the application convert the mouse position to index. Every index represent a rectangle. Any point in the rectangle will use same index.
4. The item will insert at the rectangle represent by the index.
The function that i found all will draw or insert picture base on index and not actual mouse position. It need to pass in index and not mouse position. So, anyone have any suggestion?
Regards,
Pei Yang
|
|
|
|
|
Hello!
I'm trying to have a custom-sorter class to sort my objects.
I want my objects to always be in a certain order(not alphabetically).
In my Compare(object a, object b) method I check which objects they are and return 1 or -1 depending on which one I want to come first. How exactly does it work, do I return -1 if I want object a before object b in my collection? It doesn´t seem consistent to me, some values get sorted correctly and some don´t.
|
|
|
|
|
You probably should post your code and your test data.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
-1 if a less than b,
0 if a equal b,
1 if a greater than b.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
I wrote you a little sameple which might help.
I have 3 types of pet:
public class Dog { }
public class Cat { }
public class Rabbit { }
I dont want to order these alphabetically, I want to order them in order of my preference of them as pets. I like cats more than dogs, and dogs more than rabbits. I could write a comparer to always order a list of pets in my prefered order:
public class MyPetPreferenceComparer : IComparer
{
#region IComparer Members
public int Compare(object x, object y)
{
if (x.GetType() == y.GetType())
return 0;
if (x is Cat)
{
return -1;
}
if(x is Dog)
{
if (y is Rabbit)
return -1;
return 1;
}
if (x is Rabbit)
return 1;
return 0;
}
#endregion
}
Now the test code:
ArrayList list = new ArrayList();
list.Add(new Rabbit());
list.Add(new Cat());
list.Add(new Dog());
list.Add(new Cat());
list.Add(new Rabbit());
list.Add(new Dog());
list.Sort(new MyPetPreferenceComparer());
foreach (object obj in list)
{
Console.WriteLine(obj.GetType().Name);
}
Outputs:
Cat
Cat
Dog
Dog
Rabbit
Rabbit
One thing Id add - nowdays we tend to prefer the generic versions of these sorts of interface so try to use IComparer<T> in System.Collection.Generics
|
|
|
|
|
|
Hi,
an int comparer could just return the difference, as in:
public static int Compare(int a, int b) {return a-b;}
BTW: you MUST return zero when both objects are identical.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 9:02 AM
|
|
|
|
|
Hi All,,,
I Get A Data From a sql server2005 Veiw/table using Dataset And Data Adapter,,,
I Want A C# Code To Return All constraints and Foreign Key At This Veiw/table.
please help me about this code
thanks all;
|
|
|
|
|
Do not repost the same vague question.
|
|
|
|