Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Threading: Exercises for beginners

So far I have been trying to master threading by immediately implementing threads in my project. And I have been trying to do that for a long time. But this hasn't resulted any results, nor gave me any experience with threading. The only thing that the attempting gave me is the impression that threading in C# has many important refinements.

I couldn't find any simple exercises about threading. I'm searching for exercises where you have to make different simple console applications. I am searching for simple exercises so I can get an idea of how things work when working with threads and master that idea. I have seen a book of programming exercises with difficulty that gets harder as the problem's number gets bigger. I am searching for something similar. Afterwards I will continue with more complicated stuff and try to add threads in my project (which is made with Windows Forms).

Where can I find exercises/book of exercises about threading in C#?

EDIT:

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

like image 346
AlexSavAlexandrov Avatar asked Dec 14 '12 22:12

AlexSavAlexandrov


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

Simple exercises:

1) change code that works (learn by example)

  • open any tutorial
  • find code samples
  • play with code samples, see what works and what does not

2) answer questions on SO (learn by teaching)

  • find an interesting question
  • answer it (you should do a research to do this)
  • talk and interact with other users
like image 70
Miguel Angelo Avatar answered Sep 22 '22 13:09

Miguel Angelo


Joseph Albahari has a great article called Threading in C#. This is really cool blog post about start to learn for threading in C#. Joseph clearly explained:

  • Introduction and Concepts
  • Join and Sleep
  • How Threading Works
  • Creating and Starting Threads
  • Thread Pooling

And check this out article from Codeproject.

  • Getting Started

You can create and start a new thread by instantiating a Thread object and calling its Start method. The simplest constructor for Thread takes a ThreadStart delegate: a parameterless method indicating 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 threading deeply get a copy of C# 4.0 in a Nutshell

like image 32
Soner Gönül Avatar answered Sep 22 '22 13:09

Soner Gönül