|
-
Oct 5th, 2010, 04:38 PM
#1
Thread Starter
Addicted Member
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:
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:
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:
Sub someSubRoutine(ByVal value As String)
RichTextBox1.AppendText(value)
End Sub
Now, go back a step and in your Form's "Load" event, establish your declaration from earlier, as such;
establish Code:
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:
Sub appendRTX(ByVal value As String)
If RichTextBox1.InvokeRequired = True Then
RichTextBox1.Invoke(nameitDeclared, value.ToString)
Else
someSubRoutine(value.ToString)
End If
End Sub
There you go!
Now to see it in action! Simply create a sub call Test
Test Code Code:
Sub Test()
For i As Integer = 0 To 10000
appendRTX(i)
Next
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:
Dim newThread As Threading.Thread
newThread.IsBackground = True
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
-
Oct 7th, 2010, 05:59 AM
#2
Lively Member
Re: Multi-Threading Made EZ
hey, nice tutorial thx, i'm trying to make a form pause till a page is loaded within the webbrowser control on the form , can u tell me how do i use threading for that ?
-
Oct 7th, 2010, 08:36 AM
#3
Thread Starter
Addicted Member
Re: Multi-Threading Made EZ
I could be wrong, or I could be reading your question wrong, but I'm sure there is an event for something like that. What you could then do is have a "starter" method for your elongated code & have it fire off when the page is loaded. I don't have vs open, so again, not sure the event is there, but i'm 99% positive it is.
It should probably look something like:
vb Code:
' to simplify, this is whatever method you want it to be Private Sub ThisIsYourStartUpMethod(ByVal PassedValueIfNeeded As Object) Try Select Case PassedValueIfNeeded Case "Hysteria" If WebBrowser1.Focused Then Me.Close() Else WebBrowser1.Focus() Case Else Throw New Exception End Select Catch ex As Exception Dim bob As String = ex.StackTrace.Substring(ex.StackTrace.Length - 8) MsgBox(bob & Environment.NewLine & ex.Message) End Try End Sub ' This is the important part as this is the even you need to look for, i believe asp browser controls have somthing similar as well, but i dont do alota browser stuff Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As Whatever.TheSystem.Argument.IsForThis.Control) Handles WebBrowser1.DocumentCompleted ThisIsYourStartUpMethod(sender) End Sub
Last edited by spyk3; Oct 7th, 2010 at 08:39 AM.
Reason: explination
-
Oct 7th, 2010, 08:42 AM
#4
Thread Starter
Addicted Member
Re: Multi-Threading Made EZ
On an added note, you might check This nice tut by zeidhaddadin here on the forums!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|