Post: C# Better than a Timer tutorial
05-26-2015, 03:28 AM #1
Cain532
Vault dweller
(adsbygoogle = window.adsbygoogle || []).push({});

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.

You must login or register to view this content.

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;

You must login or register to view this content.


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 Smile

You must login or register to view this content.

The following user thanked Cain532 for this useful post:

BISOON

The following user groaned Cain532 for this awful post:

Pichu
05-26-2015, 01:39 PM #2
Default Avatar
Oneup
Guest
The only problem with this is that you are comparing 2 different controls that do not behave in the same way. A background worker acts as a separate thread where as a timer does not. You will end up with cross threading issues if they are not handled properly.

The following user thanked Oneup for this useful post:

Pichu
05-26-2015, 02:42 PM #3
jagex
Gym leader
If you need to interact with the UI you should be using ProgressChanged event.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo