C# Code Snippets TPL(Task Parallel Library) and Tasks

C# TPL(Task Parallel Library) And Tasks

In this code snippet, we will take a look at the TPL(Task Parallel Library) in C#. If you read some of my other posts on threading you will know that it can be quite complicated and laborious for the developers to implement parallelism into their programs(deadlocks, cancellation, thread pooling, getting data back from the thread, …). The Task Parallel Library(TPL) takes care of a lot of these things and or makes them easier manage.

C# Code Snippets Thread Pooling

C# Thread Pooling

In this tutorial, we will take a look at thread pools in .NET and C#. Creating threads and disposing of them when a task is done can be an expensive operation to perform as it uses up time and memory. To mitigate this we can use a thread pool which is essentially a collection of pre instantiated threads. We can assign tasks to these threads. When the task is completed the thread gets returned to the thread pool instead of being disposed of.

C# Code Snippets Thread Join IsAlive Abort

C# Thread Join, IsAlive, Abort

In this tutorial, we will take a look at thread join, isAlive and abort in C#.

join() The main(parent) thread waits for the joined threads(threads that had the join() method called on them) to complete their execution before proceeding with its execution.

abort() Can be used to abort the thread execution. For example, if a thread takes to long to finish we can abort its execution. When this method is called on a thread it will throw a ThreadAbortException that will stop the thread. However, it’s not guaranteed that a thread will stop in all situations.

isAlive() Checks if the thread is running and returns true or false.

C# Code Snippets Thread deadlock

C# Solving a Thread Deadlock

In this tutorial, we will take a look at thread deadlocks in C# and how to resolve them. If you see this post I made about thread resource locking you will see why a thread would need to lock a resource to prevent other threads from using it. But when you start locking down resources you could potentially run into a deadlock.

C# Code Snippets Thread Synchronization with Monitor and Lock

C# Threads And Resource Locking

In this tutorial, we will take a look at thread synchronization with Monitor and Lock in C#. In this post, I showed how to start new threads to execute code in parallel. However, when you start using threads what can happen is that two threads will access the same resources at the same time. One thread might modify a variable before another thread is done executing. This can, of course, cause errors. To prevent this from happening we have to lock the resources in question and prevent other threads from using them until the current thread is done executing. We can do this by either using the Monitor class or the lock keyword. These two approaches are functionally identical. The only difference (as you will be able to see in the code below) is that lock is more compact and easier to use.

C# Code Snippets Event Arguments

C# Event Arguments

In this tutorial, we will take a look at event arguments in C#. Event arguments allow you to pass data(from publisher to subscriber) with the event when it is triggered. To return data with an event you must make a class that inherits from the EventArgs class. In this new derived class you can add the properties you want to send. Then you return this EventArgs child class with the event.

C# Code Snippets Thread Data Exchange by Object

C# Thread Data Exchange by Object

In this tutorial, we will see how to exchange thread data by using an object in C#. This post is very similar to this post I already made about sending/receiving data to/from a thread. The only difference is that here an object will be used for the data. To pass data into the thread we will create a class and make two properties one for the input data and one for the callback delegate. When an instance of the class is being created the input data and the callback delegate must be passed into the constructor. After the object has been created its doWork() method can be started as a new thread.

C# Code Snippets Sending and Receiving Data From a Thread

C# Sending and Receiving Data From a Thread

In this tutorial, we will see how to pass/get data to/from a thread in C#. To pass data into the thread we will simply pass it as an input parameter to the method that is being started as a new thread. To get the data from the thread a delegate callback method will be used. First, we will make a regular method that takes in an input parameter. Then we will make a delegate and make it point to this method. Finally, the delegate will be passed as an input parameter to the method that is being started as a new thread. When the child thread is done executing it will call the callback method that is located in the main thread and pass it the result as an input parameter. This is how we will be able to get the result from the child thread back into the main one.

Advertisment ad adsense adlogger