Delaying some codes

06/21/2013 00:39 ahmedragab#1
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 (
== 0)
{
if (
DateTime.Now Time.AddMilliseconds(4000))
                    {
                                
1;
                    }
                    if (
== 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.

Thanks in advance
06/21/2013 01:05 shadowman123#2
Threading is good OR you can use Timer
06/21/2013 01:31 pro4never#3
It depends what you're trying to do really.


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.
06/21/2013 07:32 ahmedragab#4
Quote:
Originally Posted by shadowman123 View Post
Threading is good OR you can use Timer
Thanks for you.

Quote:
Originally Posted by pro4never View Post
It depends what you're trying to do really.


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 (
== 0)
                                        {
                                            if (
DateTime.Now Time.AddMilliseconds(4000))
                                            {
                                                
1;
                                            }
                                            if (
== 1)
                                            {
                                                if (
client.Entity.== && client.Entity.== 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!?
06/21/2013 09:07 Smaehtin#5
Quote:
Originally Posted by ahmedragab View Post
But it doesn`t work ,when I move from my place it runs the body of true condition of If . Any one can explain!?
[Only registered and activated users can see links. Click Here To Register...]

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.
06/21/2013 10:22 pro4never#6
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.
06/21/2013 10:47 shadowman123#7
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 ?
06/21/2013 14:54 urgabel#8
Quote:
Originally Posted by ahmedragab View Post
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.
06/21/2013 20:11 Super Aids#9
@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.
06/21/2013 22:40 CptSky#10
Nice loop that will :

a) heat all the CPU
b) make an enormous lag as you'll block the packet handler
06/23/2013 21:21 ahmedragab#11
Quote:
Originally Posted by Smaehtin View Post
[Only registered and activated users can see links. Click Here To Register...]

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 View Post
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 View Post
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 View Post
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 View Post
@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 View Post
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.
06/23/2013 22:12 Super Aids#12
[Only registered and activated users can see links. Click Here To Register...]

Have fun.
06/24/2013 11:09 ahmedragab#13
Quote:
Originally Posted by Super Aids View Post
[Only registered and activated users can see links. Click Here To Register...]

Have fun.
Thanks.But I know most of this basics
06/24/2013 11:20 Dr.unreal#14
Quote:
Originally Posted by ahmedragab View Post
Thanks.But I know most of this basics
No ... you dont .
06/24/2013 17:22 abdoumatrix#15
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