unknowncheats uc-forum.com ucdownloads ucdownloads.com

Go Back   UC-Tutorials - Multiplayer Game Hacking and Cheat Tutorials > Programming > Visual Basic

- Sponsored Advertisement -
http://www.myfpscheats.com/


Reply
 
Thread Tools Display Modes
  #1  
Old 12-23-2006, 07:22 AM
zero_tolerance zero_tolerance is offline
Senior Member
 
Join Date: Dec 2006
Posts: 289
Default MultiThreading in VB2005

By Acco

This is probley my personal favourite new feature to Visual Basic over older versions, and thats MultiThreading.

First head over to the Microsoft website and download Visual Basic 2005 Express (yes its legal and free), you can find it here http://msdn.microsoft.com/vstudio/express/vb/download/

After you have that installed start up Visual Basic and start a new Console Application: File>>New Project>>Console Application

Once you have the new project open you will see the following code:

Code:
Module Module1

   Sub Main()
   
   End Sub

End Module
Next make a new public sub called 'Message' with your functions in it, in this example im going to write a line on the console using the Console.Write command.

Ok now your program should look as follows:
Code:
Module Module1

    Sub Main()

    End Sub

    Public Sub Message()
        Console.Write("Please visit www.unknowncheats.com")
    End Sub

End Module
Ok now we have that done make another public sub called 'FileDelete', in this you will use the IO.File.Delete() function, to use this you will also need to add the decleration 'Imports System.IO.File' to the top of the code.
Your code should now look as follows:
Code:
Imports System.IO.File
Module Module1

    Sub Main()

    End Sub

    Public Sub Message()
        Console.Write("Please visit www.unknowncheats.com")
    End Sub

    Public Sub FileDelete()
        IO.File.Delete("C:\Test.txt")
    End Sub

End Module
Aight now we have our subs setup, now without multithreading a normal program would execute each line of code in turn. The problem here is that if the program first execute a part of the code which say, counts the files on C:\ this may take a while; and until that is complete nothing else in the program can continue sort or like a traffic jam, the cars behind cant move (execute) until the other cars (lines of code) are moving (complete).

Next add the following code to your Sub Main:
Code:
Dim MessageThread As New System.Threading.Thread(AddressOf Message)
        MessageThread.Start()
Now lets break this code down a little, the first part we are defining a variable called 'MessageThread' as a new instance of threading. Next we have to set what address it will run, AddressOf is followed by the name of the sub you would like it to execute on a seperate thread, in this example we are giving the Public Sub 'Message' its own thread shown here by typing 'AddressOf Message' <<<Message = Public sub Message() for those who arent following lol

Next we start the seperate thread which will execute the code within Public Sub Message() by saying 'MessageThread.Start()'. Ok thats your first thread, now we will make another. Add the following code in Sub Main():
Code:
Dim FileDeleteThread As New System.Threading.Thread(AddressOf FileDelete)
        FileDeleteThread.Start()
I bet you can tell what this will do, yep thats right it will start the 'Public Sub FileDelete()' on its own thread and start it.

Now our code should look like this:

Code:
Imports System.IO.File
Module Module1

    Sub Main()
        Dim MessageThread As New System.Threading.Thread(AddressOf Message)
        Dim FileDeleteThread As New System.Threading.Thread(AddressOf FileDelete)
        FileDeleteThread.Start()
        MessageThread.Start()
    End Sub

    Public Sub Message()
        Console.Write("Please visit www.unknowncheats.com")
    End Sub

    Public Sub FileDelete()
        IO.File.Delete("C:\Test.txt")
    End Sub

End Module
Now when the program is executed rather then executing each line of code and waiting for it to finish befor moving onto the next, it will load our Message sub on its own thread, and our FileDelete sub on its own thread. This will now avoid a code gridlock, because while our Message sub is doing its stuff, our FileDelete Sub can do its stuff to; and not have to wait for Message sub to complete (think of this as a 2 lane road rather then one).

Well thats all folks, i hope it was easier for you to understand then it was for me to write (in a way that is easy for newbies to understand lol). If you have any questions please post them here or contact me on msn at acco-2006@hotmail.com
Chow
Attached Files
File Type: rar MultiThreading.rar (15.8 KB, 70 views)
Reply With Quote
Reply

  • Submit Thread to Digg
  • Submit Thread to del.icio.us
  • Submit Thread to StumbleUpon
  • Submit Thread to Google
  • Submit Thread to Facebook
  • Submit Thread to My Yahoo!
  • Submit Thread to MySpace
  • Submit Thread to Twitter
  • Submit Thread to Reddit

Tags
multithreading, vb2005

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT. The time now is 08:48 AM.