Results 1 to 4 of 4

Thread: Multi-Threading Made EZ

Threaded View

  1. #1

    Thread Starter
    Addicted Member spyk3's Avatar
    Join Date
    Apr 2009
    Location
    Dothan, AL
    Posts
    218

    Multi-Threading Made EZ

    I realize it's a bit late in the game for this (since wpf handles it so well), but i'm bored and killing time.

    This simple walkthrough will show you how to post to a richtextbox on a seperate thread (could easily show a status bar for installing using this method).

    First, of course, make a form and place a richtextbox on it.

    Now, in your form's code, delegate a sub routine with one variable for string passing as such

    Delegate Code:
    1. Delegate Sub nameIt(ByVal value As String)

    Now, I know this seems redundant to you first timers, but now you "Declare" your new Sub as follows:

    Declare Code:
    1. Private nameItDeclared as nameIt
    Notice we declare this variable as the delegated sub, this will be usefull when we actually call upon the sub!

    Now create a simple sub routine (no invoking) to preform the action you want performed. The key here is simplicity! Grab only the direct part of your sub. In our case we are using the "append" method of the richtextbox, as so;

    subRout Code:
    1. Sub someSubRoutine(ByVal value As String)
    2.                RichTextBox1.AppendText(value)
    3.             End Sub

    Now, go back a step and in your Form's "Load" event, establish your declaration from earlier, as such;

    establish Code:
    1. nameitDeclared = AddressOf someSubRoutine

    This tells our form that our declared sub needs to use "someSubRoutine" as its established method, otherwise, nameItDeclared wouldn't do anything!

    Now for the fun part, the use of it all!

    For this I'm going to make things VERY SIMPLE!!!
    Create a new sub called appendRTX and add the following

    theUse Code:
    1. Sub appendRTX(ByVal value As String)
    2.             If RichTextBox1.InvokeRequired = True Then
    3.                 RichTextBox1.Invoke(nameitDeclared, value.ToString)
    4.             Else
    5.                 someSubRoutine(value.ToString)
    6.             End If
    7. End Sub

    There you go!

    Now to see it in action! Simply create a sub call Test

    Test Code Code:
    1. Sub Test()
    2.         For i As Integer = 0 To 10000
    3.             appendRTX(i)
    4.         Next
    5.     End Sub

    One Last thing! In your form's "Load" event (below our establied declaration) place the following code to start our test on new thread!

    finally Code:
    1. Dim newThread As Threading.Thread
    2.         newThread.IsBackground = True
    3.         newThread.Start(Test)

    Follow all the directions right? This should work perfect and easy, course I ahvn't tested it cause i'm busy being bored, but if it breaks, someone i'm sure will tell me and i fix it?

    hehehe :P

    Enjoy peeps, see you all laters!
    Last edited by spyk3; Oct 7th, 2010 at 09:01 AM. Reason: fixed error i found while reading it, lolz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width