![]() |
|
[.NET] [VB.NET] Multi-Threading [TUT] - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [.NET] [VB.NET] Multi-Threading [TUT] (/showthread.php?tid=1410154) Pages: Pages
1
2
|
[VB.NET] Multi-Threading [TUT] - KingCobraXD - Jun 23, 2011 What is a multi-threaded application? A multi-threaded application is an application that contains more than one thread. Why are multi-threaded applications better than single-threaded applications? A multi-threaded application has the ability to do many simultaneous things at once. Example of a single-threaded application: Code Sub Main()Example of a multi-threaded application: Code Sub Main()This code will create t1 before t2. However, MsgBox("2") no longer needs to wait for the user to close/press OK on MsgBox("1") before it will show. Both these messageboxes will show around the same time. In order to create a new thread, you would be required to something like this: Code Dim t As New Thread(AddressOf NameOfSub)Is that all of threading? No. Threading is a very useful tool but it is also a very confusing tool. Unfortunately, you are not allowed to edit GUI properties that aren't created in the same thread. Take a look at the code below: Code Sub Main()Two threads are created and both of them create new forms. However, t1 will not be allowed to edit Form2 and t2 will not allowed to edit Form1. NOTE: When an application runs, it creates its own main thread. The above code creates two threads but it actually has three threads running. How do I manage to edit forms that aren't created in the same thread? We use the invoke method along with delegates. Take a look at the code below: Code Dim Form1, Form2 As Form()NOTE: The Delegate and the Sub that is going to be executed MUST have the EXACT same parameters. After invoking the delegate, we need to specify which sub is doing the editing by using AddressOf Operator (Code rewritten below). Code Invoke(New ChangeTextDelegate(AddressOf ChangeText), Form2, "Form2TextHere")Last, but not least, directly after the specified sub would be the parameter you want to pass into the sub. NOTE: You can use MethodInvoker as a delegate without any parameters. Example Code: Code Dim Form1, Form2 As Form()I hope you have learned a thing or two about threading. =) Please leave a comment! It took me half an hour to write this out. It only takes you ten seconds to say THANKS. © KingCobraXD * - theonetrueace - Jun 23, 2011 Looks good so far. If you wanted to expand it you could talk about ThreadPools, the overhead associated with the creation and destruction of threads, ways of monitoring threads, etc. Just a suggestion, great thus far though. :) * - Marvincmarvin. - Jun 23, 2011 Code Sub Main()Would this code spam message boxes or am I not understanding ? * - KingCobraXD - Jun 23, 2011 (Jun 23, 2011 01:57 PM)Marvincmarvin Wrote: Yes. It would. But it would take a lot of memory becuase it's creating so many threads. Try this code: Code Sub Main()(Jun 23, 2011 01:57 PM)theonetrueace Wrote: Looks good so far. If you wanted to expand it you could talk about ThreadPools, the overhead associated with the creation and destruction of threads, ways of monitoring threads, etc. Just a suggestion, great thus far though. :) Thanks. I will do that some other time. lol. * - INFAМOUS - Jun 23, 2011 Good explanation on Multi-Threading. Good job. * - Marvincmarvin. - Jun 23, 2011 If your spamming message boxes you mind as well spam threads ;P ~Thanks for the tutorial btw * - shadow1992 - Jun 23, 2011 Why are you using "AddressOf" instead of "sub()"???? Wrong programming technique.... * - Manager - Jun 23, 2011 'How do I manage to edit forms that aren't created in the same thread?' its called crossThread. and its mean that thread want change thing in second thread. nice sample ... thread B want to change propertie in Thread A [main form -> some label] so i have to use. this.invoke Or base.invoke to run that operation in the main thread. so now A will change the label. * - KingCobraXD - Jun 23, 2011 (Jun 23, 2011 03:49 PM)shadow1992 Wrote: Why are you using "AddressOf" instead of "sub()"???? Do some research. It's AddressOf.... * - W33DY - Jun 23, 2011 lol,u posted this what i need on my bday wow :) thanks ! p.s i hope u release soon ShadowTech Rat * - KingCobraXD - Jun 23, 2011 (Jun 23, 2011 04:15 PM)W33DY Wrote: lol,u posted this what i need on my bday wow :) Lol. No prob. Happy BDay! * - RDCA - Jun 23, 2011 Thanks for this. Actually I have looked into multi-threading before, but it didn't make perfect sense at the time. But this does. Thank you a lot for this thread. * - Jash - Jun 23, 2011 Fck, 'been looking for this for days. * - christiano - Jun 23, 2011 Good Job mate, Appreciate it Thanks! * - Bigandrewgold - Jun 23, 2011 PHP Code |