Help With Functions

Posts 1–11 of 11 · Page 1 of 1
Help With Functions
Hey guys, I've been making this code, what it basically does is that there is this function that will loop itself over and over again with goto. I need to be able to end that function at any time if i press a hotkey. I was wondering is there a way to do this?
I would recommend replacing your goto loop with a for loop. It would go a little something like this:

[php]
for(;
{
if(GetAynscKeyState(hotkey))
{
break;
}
//otherwise do stuff
}
[/php]

The 'break' keyword is actually quite useful, check it out here.
break (C#)
I'm aware of the break word, although I'm planning to use return to exit the function completey, a timer with getasynckeystate is activating the function once again. But I need it to break whenever. The function does a couple of things and is time consuming, it doesn't execute within a tight loop.

I was thinking like maybe putting the code into a seperate thread (backgroundworker) and then stopping that backgroundworker... but cross threading is annoying and very troublesome... Is there a way just to stop a function? like Function().Stop?
I don't get what you're trying to do.. You need it to break whenever? Huh? Why would you ever need to stop a function half way through, if you want it to stop completely just return like you said.
But i would need to do such code like:
Code:
if (break)
{
return;
}
At around every section...

because my function takes lets say.... 10 minuets per loop and it returns to a previous location if an error occurs to re-do the action.I need it to end any time within that 10 minuets. So if I only did it in one place, u'll have to wait 10 min for it to exit.
Either check for some flag periodically (or in multiple spots in your code - of course in the above sample you'd simply do : while(!stopFlag) or something similar - or your second option indeed is to run it as a thread and stop it when required, and restart based on some other event and just deal with the cross threading issues (not that hard. just check for invokerequired if you're updating UI components)
Quote Originally Posted by User1 View Post
I need it to end any time within that 10 minuets. So if I only did it in one place, u'll have to wait 10 min for it to exit.
Then ur not doing it right... why set ur function to work every ten minutes if u need it to be responsive all the time... seems to me the way ur organizing ur code is just flat out wrong. Post it and some1 can help you out. =/
Code:
while(1)
{
    while(1)
    {
          if(getasynckeystate(vkdjdh)) break;
    }
    while(1)
    {
          //'leet code goes here'
          if(getasynckeystate(vkdjdh)) break;
    }
}
The function finishes every 10 minuets. It is an AFK bot and therefore it needs to loop "stay alive" code till the game ends...
From what I understood, threading would be effective here. Of course, I might have misunderstood.
I already solved my solution with a killable backgroundworker.
Posts 1–11 of 11 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Talk with us