DebateFort - Where Warriors Come To Debate
RAGECRY - Funny, Amusing, Interesting, Trending & Viral Videos and Images
GameOrc - Free Flash Games Online

Thread: Threading

Results 1 to 8 of 8
  1. #1
    MPGH Member
    Donator
    Sketchy's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Location
    Winnipeg, MB
    Posts
    4,887
    Reputation
    515
    Thanks
    600
    My Mood
    Inspired

    Threading

    I don't understand that shit on Google. I need somebody to tutor me on how to use threading.

    I keep trying to figure it out from Google, but every guide on it says a different thing. I just need somebody to explain it to me, then show me how, allowing me to understand it.

    My MSN is MPGH_iFacebook@hotmail.com , please add me and explain this stuff. Will attempt to pay you back somehow.

    Can anybody post an example of threading too? For example, use a thread to run this code:
    Code:
    Private Sub Count()
    
            Dim i As Integer = 0
    
            Do While True
    
                i += 1
                ListBox1.Items.Add(i)
    
            Loop
    
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Count()
        End Sub
    mpghsketchy@hotmail.com

  2. #2
    Wave to yesterday
    Former Staff
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    Somewhere where the Sunlight hurts my eyes.
    Posts
    5,652
    Reputation
    865
    Thanks
    4,842
    My Mood
    Mellow
    Threading is a bit difficult to actually understand and I emphasise "understand"; people can get away with basic multi-threading without actually understanding much about how threading works (I'm talking about Control.CheckForIllegalCrossThreadCalls = false and shit like that). I'd recommend reading up on computer threading (from a non-programming perspective) before you try to take it any further. You'll also need to look up delegates for cross-thread actions.

    John Mcilhinney does a pretty good job at explaining over at VBForums:
    VBForums - Search Results

    if that comes up wrong (search probs expired) just lookup jmcilhinney and follow the "my codebank submissions" link in his signature.

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    So tear the pieces from the bone,
    Like you've torn us apart.
    We build bridges, just for burning


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  3. #3
    Former Staff
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,750
    Reputation
    445
    Thanks
    1,492
    My Mood
    Amused
    Suppose you have a long running code like this:

    Code:
    Sub SomeLongRunningCode()
            Dim Counter As Long = 1
            While Counter <= 100000000000000
                ListBox1.Items.Add(Counter)
                Counter += 1
            End While
    End Sub
    You obviously wouldn't want to run it on the main thread, thereby blocking the application. You use threading, so this code works on a separate thread. Creating a thread is dead simple. You simply pass the procedure above to the thread class and then start the thread as shown below:

    Code:
    Dim WorkerThread As New Threading.Thread(AddressOf SomeLongRunningCode)
    WorkerThread.Start()
    The first line creates a new thread and passes the address of our procedure that we want to run separately. And then on the second line we just start the thread.
    There is however, one problem with our procedure. We are adding the elements to the ListBox1 which is running with the main application's thread. The thread we created cannot access the ListBox directly. To solve this problem, we use Delegates. A delegate is a type which stores a reference to a method (or multiple methods) in an object.

    To solve our problem, we first declare a Delegate:

    Code:
    Delegate Sub AddItem(ByVal Item As Long)
    This line creates a delegate procedure named AddItem that takes 1 parameter (which is the item that we want to add to the list).

    Next, we need to check if our ListBox requires invoke. If required, we invoke the ListBox passing our delegate along with the parameters.

    Code:
    Sub AddToList(ByVal Item As Long)
            If ListBox1.InvokeRequired Then
                ListBox1.Invoke(New AddItem(AddressOf AddToList), Item)
            Else
                ListBox1.Items.Add(Item)
            End If
    End Sub
    This method takes one parameter (the item that we want to add to the list). The method starts by checking if the ListBox requires invoke. If it does, it invokes it by calling the invoke method. The invoke method takes 2 parameters. The first one is the delegate object and the second is the parameter array that is passed into the delegate. In this case, we create a new instance of the our delegate (New AddItem) and passing it the address of the this method itself (AddToList) because if the invoke isn't required, it will comeback and execute this line:

    Code:
    ListBox1.Items.Add(Item)
    In the second parameter of the invoke method, we just pass the Item parameter of the AddToList method that will eventually be passed to our new delegate instance that we created.

    Next, we need a little modification to our SomeLongRunningCode method.

    Code:
    Sub SomeLongRunningCode()
            Dim Counter As Long = 1
            While Counter <= 100000000000000
                AddToList(Counter)
                Counter += 1
            End While
    End Sub
    Notice how the ListBox1.Items.Add(Counter) is replaced with AddToList(Counter). Everytime we want to add an item to the list we call the AddToList method that utilizes our delegate. This way we can add items to the ListBox without any runtime exceptions of invalid cross thread operation.

    Finally, we create a thread, pass the address of SomeLongRunningCode() and start it:

    Code:
    Dim WorkerThread As New Threading.Thread(AddressOf SomeLongRunningCode)
    WorkerThread.Start()
    Now, if you'll run the code, the long running code will execute on a separate thread and can now access the visual elements of your form too without blocking the main application.

    I'll quickly summarize what we have done above:
    1: We created a delegate that will hold a reference (address) of our AddToList method.
    2: We created a simple method that performs some checks and adds item to our ListBox. We checked if ListBox1 requires invoke. If it does, we invoked it. When we have multiple threads in our application, the only thread that can modify the visual controls is the thread that owns those controls. So, to modify these controls from child threads of your process, you need to push the function call to the correct thread. This is the main purpose of invocation.
    3: After the invocation was completed, we simply added our item to the ListBox in the AddToList method.
    4: We created a method that contains a code that can run for a long time thereby blocking the main application for a certain period of time. So, instead of directly adding our items to ListBox from that method we called the AddToList method everytime we wanted to add an item to the list. This was to make sure that we can access visual controls from our child thread.
    5: We created a new Thread object and passed the address of our SomeLongRunningCode method. Finally, we started the thread to execute the long running code.

    Hope this helps. Feel free to ask any questions you have.
    @Sketchy
    Last edited by Hassan; 05-04-2012 at 01:59 AM.

  4. The Following User Says Thank You to Hassan For This Useful Post:

    Sketchy (05-04-2012)

  5. #4
    Threadstarter
    MPGH Member
    Donator
    Sketchy's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Location
    Winnipeg, MB
    Posts
    4,887
    Reputation
    515
    Thanks
    600
    My Mood
    Inspired
    Quote Originally Posted by Hassan View Post
    Suppose you have a long running code like this:

    Code:
    Sub SomeLongRunningCode()
            Dim Counter As Long = 1
            While Counter <= 100000000000000
                ListBox1.Items.Add(Counter)
                Counter += 1
            End While
    End Sub
    You obviously wouldn't want to run it on the main thread, thereby blocking the application. You use threading, so this code works on a separate thread. Creating a thread is dead simple. You simply pass the procedure above to the thread class and then start the thread as shown below:

    Code:
    Dim WorkerThread As New Threading.Thread(AddressOf SomeLongRunningCode)
    WorkerThread.Start()
    The first line creates a new thread and passes the address of our procedure that we want to run separately. And then on the second line we just start the thread.
    There is however, one problem with our procedure. We are adding the elements to the ListBox1 which is running with the main application's thread. The thread we created cannot access the ListBox directly. To solve this problem, we use Delegates. A delegate is a type which stores a reference to a method (or multiple methods) in an object.

    To solve our problem, we first declare a Delegate:

    Code:
    Delegate Sub AddItem(ByVal Item As Long)
    This line creates a delegate procedure named AddItem that takes 1 parameter (which is the item that we want to add to the list).

    Next, we need to check if our ListBox requires invoke. If required, we invoke the ListBox passing our delegate along with the parameters.

    Code:
    Sub AddToList(ByVal Item As Long)
            If ListBox1.InvokeRequired Then
                ListBox1.Invoke(New AddItem(AddressOf AddToList), Item)
            Else
                ListBox1.Items.Add(Item)
            End If
    End Sub
    This method takes one parameter (the item that we want to add to the list). The method starts by checking if the ListBox requires invoke. If it does, it invokes it by calling the invoke method. The invoke method takes 2 parameters. The first one is the delegate object and the second is the parameter array that is passed into the delegate. In this case, we create a new instance of the our delegate (New AddItem) and passing it the address of the this method itself (AddToList) because if the invoke isn't required, it will comeback and execute this line:

    Code:
    ListBox1.Items.Add(Item)
    In the second parameter of the invoke method, we just pass the Item parameter of the AddToList method that will eventually be passed to our new delegate instance that we created.

    Next, we need a little modification to our SomeLongRunningCode method.

    Code:
    Sub SomeLongRunningCode()
            Dim Counter As Long = 1
            While Counter <= 100000000000000
                AddToList(Counter)
                Counter += 1
            End While
    End Sub
    Notice how the ListBox1.Items.Add(Counter) is replaced with AddToList(Counter). Everytime we want to add an item to the list we call the AddToList method that utilizes our delegate. This way we can add items to the ListBox without any runtime exceptions of invalid cross thread operation.

    Finally, we create a thread, pass the address of SomeLongRunningCode() and start it:

    Code:
    Dim WorkerThread As New Threading.Thread(AddressOf SomeLongRunningCode)
    WorkerThread.Start()
    Now, if you'll run the code, the long running code will execute on a separate thread and can now access the visual elements of your form too without blocking the main application.

    I'll quickly summarize what we have done above:
    1: We created a delegate that will hold a reference (address) of our AddToList method.
    2: We created a simple method that performs some checks and adds item to our ListBox. We checked if ListBox1 requires invoke. If it does, we invoked it. When we have multiple threads in our application, the only thread that can modify the visual controls is the thread that owns those controls. So, to modify these controls from child threads of your process, you need to push the function call to the correct thread. This is the main purpose of invocation.
    3: After the invocation was completed, we simply added our item to the ListBox in the AddToList method.
    4: We created a method that contains a code that can run for a long time thereby blocking the main application for a certain period of time. So, instead of directly adding our items to ListBox from that method we called the AddToList method everytime we wanted to add an item to the list. This was to make sure that we can access visual controls from our child thread.
    5: We created a new Thread object and passed the address of our SomeLongRunningCode method. Finally, we started the thread to execute the long running code.

    Hope this helps. Feel free to ask any questions you have.
    @Sketchy
    This is really helpful, I'll try it later today.
    mpghsketchy@hotmail.com

  6. #5
    Threadstarter
    MPGH Member
    Donator
    Sketchy's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Location
    Winnipeg, MB
    Posts
    4,887
    Reputation
    515
    Thanks
    600
    My Mood
    Inspired
    Quote Originally Posted by Hassan View Post
    Suppose you have a long running code like this:

    Code:
    Sub SomeLongRunningCode()
            Dim Counter As Long = 1
            While Counter <= 100000000000000
                ListBox1.Items.Add(Counter)
                Counter += 1
            End While
    End Sub
    You obviously wouldn't want to run it on the main thread, thereby blocking the application. You use threading, so this code works on a separate thread. Creating a thread is dead simple. You simply pass the procedure above to the thread class and then start the thread as shown below:

    Code:
    Dim WorkerThread As New Threading.Thread(AddressOf SomeLongRunningCode)
    WorkerThread.Start()
    The first line creates a new thread and passes the address of our procedure that we want to run separately. And then on the second line we just start the thread.
    There is however, one problem with our procedure. We are adding the elements to the ListBox1 which is running with the main application's thread. The thread we created cannot access the ListBox directly. To solve this problem, we use Delegates. A delegate is a type which stores a reference to a method (or multiple methods) in an object.

    To solve our problem, we first declare a Delegate:

    Code:
    Delegate Sub AddItem(ByVal Item As Long)
    This line creates a delegate procedure named AddItem that takes 1 parameter (which is the item that we want to add to the list).

    Next, we need to check if our ListBox requires invoke. If required, we invoke the ListBox passing our delegate along with the parameters.

    Code:
    Sub AddToList(ByVal Item As Long)
            If ListBox1.InvokeRequired Then
                ListBox1.Invoke(New AddItem(AddressOf AddToList), Item)
            Else
                ListBox1.Items.Add(Item)
            End If
    End Sub
    This method takes one parameter (the item that we want to add to the list). The method starts by checking if the ListBox requires invoke. If it does, it invokes it by calling the invoke method. The invoke method takes 2 parameters. The first one is the delegate object and the second is the parameter array that is passed into the delegate. In this case, we create a new instance of the our delegate (New AddItem) and passing it the address of the this method itself (AddToList) because if the invoke isn't required, it will comeback and execute this line:

    Code:
    ListBox1.Items.Add(Item)
    In the second parameter of the invoke method, we just pass the Item parameter of the AddToList method that will eventually be passed to our new delegate instance that we created.

    Next, we need a little modification to our SomeLongRunningCode method.

    Code:
    Sub SomeLongRunningCode()
            Dim Counter As Long = 1
            While Counter <= 100000000000000
                AddToList(Counter)
                Counter += 1
            End While
    End Sub
    Notice how the ListBox1.Items.Add(Counter) is replaced with AddToList(Counter). Everytime we want to add an item to the list we call the AddToList method that utilizes our delegate. This way we can add items to the ListBox without any runtime exceptions of invalid cross thread operation.

    Finally, we create a thread, pass the address of SomeLongRunningCode() and start it:

    Code:
    Dim WorkerThread As New Threading.Thread(AddressOf SomeLongRunningCode)
    WorkerThread.Start()
    Now, if you'll run the code, the long running code will execute on a separate thread and can now access the visual elements of your form too without blocking the main application.

    I'll quickly summarize what we have done above:
    1: We created a delegate that will hold a reference (address) of our AddToList method.
    2: We created a simple method that performs some checks and adds item to our ListBox. We checked if ListBox1 requires invoke. If it does, we invoked it. When we have multiple threads in our application, the only thread that can modify the visual controls is the thread that owns those controls. So, to modify these controls from child threads of your process, you need to push the function call to the correct thread. This is the main purpose of invocation.
    3: After the invocation was completed, we simply added our item to the ListBox in the AddToList method.
    4: We created a method that contains a code that can run for a long time thereby blocking the main application for a certain period of time. So, instead of directly adding our items to ListBox from that method we called the AddToList method everytime we wanted to add an item to the list. This was to make sure that we can access visual controls from our child thread.
    5: We created a new Thread object and passed the address of our SomeLongRunningCode method. Finally, we started the thread to execute the long running code.

    Hope this helps. Feel free to ask any questions you have.
    @Sketchy
    Just tried it out, and I got a "Ran out of memory" error. Not to mention I might be doing it wrong.
    mpghsketchy@hotmail.com

  7. #6
    Wave to yesterday
    Former Staff
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    Somewhere where the Sunlight hurts my eyes.
    Posts
    5,652
    Reputation
    865
    Thanks
    4,842
    My Mood
    Mellow
    If you were looping to "100000000000000" as Hassan put in his post, of course you're going to run out of memory haha.

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    So tear the pieces from the bone,
    Like you've torn us apart.
    We build bridges, just for burning


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  8. #7
    Threadstarter
    MPGH Member
    Donator
    Sketchy's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Location
    Winnipeg, MB
    Posts
    4,887
    Reputation
    515
    Thanks
    600
    My Mood
    Inspired
    Quote Originally Posted by Jason View Post
    If you were looping to "100000000000000" as Hassan put in his post, of course you're going to run out of memory haha.
    That's the first thing I thought of. So I made it 100,000 to no avail. Then I tried 10 and it worked. Just making sure that it's not some error because I messed up.
    mpghsketchy@hotmail.com

  9. #8
    Former Staff
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,750
    Reputation
    445
    Thanks
    1,492
    My Mood
    Amused
    Quote Originally Posted by Sketchy View Post

    That's the first thing I thought of. So I made it 100,000 to no avail. Then I tried 10 and it worked. Just making sure that it's not some error because I messed up.
    100,000 is still a pretty big value if you have limited resources. If you're adding to some generic collection, that would be a different story. But since you are adding it to ListBox, you can't add such a huge amount of items.

Similar Threads

  1. First Spam Thread
    By EleMentX in forum Spammers Corner
    Replies: 19
    Last Post: 01-24-2013, 06:29 PM
  2. obligitary count to a bagalion thread
    By i eat trees in forum Spammers Corner
    Replies: 166
    Last Post: 05-03-2012, 01:57 PM
  3. Challenge Thread
    By arunforce in forum General
    Replies: 7
    Last Post: 03-26-2007, 01:22 PM
  4. Server List Thread
    By pesst in forum Gunz General
    Replies: 7
    Last Post: 02-09-2006, 03:55 PM
  5. I eat trees (lost thread)
    By Chronologix in forum Help & Requests
    Replies: 14
    Last Post: 01-27-2006, 09:29 PM