THT DUYURU

 
 
Programming The area where various programming languages are shared.

Seçenekler

C# Threading in console.

Shezzar - ait Kullanıcı Resmi (Avatar)
Üye
Üyelik tarihi:
01/2019
Nereden:
Ankara
Mesajlar:
85
Konular:
24
Teşekkür (Etti):
93
Teşekkür (Aldı):
67
Ticaret:
(0) %
0
570
29-09-2020 18:20
#1
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:
TurkTurkTurkTurkTurkTurkTurkTurkTurkTurkHackHackHackHackHackHackHackHackHackHackTeamTeamTeamTeamTeamTeamTeamTeamTeamTeam
As you can see, it does the process sequentially. Let's do it by using threads and see the difference.

Kod:
HackHackHackHackHackHackHackHackHackHackTurkTeamTeamTeamTeamTeamTeamTeamTeamTeamTeamTurkTurkTurkTurkTurkTurkTurkTurkTurk
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)
        {
            Thread thd = new Thread(new ThreadStart(sinif));
            Thread thd1 = new Thread(new ThreadStart(sinif1));

            thd.Start(); // thd Thread'ini başlattık.
            thd1.Start(); // thd1 Thread'ini başlattık.
        }

        static v o i d sinif()
        {
             // Burada thd Thread'ini çalıştıracağız.
        }
        static v o i d sinif1()
        {
            // Burada ise thd1 Thread'ini çalıştıracağız.
        }
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 thd1 = new Thread(new ThreadStart(fonksiyon1));
            thd1.Start();
        }
            // button1_Click Event'inde thd1 adından bir Thread tanımladık ve başlattık.

        private v o i d fonksiyon1()
        {
            // button1_Click Event'inde yapılacak işlemler buraya yazılacak.
        }
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)
        {
            Thread thd = new Thread(new ThreadStart(sinif));
            Thread thd1 = new Thread(new ThreadStart(sinif1));

            thd2.Priority = ThreadPriority.Lowest; // Daha yavaş.
            thd1.Priority = ThreadPriority.Highest; // Daha hızlı (öncelikli).

            thd.Start(); // thd Thread'ini başlattık.
            thd1.Start(); // thd1 Thread'ini başlattık.
        }

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



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-...ml#post5649230
---------------------
International Forum Board Moderator - Feel free to PM me anything about forum!
Konu Shezzar tarafından (29-09-2020 18:22 Saat 18:22 ) değiştirilmiştir.

Bookmarks


« Önceki Konu | Sonraki Konu »
Seçenekler