So, today we are going to be learning how to do away with slow, clunky, laggy timers and ramp things up with minimal to no impact to your project!!!
For those of you who don't know what timers are and how they are used, typically speaking you would use them to write a process or function constantly, or to monitor various things such as int's and so on.
One thing to use timers for is monitoring bytes via
int Check;
Check = PS3.Extension.ReadInt32(PS3.Extension.ReadByte(0x84934523));
This will display the information at 0x84934523, we can use a timer to constantly read this.
There are loads of other things you can do with timers, but I won't be getting into that here.
On with the tutorial!!!
Biggest difference between Timers and Background Workers is that Background Workers are essentially ALWAYS running (unless you programmicatically end them)
Timers can be either way, but the longer you have a timer running, the more chance of lag you have.
So, first and foremost, you are going to want to get a background worker added to your project, you can find it in your Toolbox.
Once you have added that, name it whatever you want. for this demonstration we will be naming it "worker".
Double click worker to go into your coding window; here is where we will start the magic!
We need to declare an bool somewhere outside of the background worker, you can do that almost anywhere simply type something like "bool i;" without quotations.
Then inside the bgworker, we will be referencing that bool like this;
while (i) //The function will run so long as Bool i returns a "true" value
{
Invoke(new MethodInvoker(delegate()
{
//Put your code here!!!
})); // Invoke allows the background worker to interact with objects within your Form.
}
We use a Bool because it can be toggled on or off. It's the same thing as using timer1.Start(); or timer1.Stop();
Now, make yourself either a button or a checkbox, we will be using a button for this example
Once you get your button placed, double click it and put this in there;
i = !i; //this toggles the bool each time the button is pressed
if (!worker.IsBusy) //this checks if the worker is already on or not.
worker.RunWorkerAsync();
That's it!! Enjoy your ridiculously fast functioning!
Here is a bit of a comparison for you all just to put things in perspective 🙂