Post: C# Make your application run faster with threading [SIMPLE]
10-24-2015, 02:30 AM #1
Sloth
Banned
(adsbygoogle = window.adsbygoogle || []).push({}); I was helping some people out on their projects and I noticed they where running multiple methods or functions or running heavy tasks all on the same thread as the form.

So I decided to write this quick simple thread on using threading.


Now I don't really know too much about threading so it's probably just gonna look like a skiddys work to other programmers but it's better than running everything on 1 thread.



So lets say below is our method that will show a message box with the sum of 64 and i
    [/COLOR][/B][B][COLOR=#ff0000]private void DoAddition(int i) { MessageBox.Show(64 + i); }[/COLOR][/B][B][COLOR=#FF0000]



This method would be called like this
    [/COLOR][/B][B][COLOR=#ff0000]DoAddition(24);[/COLOR][/B][B][COLOR=#FF0000]


[Any Number between parenthese]


Now lets pretend it took 20 seconds for this to complete now during that time or form is going to be frozen.
This is where threading comes in, we can use it to run that method on a thread separate from the one running the form

So instead of doing

    [COLOR=#FF0000]DoAddition(24);[/COLOR]


we would Firstly add this reference
    [/COLOR][/B][B][COLOR=#FF0000]using System.Threading;[/COLOR][/B][B][COLOR=#FF0000]


Then add this in place of the general call
    [/COLOR][/B][B][COLOR=#FF0000]Thread Add = new Thread(() => DoAddition(24));[/COLOR][/B][B][COLOR=#FF0000]


This will cause the thread to stop if the application is closed, because otherwise it would still run.
    [/COLOR][/B][B][COLOR=#FF0000]Add.IsBackground = true;[/COLOR][/B][B][COLOR=#FF0000]


Finally we'll start the Thread
    [/COLOR][/B][B][COLOR=#FF0000]Add.Start();[/COLOR][/B][B][COLOR=#ff0000]



Now it will run on a seperate thread so it should run faster and will not cause your form to freeze which means you can do other things.
Threading will also allow you to run lots of different things at the same time.

If for some reason you need to stop the thread you can call this
    [/COLOR][/B][B][COLOR=#ff0000]Add.Abort();[/COLOR][/B][B][COLOR=#FF0000]


This is not recommended as it can cause issues and is not good coding practice.





If something in this thread confused you, I got something wrong, or you have some criticism please quote me so I can see.

Good Luck!
Last edited by Sloth ; 10-24-2015 at 02:33 AM.

The following 6 users say thank you to Sloth for this useful post:

Algebra, Boliberrys, Devious, Helping-Hand, jagex, Hash847
10-24-2015, 11:14 AM #2
Hash847
Purple God
Originally posted by Sloth View Post
I was helping some people out on their projects and I noticed they where running multiple methods or functions or running heavy tasks all on the same thread as the form.

So I decided to write this quick simple thread on using threading.


Now I don't really know too much about threading so it's probably just gonna look like a skiddys work to other programmers but it's better than running everything on 1 thread.



So lets say below is our method that will show a message box with the sum of 64 and i
    [/COLOR][/B][B][COLOR=#ff0000]private void DoAddition(int i) { MessageBox.Show(64 + i); }[/COLOR][/B][B][COLOR=#FF0000]



This method would be called like this
    [/COLOR][/B][B][COLOR=#ff0000]DoAddition(24);[/COLOR][/B][B][COLOR=#FF0000]


[Any Number between parenthese]


Now lets pretend it took 20 seconds for this to complete now during that time or form is going to be frozen.
This is where threading comes in, we can use it to run that method on a thread separate from the one running the form

So instead of doing

    [COLOR=#FF0000]DoAddition(24);[/COLOR]


we would Firstly add this reference
    [/COLOR][/B][B][COLOR=#FF0000]using System.Threading;[/COLOR][/B][B][COLOR=#FF0000]


Then add this in place of the general call
    [/COLOR][/B][B][COLOR=#FF0000]Thread Add = new Thread(() => DoAddition(24));[/COLOR][/B][B][COLOR=#FF0000]


This will cause the thread to stop if the application is closed, because otherwise it would still run.
    [/COLOR][/B][B][COLOR=#FF0000]Add.IsBackground = true;[/COLOR][/B][B][COLOR=#FF0000]


Finally we'll start the Thread
    [/COLOR][/B][B][COLOR=#FF0000]Add.Start();[/COLOR][/B][B][COLOR=#ff0000]



Now it will run on a seperate thread so it should run faster and will not cause your form to freeze which means you can do other things.
Threading will also allow you to run lots of different things at the same time.

If for some reason you need to stop the thread you can call this
    [/COLOR][/B][B][COLOR=#ff0000]Add.Abort();[/COLOR][/B][B][COLOR=#FF0000]


This is not recommended as it can cause issues and is not good coding practice.





If something in this thread confused you, I got something wrong, or you have some criticism please quote me so I can see.

Good Luck!


Just a quick little tip, you can do the following;

    
new Thread(_ => {
Console.WriteLine("This is called from another thread!");
}).Start(); //NOTE: Start() is a void so if you try to do "var thread = new Thread(_ => { }).Start();" it wont work, unless you make an extension method


As an extra note, you can qualify the thread with "_" so you can do "_.Abort()" but it works like a using so you'll have to add it to a list or something if you want to qualify it outside of that, or you'll need to add another line like so

     
var thread = new Thread(_ => {
Console.WriteLine("This is called from another thread!");
});
thread.Start();
10-24-2015, 11:30 AM #3
Hash847
Purple God
Also, you should do a tutorial on thread pooling. It's an extremely useful thing. I was going to use it in my twitch account checker to fix the thread creation limit but I couldn't be bothered lol - if you don't want to do it, I can.
10-24-2015, 04:45 PM #4
jagex
Gym leader
Have you tried out Task? I find it a lot better than threading for most things.
10-24-2015, 07:07 PM #5
Default Avatar
Oneup
Guest
Well it doesn't really make the code run any faster. It just lets you do more things in parallel.

Something I didn't notice you mention was cross threaded exceptions.

I made a thread last year that could build onto this:
You must login or register to view this content.

Copyright © 2025, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo