Hello everybody here
I want to ask about the best code for delaying some codes
I am begginer and I tried some codes like:
PHP Code:
System.Threading.Thread.Sleep(4000);
And like:
PHP Code:
int Z = 0; DateTime Time = new DateTime(); Time = DateTime.Now; while (Z == 0) { if (DateTime.Now > Time.AddMilliseconds(4000)) { Z = 1; } if (Z == 1) { //Codes here } }
i know it isn`t good at all but it works fine.
the problem with this two codes that it makes the Ping Very high.
Timers = good for simple delays on actions but could get very messy if you're not careful and keep spamming new timers for absolutely everything.
Threads = fine for continuous logic checks but need to be very careful of cross thread calls, thread safety of collections/deadlocks, etc.
Keep in mind that using thread.sleep blocks the entire thread for the full duration. Anything else running on the thread will completely freeze. If you're calling it in a bad place you might be freezing your entire server for the full 4 seconds which will be very, very bad ahaha.
Timers = good for simple delays on actions but could get very messy if you're not careful and keep spamming new timers for absolutely everything.
Threads = fine for continuous logic checks but need to be very careful of cross thread calls, thread safety of collections/deadlocks, etc.
Keep in mind that using thread.sleep blocks the entire thread for the full duration. Anything else running on the thread will completely freeze. If you're calling it in a bad place you might be freezing your entire server for the full 4 seconds which will be very, very bad ahaha.
I thought that, So I made the timer but in the two ways make the ping very high.
To tell you, I want to make a code that when you click on item it gives you a reward after 4 seconds but only if you haven`t moved from your position,so
i made that code:
PHP Code:
case 81708:
{
switch (npcRequest.OptionID)
{
case 0:
{
int X = client.Entity.X;
int Y = client.Entity.Y;
int Z = 0;
DateTime Time = new DateTime();
Time = DateTime.Now;
while (Z == 0)
{
if (DateTime.Now > Time.AddMilliseconds(4000))
{
Z = 1;
}
if (Z == 1)
{
if (client.Entity.X == X && client.Entity.Y == Y)
{
Network.GamePackets.NpcInitial2.DeleteNPC3(client.ActiveNpc);
client.Entity.ConquerPoints+= 10000;
}
else
{
dialog.Text("Don`t move To Get The Prize!.");
dialog.Option("Sorry.", 255);
dialog.Send();
}
}
}
}
}
break;
}
But it doesn`t work ,when I move from my place it runs the body of true condition of If . Any one can explain!?
But it doesn`t work ,when I move from my place it runs the body of true condition of If . Any one can explain!?
Although your code is terribly wrong, I see what you're trying to do. Don't ever put stuff like that in your packet handling code. You've basically created your own, completely useless version of Thread.Sleep except this abomination of yours actually doesn't sleep and will consume your CPU while waiting - That is if your code even worked in the first place, of course.
Best advice: Don't try to develop a private server when you have absolutely no clue what you're doing.
i think he understood nothing .. DUDE y do u use While although you can add just condition in Threads ( in Program.cs ) its Runs Every1 Sec so y dont u add ur cases there ?
I want to ask about the best code for delaying some codes [...] the problem with this two codes that it makes the Ping Very high.
I'm not very used yet to threads, but what I think is Events is the best option for that.
So, you need to separate the delay/checking from the action.
A thread checks for conditions, without delays, and triggers actions (raise events) when needed; when action is triggered, you (in the event handler) do whatever you need, without delays.
Although your code is terribly wrong, I see what you're trying to do. Don't ever put stuff like that in your packet handling code. You've basically created your own, completely useless version of Thread.Sleep except this abomination of yours actually doesn't sleep and will consume your CPU while waiting - That is if your code even worked in the first place, of course.
Best advice: Don't try to develop a private server when you have absolutely no clue what you're doing.
Quote:
Originally Posted by pro4never
That's not a timer, it's a near infinite loop which will max out the cpu until the condition to break the loop is met...
A timer would be like...
Timer t = new Timer();
t.Elapse += t_tick;
where t_tick is a function to call after the interval time.
Quote:
Originally Posted by shadowman123
i think he understood nothing .. DUDE y do u use While although you can add just condition in Threads ( in Program.cs ) its Runs Every1 Sec so y dont u add ur cases there ?
Quote:
Originally Posted by urgabel
I'm not very used yet to threads, but what I think is Events is the best option for that.
So, you need to separate the delay/checking from the action.
A thread checks for conditions, without delays, and triggers actions (raise events) when needed; when action is triggered, you (in the event handler) do whatever you need, without delays.
Quote:
Originally Posted by Super Aids
@OP I would suggest you learning to actual programming in C# before jumping into this, because at the look of this you have no idea what you're doing.
Thanks all for your replys,i understood.
But to tell you, I haven`t Solved the problem i need some ways not just say error.
The best reply now is
Quote:
Originally Posted by pro4never
That's not a timer, it's a near infinite loop which will max out the cpu until the condition to break the loop is met...
A timer would be like...
Timer t = new Timer();
t.Elapse += t_tick;
where t_tick is a function to call after the interval time.
that is a way and i will try it
And For "Smaehtin"
I don`t have a server and i won`t do
I just want to learn C#
I studied C++ Not Even VC++ So, I said i am begginer
Thanks for all.
why do u make it like when he click the fist click it become true and if he moved or jumped it become false and after 4 sec if he clicked again at the npc he get the prize
Coming soon: free purchase codes for sale + Discount codes for Origin as a bonus 11/22/2011 - Need for Speed World - 2 Replies In a few weeks, i would be selling 2 free purchase codes in this site. I do not know when, but when i acquire more codes now, i would be selling them.
As of now, there are no codes available. But there would be a bonus promo in the day of selling codes:
First to redeem the code posted wins 2 free purchases.
And for the others, you can buy more codes for $5 since others sold them for $20, thus, being a rip off.
My only goal atm is to reach $50, after reaching $50, i will still sell...