C# Threading in console.

Shezzar

Üye
20 Ocak 2019
89
3
C#
What is a Thread?
Let me explain the topic myself, else you are getting colder from the software while trying to understand the definition in other sources on the internet; It allows us to do the processions in parallel in the program. So let we have 2 jobs. It allows us to do both at the same time. Of course it's not limited to 2.

First things to be used;
Kod:
using System.Threading;
You need to add its reference above.

Let's write our threadless program.
Step one;
Kod:
for (int i = 1; i <= 10; i++)
Console.Write("Turk");

Step two;
Kod:
for (int i = 1; i <= 10; i++)
Console.Write("Hack");

Step three;
Kod:
for (int i = 1; i <= 10; i++)
Console.Write("Team");
This is the screen we will see after we do:
Kod:
[COLOR="yellow"]TurkTurkTurkTurkTurkTurkTurkTurkTurkTurk[/COLOR][COLOR="red"]HackHackHackHackHackHackHackHackHackHack[/COLOR][COLOR="White"]TeamTeamTeamTeamTeamTeamTeamTeamTeamTeam[/COLOR]

As you can see, it does the process sequentially. Let's do it by using threads and see the difference.

Kod:
[COLOR="Red"]HackHackHackHackHackHackHackHackHackHack[/COLOR][COLOR="Yellow"]Turk[/COLOR][COLOR="White"]TeamTeamTeamTeamTeamTeamTeamTeamTeamTeam[/COLOR][COLOR="yellow"]TurkTurkTurkTurkTurkTurkTurkTurkTurk[/COLOR]
It was doing the procession in a row before but now it is doing it in parallel. On the printout, Turk was written once before team, and rewritten when team finished. It did all the processions in a mixed way at the same time. If you examine the sequence in detail, you can fully understand the difference.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Well, you just showed the output of this created by the Thread difference. Where are these codes you wrote? Don't be confused. We can now move on to the coding part.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------

How to use thread?

[CONSOLE]
Let me show you first in the console application. Although it is quite simple, there will always be confusion. I will try to explain it in a simple and understandable way.

Kod:
static v o i d Main(string[] args)
        {
            [COLOR="MediumTurquoise"]Thread [/COLOR][COLOR="Yellow"]thd [/COLOR]= [COLOR="PaleGreen"]new [/COLOR][COLOR="mediumturquoise"]Thread[/COLOR]([COLOR="palegreen"]new [/COLOR][COLOR="mediumturquoise"]ThreadStart[/COLOR]([COLOR="green"]sinif[/COLOR]));
            [COLOR="mediumturquoise"]Thread [/COLOR][COLOR="yellow"]thd1 [/COLOR]= [COLOR="palegreen"]new [/COLOR][COLOR="mediumturquoise"]Thread[/COLOR]([COLOR="palegreen"]new [/COLOR][COLOR="mediumturquoise"]ThreadStart[/COLOR]([COLOR="green"]sinif1[/COLOR]));

            [COLOR="yellow"]thd[/COLOR].Start(); [COLOR="SeaGreen"]// thd Thread'ini başlattık.[/COLOR]
            [COLOR="yellow"]thd1[/COLOR].Start(); [COLOR="SeaGreen"]// thd1 Thread'ini başlattık.[/COLOR]
        }

        static v o i d [COLOR="green"]sinif[/COLOR]()
        {
             [COLOR="SeaGreen"]// Burada thd Thread'ini çalıştıracağız.[/COLOR]
        }
        static v o i d [COLOR="green"]sinif1[/COLOR]()
        {
            [COLOR="SeaGreen"]// Burada ise thd1 Thread'ini çalıştıracağız.[/COLOR]
        }
I made the explanation in the code. I've done coloring so you can understand what depends on what.

[FormApp]
Now let's see how we do it in FormApplication. The logic is the same as always. The only difference is that we connect it to the button event in FormApplication. Let me show it in this way;

Kod:
        private v o i d button1_Click(object sender, EventArgs e)
        {
            Thread [COLOR="yellow"]thd1 [/COLOR]= new Thread(new ThreadStart([COLOR="Green"]fonksiyon1[/COLOR]));
            [COLOR="Yellow"]thd1[/COLOR].Start();
        }
            [COLOR="seagreen"]// button1_Click Event'inde thd1 adından bir Thread tanımladık ve başlattık.[/COLOR]

        private v o i d [COLOR="green"]fonksiyon1[/COLOR]()
        {
            [COLOR="SeaGreen"]// button1_Click Event'inde yapılacak işlemler buraya yazılacak.[/COLOR]
        }

The logic is the same as you can see. The only difference is that we use it in Event.

Setting priority on multiple thread
Thread Priority is used if it is necessary to set a priority in multiple threads.

Kod:
static v o i d Main(string[] args)
        {
            [COLOR="MediumTurquoise"]Thread [/COLOR][COLOR="Yellow"]thd [/COLOR]= [COLOR="PaleGreen"]new [/COLOR][COLOR="mediumturquoise"]Thread[/COLOR]([COLOR="palegreen"]new [/COLOR][COLOR="mediumturquoise"]ThreadStart[/COLOR]([COLOR="green"]sinif[/COLOR]));
            [COLOR="mediumturquoise"]Thread [/COLOR][COLOR="yellow"]thd1 [/COLOR]= [COLOR="palegreen"]new [/COLOR][COLOR="mediumturquoise"]Thread[/COLOR]([COLOR="palegreen"]new [/COLOR][COLOR="mediumturquoise"]ThreadStart[/COLOR]([COLOR="green"]sinif1[/COLOR]));

            [COLOR="Yellow"]thd2[/COLOR].Priority = ThreadPriority.Lowest; [COLOR="SeaGreen"]// Daha yavaş.[/COLOR]
            [COLOR="yellow"]thd1[/COLOR].Priority = ThreadPriority.Highest; [COLOR="seagreen"]// Daha hızlı (öncelikli).[/COLOR]

            [COLOR="yellow"]thd[/COLOR].Start(); [COLOR="SeaGreen"]// thd Thread'ini başlattık.[/COLOR]
            [COLOR="yellow"]thd1[/COLOR].Start(); [COLOR="SeaGreen"]// thd1 Thread'ini başlattık.[/COLOR]
        }

        static v o i d [COLOR="green"]sinif[/COLOR]()
        {
             [COLOR="SeaGreen"]// Burada thd Thread'ini çalıştıracağız.[/COLOR]
        }
        static v o i d [COLOR="green"]sinif1[/COLOR]()
        {
            [COLOR="SeaGreen"]// Burada ise thd1 Thread'ini çalıştıracağız.[/COLOR]
        }




Thread is an important thing in C#.

The topic is completely mine. Do not take/steal it without permission.





Translator: Shezzar
Source: https://www.turkhackteam.org/c-j-vb...-console-formapp-turkce-lost.html#post5649230
 
Son düzenleme:
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.