|
You last visited: Today at 05:11
Advertisement
Delaying some codes
Discussion on Delaying some codes within the CO2 Private Server forum part of the Conquer Online 2 category.
06/21/2013, 00:39
|
#1
|
elite*gold: 0
Join Date: Aug 2009
Posts: 18
Received Thanks: 0
|
Delaying some codes
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.
Thanks in advance
|
|
|
06/21/2013, 01:05
|
#2
|
elite*gold: 0
Join Date: Aug 2007
Posts: 1,525
Received Thanks: 230
|
Threading is good OR you can use Timer
|
|
|
06/21/2013, 01:31
|
#3
|
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,379
|
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
|
#4
|
elite*gold: 0
Join Date: Aug 2009
Posts: 18
Received Thanks: 0
|
Quote:
Originally Posted by shadowman123
Threading is good OR you can use Timer
|
Thanks for you.
Quote:
Originally Posted by pro4never
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 (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!?
|
|
|
06/21/2013, 09:07
|
#5
|
elite*gold: 0
Join Date: Mar 2013
Posts: 118
Received Thanks: 95
|
Quote:
Originally Posted by ahmedragab
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.
|
|
|
06/21/2013, 10:22
|
#6
|
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,379
|
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
|
#7
|
elite*gold: 0
Join Date: Aug 2007
Posts: 1,525
Received Thanks: 230
|
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
|
#8
|
elite*gold: 0
Join Date: Sep 2012
Posts: 51
Received Thanks: 32
|
Quote:
Originally Posted by ahmedragab
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
|
#9
|
elite*gold: 0
Join Date: Dec 2012
Posts: 1,761
Received Thanks: 950
|
@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
|
#10
|
elite*gold: 0
Join Date: Jan 2008
Posts: 1,443
Received Thanks: 1,175
|
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
|
#11
|
elite*gold: 0
Join Date: Aug 2009
Posts: 18
Received Thanks: 0
|
Quote:
Originally Posted by Smaehtin
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.
|
|
|
06/23/2013, 22:12
|
#12
|
elite*gold: 0
Join Date: Dec 2012
Posts: 1,761
Received Thanks: 950
|
Have fun.
|
|
|
06/24/2013, 11:09
|
#13
|
elite*gold: 0
Join Date: Aug 2009
Posts: 18
Received Thanks: 0
|
Quote:
Originally Posted by Super Aids
Have fun.
|
Thanks.But I know most of this basics
|
|
|
06/24/2013, 11:20
|
#14
|
elite*gold: 0
Join Date: Oct 2006
Posts: 273
Received Thanks: 86
|
Quote:
Originally Posted by ahmedragab
Thanks.But I know most of this basics
|
No ... you dont .
|
|
|
06/24/2013, 17:22
|
#15
|
elite*gold: 0
Join Date: Jul 2008
Posts: 874
Received Thanks: 238
|
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
|
|
|
 |
Similar Threads
|
Removing Skill Book Delaying With GM Code Very Simple... (Without DIFF + C++ Code.)
05/29/2012 - Metin2 PServer Guides & Strategies - 8 Replies
How2 Enable:
Very simple, login with GM char and write this code:
/e no_read_delay 1
---------
e = change event flag
|
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...
|
[B] iTunes Codes [S] Nintendo VIP Sterne Codes & Coke Fridge Codes
01/01/2011 - Trading - 5 Replies
Bitte löschen.
|
All times are GMT +1. The time now is 05:14.
|
|