Skip to content
MPGHThe Dark Arts
/
RegisterLog in
Forum
Community
What's NewLatest posts across the boardTrendingHottest threads right nowSubscribedThreads you follow
Discussion
GeneralIntroductionsEntertainmentDebate FortFlaming & Rage
Board
News & AnnouncementsMPGH TimesSuggestions & HelpGiveaways
More Sections
Art & Graphic DesignProgrammingHackingCryptocurrency
Hacks & Cheats
Games
ValorantCS2 / CS:GOCall of Duty / WarzoneFortniteApex LegendsEscape From Tarkov
+14 moreLeague of LegendsGTA VMinecraftRustROTMGBattlefieldTroveBattleOnCombat ArmsCrossFireBlackshotRuneScapeDayZDead by Daylight
Resources
Game Hacking TutorialsReverse EngineeringGeneral Game HackingAnti-CheatConsole Game Hacking
Tools
Game Hacking ToolsTrainers & CheatsHack/Release NewsNew
Submit a release →Share your cheat, tool, or config with the community.
AINEW
AI Tools
General & DiscussionPrompt EngineeringLLM JailbreaksHotAI Agents & AutomationLocal / Open Models
AI × Gaming
AI Aimbots & VisionML Anti-CheatGame Bots & Automation
Create
AI Coding / Vibe CodingAI Art & MediaAI Voice & TTS
The AI frontier →Where game hacking meets modern machine learning. Jump in.
Marketplace
Buy & Sell
SellingBuyingTradingUser Services
Trust & Safety
Middleman LoungeMarketplace TalkVouch Copy Profiles
Money
Cryptocurrency TalkCurrency ExchangeWork & Job Offers
Start selling →List accounts, services, and goods. Use the middleman to trade safe.
MPGH The Dark Arts

A community for offensive security research, reverse engineering, and AI.

Community

ForumMarketplaceSearch

Account

RegisterLog in

Legal

Privacy PolicyForum RulesHelp & FAQ
© 2026 MPGH · All rights reserved.Built by the community, for the community. For educational purposes onlyContent is shared for security research and education — we don't condone illegal use. You're responsible for complying with applicable laws. Use at your own risk.
Home › Forum › Programming › Visual Basic Programming › Threading

Threading

Posts 1–8 of 8 · Page 1 of 1
Sketchy
Sketchy
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
#1 · 14y ago
Jason
Jason
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.
#2 · 14y ago
Hassan
Hassan
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
#3 · edited 14y ago · 14y ago
Sketchy
Sketchy
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.
#4 · 14y ago
Sketchy
Sketchy
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.
#5 · 14y ago
Jason
Jason
If you were looping to "100000000000000" as Hassan put in his post, of course you're going to run out of memory haha.
#6 · 14y ago
Sketchy
Sketchy
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.
#7 · 14y ago
Hassan
Hassan
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.
#8 · 14y ago
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

  • First Spam ThreadBy EleMentX in Spammers Corner
    34Last post 6y ago
  • obligitary count to a bagalion threadBy i eat trees in Spammers Corner
    166Last post 14y ago
  • Challenge ThreadBy arunforce in General
    7Last post 19y ago
  • I eat trees (lost thread)By Chronologix in Help & Requests
    14Last post 20y ago
  • Server List ThreadBy pesst in Gunz General
    7Last post 20y ago

Tags for this Thread

None