C # Threading: Beginner Exercises

So far, I have tried to master threads, immediately applying threads to my project. And I tried to do this for a long time. But this did not lead to any results and did not give me experience with threading. The only thing I tried to give me was the impression that threading in C # has many important improvements.

I could not find simple streaming usage exercises. I am looking for exercises where you need to create different simple console applications. I am looking for simple exercises so that I can understand how everything works when working with threads and master this idea. With difficulty I saw a book of programming exercises, which becomes more complicated as the number of problems increases. I am looking for something like that. Subsequently, I will continue with more complex things and try to add threads to my project (which is created using Windows Forms).

Where can I find exercises / exercise book about streaming in C #?

EDIT:

I am NOT looking for any textbooks - I can find them myself. I am only looking for exercises and exercises. If there are no such exercises, please tell me.

+6
source share
3 answers

Simple exercises:

1) change the code that works (learn by example)

  • open any textbook
  • find code samples
  • play with code samples, see what works and what doesn't

2) answer questions about SO (learn by learning)

  • find an interesting question
  • answer (you have to do research to do this)
  • talk and interact with other users
+7
source

Joseph Albahari has a great article called Threading in C # . This is a really cool blog post on how to get started exploring threads in C #. Joseph clearly explained:

  • Introduction and Concepts
  • Registration and sleep mode
  • How Threading Works
  • Creating and starting threads
  • Thread pool

And check out this article from Codeproject.

  • Beginning of work

You can create and start a new thread by creating an instance of the Thread object and calling its Start method. The simplest constructor for Thread accepts a ThreadStart delegate: a parameterless method that indicates where execution should begin.

 using System; using System.Threading; class ThreadTest { static void Main() { Thread t = new Thread (WriteY); // Kick off a new thread t.Start(); // running WriteY() // Simultaneously, do something on the main thread. for (int i = 0; i < 1000; i++) Console.Write ("x"); } static void WriteY() { for (int i = 0; i < 1000; i++) Console.Write ("y"); } } // Output: xxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyy yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy yyyyyyyyyyyyyxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ... 

If you want to learn multithreading, get a copy of C # 4.0 in a nutshell

+4
source

Threading is pretty straight forward. Here is a popular link: http://www.albahari.com/threading/

The basis:

Create a method that returns void and takes no arguments. Like this:

 private void MyThreadMethod() { //Some code here. } 

To call this method in a stream, just do it using the button on the form.

 var thread = new Thread(new ThreadStart(MyThreadMethod)); thread.Start(); 

This will create a thread and run it and run the code in MyThreadMethod .

Now the devil is in the details. Thread safety and thread management is where the true art of threading and multitasking are. Another thing worth considering is Threads vs Tasks, which are much more convenient.

0
source

All Articles