Adding a pause when using an item

08/11/2013 02:58 hacksaw#1
I am currently using the code below to add a pause inbetween when an item is clicked and when it performs the main code, The code below works but gives a cpu spike while its being bounced between the (tryagain) waiting for the timer to let it through, So was wondering if there is any better way of handling this pause?

Item Use Code:

Code:
 case 4000058:
        {
          client.Entity.LoadStamp = Time32.Now;
          client.Entity.LoadTime = 3;
  tryagain:
           if (client.Entity.LoadTime == 0)
                {
                     Game.Features.lock3.Open(client);
                }
               else
                {
                      goto tryagain;
                }
         }
   break;
Timer Handle in Entity.cs

Code:
if (Now >= client.Entity.LoadStamp.AddSeconds(client.Entity.LoadTime))
     {
          client.Entity.LoadTime = 0;
     }
08/11/2013 03:06 Korvacs#2
Oh dear god...

Remove the goto for the love of god. Use Thread.Sleep();
08/11/2013 03:46 Super Aids#3
My reaction @ your code:
[Only registered and activated users can see links. Click Here To Register...]
08/11/2013 05:18 Smaehtin#4
Quote:
Originally Posted by Korvacs View Post
Oh dear god...

Remove the goto for the love of god. Use Thread.Sleep();
I really don't think using Thread.Sleep inside a packet handler routine is a good idea
08/11/2013 10:50 Korvacs#5
Quote:
Originally Posted by Smaehtin View Post
I really don't think using Thread.Sleep inside a packet handler routine is a good idea
Well of course it isn't, but I don't really care about the effects that will have on his server, if he wants to pause the packet handler then let him, just so long as he's not using an infinite loop hacked into place using goto.
08/11/2013 11:34 _Emme_#6
Don't give advice if you know it's a extremely bad idea, that's just retarded.

@hacksaw
You have the right thinking, sort of. The easiest way for you to handle it is to make a new variable for this specific item ID/action. When Now => LoadTime, check if the itemid/action is not 0, if not perform the code (in this case Features.lock3.Open(Client) ).

Don't use an infinite loop, or pausing the entire Thread, but rather a timethread that works asynchronicity.
08/11/2013 13:58 Super Aids#7
Just use something like a delayed action.
08/11/2013 14:29 _Emme_#8
Considering his first version of the code is fairly poor, I don't want to confuse OP too much. But if you're looking for as good functionality as possible, Reactive Extensions ( [Only registered and activated users can see links. Click Here To Register...] ) should do the trick nicely.

Here's an example:

Code:
case 4000058:
{
      IDisposable GLSubcription = Observable.Interval(TimeSpan.FromSeconds(3)).Subscribe(1 => Game.Features.lock3.Open(client));
      GLSubcription.Dispose();
}
08/11/2013 14:45 hacksaw#9
Thanks Emil, I was just testing out your first way of doing it and it works a lot better that way, I shall have a read on the reactive extensions page in a few.

Nvm i see now ive read up on it, Although i read that using IDisposable & Observable together causes problems, Have you had any issues with them?