C# Multi-Threading Tutorial

  Рет қаралды 162,797

Brandon Hilde

Brandon Hilde

Күн бұрын

Пікірлер: 87
@73h73373r357
@73h73373r357 9 жыл бұрын
A thread is defined as the smallest sequence of instructions that can be managed independently by a scheduler. In other words, a thread is basically, the smallest block of a code of one single program that can be safely executed, then exited, then restarted without corrupting the program. In c# this would be equivalent to a method. Threading is simply the act of timesharing (either with multiple cores, or time sliced within an individual core) multiple threads, which can either be from the same process if functions can be completed in parallel, or from multiple different processes.
@BrandonHilde129
@BrandonHilde129 8 жыл бұрын
Thanks guys for all the support on this video. I have been meaning to make another tutorial about sounds and creating uncompressed .wav files. However I have been busy. So Hopefully in the next two-ish weeks.
@MauryaSandeep
@MauryaSandeep 7 жыл бұрын
Awesome. It worked perfectly without any cross thread issues.....
@pebito
@pebito 8 жыл бұрын
This tut is easy to understand, however you should not use variable names like "th" and "th1" not even when demonstrating. Giving "thread" as method name is a bad practice too. Not to mention in C# method names start with uppercase letters. Please follow the naming convention at all times!
@spanout
@spanout 8 жыл бұрын
Great and simple demonstration that makes perfect sense!
@kentbrook2568
@kentbrook2568 6 жыл бұрын
Nice demo, easy to understand even without a working sound card. Ignore these silly nit pickers that troll your work and do no work themselves... great stuff, please keep it up!
@franciscoaraujo663
@franciscoaraujo663 8 жыл бұрын
Wow! The best example i'm found! Thanks Brandon!
@BrandonHilde129
@BrandonHilde129 8 жыл бұрын
+Francisco Araújo thanks!
@mandarinnu
@mandarinnu 3 жыл бұрын
Thank you. A small suggestion: You could do some refactoring, i.e., refactor the thread() and threadb() methods.
@garrettcolas
@garrettcolas 11 жыл бұрын
Keep in mind things like hyper-threading simulate multiple cores. So an i3 processor only has 2 physical cores, but can hyper-thread on each to effectively have 4 cores.
@franciscoaraujo663
@franciscoaraujo663 8 жыл бұрын
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; namespace APP1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Thread th1; Thread th2; Random rdm; private void Form1_Load(object sender, EventArgs e) { this.WindowState = FormWindowState.Maximized; rdm = new Random(); } private void button1_Click(object sender, EventArgs e) { // var x = MessageBox.Show("Vermelho", "Mensagem"); th1 = new Thread(thread1); th1.Start(); } private void button2_Click(object sender, EventArgs e) { // var x = MessageBox.Show("Azul", "Mensagem"); th2 = new Thread(thread2); th2.Start(); } public void thread1() { for (int i = 0; 1 < 100; i++) { this.CreateGraphics().DrawRectangle(new Pen(Brushes.Red, 4), new Rectangle(rdm.Next(0, this.Width), rdm.Next(0, this.Height), 20, 20)); Thread.Sleep(100); } } public void thread2() { for (int i = 0; 1 < 100; i++) { this.CreateGraphics().DrawRectangle(new Pen(Brushes.Blue, 4), new Rectangle(rdm.Next(0, this.Width), rdm.Next(0, this.Height), 20, 20)); Thread.Sleep(100); } } private void Form1_Closed(object sender, EventArgs e) { th1.Abort("Fim th1"); th2.Abort("Fim th2"); Environment.Exit(0); } } }
@benjaminchiozalde3414
@benjaminchiozalde3414 7 жыл бұрын
no mames te rifaste
@marcohundINV
@marcohundINV 6 жыл бұрын
you misstyped the for loop .. u typed for (int i = 0; 1 < 100; i++) instead of for (int i = 0; i < 100; i++)
@Ak0tnik
@Ak0tnik 9 жыл бұрын
Thank you. This helped me a lot with a programming project (need to simulate how a dam gets its water drained on real time)
@carlosbvz
@carlosbvz 10 жыл бұрын
I agree with other commets. Simple and complete. Thanks
@HawaiianDan123
@HawaiianDan123 11 жыл бұрын
Very nicely done! Just what I needed.
@killaurnext
@killaurnext 11 жыл бұрын
nicely done....not too complex and gets the point across
@ogdencitizensclub
@ogdencitizensclub 10 жыл бұрын
Thank you very much, the explanation is both simple and complete, it helped a lot.
@jgmangala
@jgmangala 8 жыл бұрын
thanks..... with the the things go wrong... it really helps to understand core concept.
@xarzu
@xarzu 8 жыл бұрын
Good job. Easy to understand. And Kudos for debugging on the fly like that.
@thornphillips1565
@thornphillips1565 7 жыл бұрын
Hey Brandon thanks for the tutorial. I am very new at programming and I was wondering, as I noticed you created a new thread method for each thread you were running, I couldn't help but to wonder if you could run multiple threads out of one method by maybe creating a new reference to it on each object. Seems like it would make for cleaner code.
@RejhanKuch
@RejhanKuch 7 жыл бұрын
Nice write, thank you for sharing your video, it helped me a lot for some small project about threading I had.
@razshneider2375
@razshneider2375 5 жыл бұрын
i use a canvas for my drawing because of canvas.height but when i do canvas.refresh() to delete the drawings on a different thread it gives me an error System.InvalidOperationException: 'Cross-thread operation not valid: Control 'canvas' accessed from a thread other than the thread it was created on.' so should i stop using the canvas because you just did it on the form with the this.creategraphics but how do i refresh the form then, or get the height and width?
@WoundedEgo
@WoundedEgo 10 жыл бұрын
Great job on this tutorial buddy, thanks.
@sergiopalazzi6033
@sergiopalazzi6033 5 жыл бұрын
Great tutorial. Good explanations! Thanks a lot.
@chrisjohnson7255
@chrisjohnson7255 6 жыл бұрын
Cool stuff I'll probably need this soon.
@bedennawulezi6448
@bedennawulezi6448 7 жыл бұрын
Hello Brandon, Thank you for this tutoiral! I´m trying to read a sequence of values in array to my Arduino. I´m really not so good with programming. I problem is indeed how by just hitting a button, the arduino could read the values via serial port at a given interval. i.e reading the first row of data from dataTable. After making use of the values (eg. runs motors to position) then it waits for a particular period of time and read again the the 2nd row from the datatable. etc. Do u think u could help? I would be so grateful. Thank You! Bede
@jebclang9403
@jebclang9403 4 жыл бұрын
This exact example will only work with 2 CPU cores or more right? Otherwise, with 1 core, the 2 threads will only switch back and forth to complete their task in the same time it would take without multi threading.
@khuyenbuivan7658
@khuyenbuivan7658 8 жыл бұрын
Thank you. Great video
@donmohino4414
@donmohino4414 7 жыл бұрын
Thank you for this great tutorial. I am making a serial port data reader and i am having a problem: if i use the serialport.readline method in a thread(like you did in the video), an error shows up. This is what the error says: "Invalid inter-thread operation: The 'textBox1' control was accessed from a thread other than the one on which it was created". Any ideas the fix this?
@zillabunny
@zillabunny 10 жыл бұрын
very nice!
@SaurabhSashank
@SaurabhSashank 11 жыл бұрын
This is great stuff dear......
@StansPreviouslyOwnedSoundtrack
@StansPreviouslyOwnedSoundtrack 8 жыл бұрын
Brilliant, thank you!!
@germandiez2974
@germandiez2974 7 жыл бұрын
i'm not making a videogame, but this really help me tahnks man!
@aleksandar5323
@aleksandar5323 10 жыл бұрын
So if I want to set a parameter by the Return of a thread , but try to use this parameter before the thread is finished will it wait until it's done or crash? Only one way to find out :D
@paulcox9728
@paulcox9728 10 жыл бұрын
good job with this, thanks, subscribed and liked
@standinonstilts
@standinonstilts 10 жыл бұрын
Great video!
@vincenguyen1356
@vincenguyen1356 9 жыл бұрын
I learn best with trial and error. Good tutorial Thanks
@marcusrehn
@marcusrehn 11 жыл бұрын
Threading like this is not hard, but I can never find a good video that includes the sharing of resources between different threads. Thats where it becomes harder.
@leonardshin4469
@leonardshin4469 8 жыл бұрын
Thank you for explaining beautifully what no one else including my AP comp Sci teacher couldn't explain. However, there is this weird caveat when i press the blue button three or more times. There occurs this error: :::::::An unhandled exception of type 'System.InvalidOperationException' occurred in System.Drawing.dll :::::::Additional information: Object is currently in use elsewhere. I was wondering if there was a way to prevent this from happening or at least why it''s throwing this error. Thank you and it'd be wonderful if you replied (Hint hint :) )
@maxratner6141
@maxratner6141 8 жыл бұрын
multiple things are calling the same DLL at the same time (in the separate threads), could be causing the conflict. Probably want to be checking in your code for these collisions. It's like trying to fit 5 cars into a 2 lane tunnel, I suppose?
@ivyliu6182
@ivyliu6182 10 жыл бұрын
Very useful!
@BrandonHilde129
@BrandonHilde129 11 жыл бұрын
That is true. however I try to keep my explanations simple. but thanks for the clarity.
@kernelle4
@kernelle4 11 жыл бұрын
thank you, you have earned a new sub
@johnabraham9203
@johnabraham9203 9 жыл бұрын
application should open with a splash screen that displays a disabled list of days of week for 10secs and proceed to next form
@chidhvilaskommineni1466
@chidhvilaskommineni1466 9 жыл бұрын
John Abraham done
@sandradebrauwer2401
@sandradebrauwer2401 4 жыл бұрын
I tried to run the graphics only in the main loading form but it didnot work, I used no button, perhaps I have to make a button
@chidhvilaskommineni1466
@chidhvilaskommineni1466 9 жыл бұрын
good one
@indrajaya9157
@indrajaya9157 8 жыл бұрын
ON YOUR CODE AND MY CODE WEWEWW WELL WELL WELL
@FLneodesign
@FLneodesign 9 жыл бұрын
Great Video thanks :)
@DanieboyTV
@DanieboyTV 9 жыл бұрын
Simply following the tutorial gave me an error: An unhandled exception of type 'System.NullReferenceException' occurred in Draw thread boxes.exe
@SB-hs4yn
@SB-hs4yn 9 жыл бұрын
+DanieboyTV Check your code for grammatical errors and such.
@madvvasp
@madvvasp 8 жыл бұрын
Good tutorial, thank you! PS. Try to use GitHub to share the code (do not confuse with Pornhub :) ) Good luck!
@BrandonHilde129
@BrandonHilde129 8 жыл бұрын
+Юрий Руд thanks dude but I think people learn better when they don't copy and paste code. So I prefer that people follow along.
@MegaMeno
@MegaMeno 6 жыл бұрын
Brandon Hilde no, it's actually the other way around. That's why teachers give full examples so the learners can learn better, when the questions come the will know how to solve them because they know their logic.
@korioss
@korioss 12 жыл бұрын
Ty for the video
@JOHNDATGOONV2
@JOHNDATGOONV2 11 жыл бұрын
Thanks so much
@garrettcolas
@garrettcolas 11 жыл бұрын
Come on man, you can't just say programming for the console is worthless. It's very important for business/enterprise applications.
@Kay-hx8xb
@Kay-hx8xb 8 жыл бұрын
If I have a function contains parameters like Method(a, b), so how can I initailize the instance of new thread?
@BrandonHilde129
@BrandonHilde129 8 жыл бұрын
You can use a Parameterized ThreadStart to do that. Also you can use static variables that are created out side of the that thread. Hope this helps!
@Kay-hx8xb
@Kay-hx8xb 8 жыл бұрын
Ok, I created, initialized and ran it perfectly. And I think the way you recommended will work. Thank you.
@BrandonHilde129
@BrandonHilde129 8 жыл бұрын
Awesome! glad it worked :)
@4kills482
@4kills482 7 жыл бұрын
Nice video but you kept repeating that multi threads will make your programm faster. This is only partially true. If you run the application on a single core cpu the cpu will process one thread at a time, then halt that first thread to execute the second one etc.. It will not process simultaneiously. Unless you are using multi core cpus threading won't lead to a performance increase. Programmers should use threading as often as possible where possible, in order to make use of multi core processors nowadays. A lot of programms are badly written by not distributing workload across multiple processing cores.
@ziajunaidi2023
@ziajunaidi2023 11 жыл бұрын
cool
@zahidtuhin1407
@zahidtuhin1407 10 жыл бұрын
nice
@fntasticHijack
@fntasticHijack 11 жыл бұрын
Ive tried to test my game, but when the computer thinks for a solution, my game was frozen. Can multi threads solve the problem? or you have another solution for that?
@joacimjohnsson
@joacimjohnsson 11 жыл бұрын
Hi, Great... I would be interested in a tutorial on how to move the boxes with the mouse. You dont have to go on from this tutorial, you could start simple with moving a box tipsNtricks... thanks Jo
@zzvila
@zzvila 11 жыл бұрын
How can I do this using Process
@sazzzz878373478
@sazzzz878373478 9 жыл бұрын
Can you share this source of this tutorial ?
@khalilburno9713
@khalilburno9713 8 жыл бұрын
NICE smart and short tip , thank you ,but the sound is too low i cant here you almost thank you thumbs up ;-)
@TheLosgat
@TheLosgat 9 жыл бұрын
Tx man..
@Iconejey
@Iconejey 5 жыл бұрын
very useful thank you! +1 like for me
@djdannyback3455
@djdannyback3455 9 жыл бұрын
Você poderia me passar o código?
@lior19977
@lior19977 12 жыл бұрын
thanks for the vid. can you to make me a tutorial about making sound effects for my games. thanks
@josemena649
@josemena649 8 жыл бұрын
can you do pack man game in c# too ?? im a beginer and i lie to learn how to do it PLS just a symple code pls
@BrandonHilde129
@BrandonHilde129 8 жыл бұрын
maybe. not sure right now.
@BrandonHilde129
@BrandonHilde129 11 жыл бұрын
I just put up a video about playing sound a few days ago... you should check it out
@winterfrog5521
@winterfrog5521 10 жыл бұрын
please speak loud
C# Graphics - Draw a Hexagon Map with Graphics and DrawPolygon
7:00
C# multithreading 🧶
6:59
Bro Code
Рет қаралды 159 М.
黑的奸计得逞 #古风
00:24
Black and white double fury
Рет қаралды 26 МЛН
Fake watermelon by Secret Vlog
00:16
Secret Vlog
Рет қаралды 27 МЛН
c# (Csharp) threading interview question:- What is thread,background thread and foreground thread ?
12:16
Advanced C#: 15 Async & Await
18:45
Clint Eastwood
Рет қаралды 128 М.
Multithreading in C# | C# Interview Questions |  C# | Tamil
14:13
Tamil Coding
Рет қаралды 4,8 М.
C# Socket Programming - Multiple Clients
29:08
Brian (Able Opus)
Рет қаралды 361 М.
КАК УСТРОЕН TCP/IP?
31:32
Alek OS
Рет қаралды 186 М.
Emulating a CPU in C++ (6502)
52:28
Dave Poo
Рет қаралды 994 М.
Object Oriented Programming is not what you think it is. This is why.
13:36
黑的奸计得逞 #古风
00:24
Black and white double fury
Рет қаралды 26 МЛН