if you know a thing about what goes on under the hood and how a program works (aside from the code) and have studied the native windows dlls a bit, you should know that'll lead to utter disasterPut a timer on the action packet sit like foreach second you sit the stamina goes up by 20 or something.
if you have 100 people on, that means you have (100+x) threads running, one for each player
one timer per player for anything = not good
Does it matter? o.0 im not coding my own source so i don't need to make up perfect way's to get stamina working. Its only for LegendX ... just another member on rz =)
Also if he knew how to use a timer, he would not ask how to do it cause he would know some sort of way to get it working (even if its not the best way)
google's teaching me (yes'sir'e) and ive had previous knowledge of how computers work due to my research.ahh u seem to be learning fast lol where r u learning from or who's teaching u? or do u got previous experience with coding or something? :P
yes it matters thats like telling someone the most painful way of doing something when there's a much less painful wayDoes it matter? o.0 im not coding my own source so i don't need to make up perfect way's to get stamina working. Its only for LegendX ... just another member on rz =)
Then what are the alternative to timers? Thread.Sleep (apparently, that one sucks for being unaccurate)?
Never use Thread.Sleep because that stops every thread from running. Causes 100% CPU Usage and will freeze the entire server.
Then what should be used in multithreading to cause a SINGLE thread pause (or simply a timeout that does an action)?
no, im not saying timers are bad, but limit them as much as you can;
i prefer system.threading.timer it's nice and simple.
one global timer handling all major things, it would preform a 'foreach' loop on the clients and append to all the minor things such as removing stigma, giving stam, etc.
@ Thread.Sleep
Thread.Sleep ONLY freezes the thread that it's in, for example;
Code:Console.WriteLine("Hello"); new Thread(new ThreadStart( delegate() { Thread.Sleep(1000); Console.WriteLine("Goodbye"); } )).Start(); Console.WriteLine("I have to go bye!"); // output: // Hello // I have to go bye! // Goodbye
don't use Thread.Sleep, do this
what's your MSN? I can probably help you with more.Code:public static void Sleep(DateTime Time) { while (Time > DateTime.Now) Thread.Sleep(1); }
I adapted it for C#, it must work >.<
Here is my routine to check the packetsCode:public static void Sleep(int millisecondsTimeout) { DateTime Time = DateTime.Now.AddMilliseconds(millisecondsTimeout); while (Time > DateTime.Now) { // In C#, nothing is needed to do a while loop } }
As you can see, the timer is far more adapted than doing a while loop with Thread.Sleep(3000);Code:// Start Check monitor - Check every 3 seconds Timer state_monitor = new Timer(StateCheck, null, 0, 3000); // ---------------------- private void StateCheck(object data) { try { Monitor.Enter(StateObjects); for (int a = 0; a < StateObjects.Length; a++) { if (StateObjects[a].ID == -1 && (StateObjects[a].Connected == true || StateObjects[a].StateSocket != null)) { DestroyState(StateObjects[a]); continue; } if (StateObjects[a].StateSocket != null && StateObjects[a].TimeStamp.AddSeconds(30) < DateTime.Now) { StateObjects[a].StateSocket.BeginDisconnect(false, new AsyncCallback(Disconnect), StateObjects[a]); Console.WriteLine(StateObjects[a].StateSocket.RemoteEndPoint + "/ID:" + a + " has been disconnected due to the 30 seconds timeout."); continue; } } Monitor.Exit(StateObjects); } catch (Exception e) { Console.Beep(); Console.WriteLine("CRITICAL ERROR IN StateCheck()!!!\n" + e.ToString()); } }
Your point is, just because you have your own way of doing timers (system.threading.timer) that isn't the main scripts that cause disaster. People have there own way of doing things. There are alot more things in issue here, sockets, packet structure, memory Its a bit like the people that say C# could'nt be anything as good as c++ simply the main reason being c# is managed memory and c++ isn't, i think infamous proved them wrong =).
Maybe that system.threading.timer is alot better, System.timers ill use, prolly use DateTime i dunno iv allready quit conquer xD.