parameter thread in vb - vb.net

First of all, i don't have a good understanding about thread in vb, i tried to get info about it, but i'm still confused about how its work, addressof, the correct implementation and so on. I want to ask about how to create a new thread with parameter so i pass a value from the main thread.
I try to do a loop and each loop create a new thread and do ping. Below is my code:
For i = 10 To 50
Dim worker As New Thread(New ThreadStart(Function()
My.Computer.Network.Ping("192.168.1." & i)
Console.WriteLine("192.168.1." & i)
End Function))
worker.Start()
Next
I realize this is wrong because the result will loop and ping the last value of variable i. So i want to ask about the correct suggestion about it. I will really appreciate if you add a simple explanation for me just to get a better understanding about thread.
Thank you in advance

Your code is almost correct. However, since you are using the same variable for all your created threads. So when your thread starts it more or less contains a pointer to i rather than the value of i. Which means that as your loop progress the value of i change. So when your threads actually start working the loop has completed and the value of i has changed to 51.
To resolve this you have to create a new integer inside the loop. So that the supplied object is unique for every loop. Like so:
Dim x as Integer = i
Then supply x instead of i to the Thread and you are all set.
Here is some reading on the topic:
https://devblogs.microsoft.com/vbteam/closures-in-vb-part-5-looping/

You may also want to try async/await as a better alternative.
Example
Make sure you make the caller async method by using the async keyword:
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 1 To 5
Dim ip As String = $"192.168.1.{i}"
Await Task.Run(Sub() Pinger(ip))
Next
End Sub
Private Sub Pinger(ip As String)
If My.Computer.Network.Ping(ip) Then
Console.WriteLine($"{ip} is alive.")
Else
Console.WriteLine($"{ip} is dead.")
End If
End Sub
The output:
192.168.1.1 is alive.
192.168.1.2 is dead.
192.168.1.3 is dead.
192.168.1.4 is dead.
192.168.1.5 is alive.
Why do you get an ordered sequence? I quote:
The method usually includes at least one await expression, which marks
a point where the method can't continue until the awaited asynchronous
operation is complete.
Please refer to the given link above for more useful details.

Related

Wait for value before returing function result

Is there a way to make a function wait for a specific external value before returning the result?
Here is a very rudimentary example:
Function DoSomething() As boolean
'' ... do something external that takes time and sets a Registry value when done
'' ...
Dim isTaskDone as Boolean
Do Until isTaskDone = True
isTaskDone = Registry.GetValue("ExternalValuePath", "valName", 0)
Loop
Return var
End Function
This actually works for my needs, but I was wondering maybe there is more elegant way of achieving this?
Also I am not sure whether this solution is OK to leave it like that (using Until to effectively stall the function).
I apologize for a rather vague question.
Update:
I guess I am looking for some sort of simple one-call-solution, where I can call one function, it does everything I need and returns a result only when everything is done. So I know it is safe to continue.
I have also tried this with Timers, but I don't think it is possible to contain everything in a one-call-solution when using Timers.
Better approach would be to create a registry watcher using WMI. Refer the link, its in C# should not be a problem to convert in VB.Net
Your method uses a busy wait loop which will lock up your UI (and waste your CPU cycles). It's possible a BackgroundWorker could be used for this but you could likely also accomplish it with a Timer. Not very elegant perhaps but it wouldn't freeze your interface.
So rather than make it a function, a Sub would do. Then add a Timer with default Interval of 100. In your DoSomething Sub, Enable the timer. And your timer code would be something like this
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Registry.GetValue("ExternalValuePath", "valName", 0) Then
Timer1.Enabled = False
'do whatever needs to be done next
End If
End Sub

Multithreading in vb.net to simulate task

I have a program that is doing one task.
For Example i have one list box containing some links.
And on the other hand my program is opening them one by one but i want it to be done faster
i have used for-each loop for that purpose.
All what i want to do is i wanna give every 2 or 3 link to a different thread or if there is any other solution to make it Faster Kindly tell me.
This is a small piece of code from my program.
For value As Integer = 1 To TextBox1.Text
If (value = TextBox1.Text) Then
Exit For
End If
Dim page As String = "-p-" & value
Extractor.ScrapLinks(txturl.Text + page, lstbox)
lbllinks.Text = lstbox.Items.Count
Next
I don't know what Extractor.ScrapLinks actually do, but it seems that you need to access UI thread and you cannot create multiple UI threads so eventually you will process them sequentially
What you can do to improve the solution is to read the data you want from the UI controls and then process that data on a separate thread, after completion you can fill the results into the UI by invoking some method on the UI thread as shown below
Delegate Sub PerformOperationDel(value As Integer)
Sub PerformOperation(value As Integer)
Dim page As String = "-p-" & value
Extractor.ScrapLinks(txturl.Text + page, lstbox)
lbllinks.Text = lstbox.Items.Count
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For value As Integer = 1 To CInt(TextBox1.Text) - 1
lstbox.BeginInvoke(New PerformOperationDel(AddressOf PerformOperation))
Next
End Sub
You can use backgroundworkers but notice that you cannot access UI controls in the DoWork but you can access them on work completed (Refer to: Background worker proper way to access UI)
Best of luck

VB.NET multithreading, block thread until notification received

Before I begin, I have to apologize for two things. One is that it is very difficult for me to explain things in a concise manner. Two is that I need to be somewhat vague due to the nature of the company I work for.
I am working on enhancing the functionality of an application that I've inherited. It is a very intensive application that runs a good portion of my company's day to day business. Because of this I am limited to the scope of what I can change--otherwise I'd probably rewrite it from scratch. Anyways, here is what I need to do:
I have several threads that all perform the same task but on different data input streams. Each thread interacts through an API from another software system we pay licensing on to write out to what is called channels. Unfortunately we have only licensed a certain number of concurrently running channels, so this application is supposed to turn them on an off as needed.
Each thread should wait until there is an available channel, lock the channel for itself and perform its processing and then release the channel. Unfortunately, I don't know how to do this, especially across multiple threads. I also don't really know what to search Google or this site for, or I'd probably have my answer. This was my thought:
A class that handles the distribution of channel numbers. Each thread makes a call to a member of this class. When it does this it would enter a queue and block until the channel handling class recognizes that we have a channel, signals the waiting thread that a channel is available and passing it the channel id. I have no idea where to begin even looking this up. Below I have some horribly written PsuedoCode of how in my mind I would think it would work.
Public Class ChannelHandler
Private Shared WaitQueue as New Queue(of Thread)
'// calling thread adds itself to the queue
Public Shared Sub WaitForChannel(byref t as thread)
WaitQueue.enqueue(t)
End Sub
Public Shared Sub ReleaseChannel(chanNum as integer)
'// my own processing to make the chan num available again
End Sub
'// this would be running on a separate thread, polling my database
'// for an available channel, when it finds one, somehow signal
'// the first thread in the queue that its got a channel and here's the id
Public Shared Sub ChannelLoop()
while true
if WaitQueue.length > 0 then
if thereIsAChannelAvailable then '//i can figure this out my own
dim t as thread = ctype(WaitQueue.dequeue(), Thread)
lockTheChannel(TheAvailableChannelNumber) 'performed by me
'// signal the thread, passing it the channel number
t => SignalReady(theAvailableChannelNumber) '// how to signal?
end if
end if
end while
End Sub
End Class
and then
'// this inside the function that is doing the processing:
ChannelHandler.requestChannel(CurrentThread)
while (waitingForSignal) '// how?
block '// how?
dim channelNumber as int => getChannelNumberThatWasSignaledBack
'// perform processing with channelNumber
ChannelHandler.ReleaseChannel(channelNumber)
I am working with the .NET Framework 3.5 in VB.NET. I am sure there has got to be some sort of mechanism already built for this, but as I said I have no idea exactly what keywords I should be searching for. Any input pointing me in the right direction (ie specific .NET framework classes to use or code samples) would be greatly appreciated. If I need to elaborate on anything, please let me know and I will to the best of my ability.
Edit: The other problem that I have is that these channels can be turned on/off from outside of this application, manually by the user (or as a result of a user initiated event). I am not concerned with a channel be shut down while a thread is using it (it would throw an exception and then pick back up next time it came through. But the issue is that there are not a constant number of threads fighting over a constant number of channels (if a user turns one on manually, the count is reduced, etc). Both items are variable, so I cant rely on the fact that there are no external forces (ie, something outside this set of threads, which is why I do some processing via my DB to determine an available channel number)
What I would do:
Switch the System.Threading.Thread by the System.Threading.Tasks.Task class.
If a new Task needs to be created, but the List(Of Task) (or, in your example, Queue(Of Task) ) count greater than the maximum permitted, use the Task.WaitAny method.
EDIT:
As I answered the previous block on my phone (which is pretty challenging for writing code), let now me write an example about how I would do it:
Imports System.Threading.Tasks
Imports System.Collections.Generic
Public Class Sample
Private Const MAXIMUM_PERMITTED As Integer = 3
Private _waitQueue As New Queue(Of Task)
Public Sub AssignChannel()
Static Dim queueManagerCreated As Boolean
If Not queueManagerCreated Then
Task.Factory.StartNew(Sub() ManageQueue())
queueManagerCreated = True
End If
Dim newTask As New Task(Sub()
' Connect to 3rd Party software
End Sub)
SyncLock (_waitQueue)
_waitQueue.Enqueue(newTask)
End SyncLock
End Sub
Private Sub ManageQueue()
Dim tasksRunning As New List(Of Task)
While True
If _waitQueue.Count <= 0 Then
Threading.Thread.Sleep(10)
Continue While
End If
If tasksRunning.Count > MAXIMUM_PERMITTED Then
Dim endedTaskPos As Integer = Task.WaitAny(tasksRunning.ToArray)
If endedTaskPos > -1 AndAlso
endedTaskPos <= tasksRunning.Count Then
tasksRunning.RemoveAt(endedTaskPos)
Else
Continue While
End If
End If
Dim taskToStart As Task
SyncLock (_waitQueue)
taskToStart = _waitQueue.Dequeue()
End SyncLock
tasksRunning.Add(taskToStart)
taskToStart.Start()
End While
End Sub
End Class

How to make a very simple asynchronous method call in vb.net

I just have a simple vb.net website that need to call a Sub that performs a very long task that works with syncing up some directories in the filesystem (details not important).
When I call the method, it eventually times out on the website waiting for the sub routine to complete. However, even though the website times out, the routine eventually completes it's task and all the directories end up as they should.
I want to just prevent the timeout so I'd like to just call the Sub asynchronously. I do not need (or even want) and callback/confirmation that it ran successfully.
So, how can I call my method asynchronously inside a website using VB.net?
If you need to some code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Call DoAsyncWork()
End Sub
Protected Sub DoAsyncWork()
Dim ID As String = ParentAccountID
Dim ParentDirectory As String = ConfigurationManager.AppSettings("AcctDataDirectory")
Dim account As New Account()
Dim accts As IEnumerable(Of Account) = account.GetAccounts(ID)
For Each f As String In My.Computer.FileSystem.GetFiles(ParentDirectory)
If f.EndsWith(".txt") Then
Dim LastSlashIndex As Integer = f.LastIndexOf("\")
Dim newFilePath As String = f.Insert(LastSlashIndex, "\Templates")
My.Computer.FileSystem.CopyFile(f, newFilePath)
End If
Next
For Each acct As Account In accts
If acct.ID <> ID Then
Dim ChildDirectory As String = ConfigurationManager.AppSettings("AcctDataDirectory") & acct.ID
If My.Computer.FileSystem.DirectoryExists(ChildDirectory) = False Then
IO.Directory.CreateDirectory(ChildDirectory)
End If
My.Computer.FileSystem.DeleteDirectory(ChildDirectory, FileIO.DeleteDirectoryOption.DeleteAllContents)
My.Computer.FileSystem.CopyDirectory(ParentDirectory, ChildDirectory, True)
Else
End If
Next
End Sub
I wouldn't recommend using the Thread class unless you need a lot more control over the thread, as creating and tearing down threads is expensive. Instead, I would recommend using a ThreadPool thread. See this for a good read.
You can execute your method on a ThreadPool thread like this:
System.Threading.ThreadPool.QueueUserWorkItem(AddressOf DoAsyncWork)
You'll also need to change your method signature to...
Protected Sub DoAsyncWork(state As Object) 'even if you don't use the state object
Finally, also be aware that unhandled exceptions in other threads will kill IIS. See this article (old but still relevant; not sure about the solutions though since I don't reaslly use ASP.NET).
You could do this with a simple thread:
Add :
Imports System.Threading
And wherever you want it to run :
Dim t As New Thread(New ThreadStart(AddressOf DoAsyncWork))
t.Priority = Threading.ThreadPriority.Normal
t.Start()
The call to t.Start() returns immediately and the new thread runs DoAsyncWork in the background until it completes. You would have to make sure that everything in that call was thread-safe but at first glance it generally seems to be so already.
I also was looking for information on Asynchronous programming in VB. In addition to this thread, I also found the following: beginning with Visual Studio 2012 and .Net Framework 4.5, VB was given two new keywords to make a method asynchronous right in the declaration, without using Thread or Threadpool. The new keywords are "Async" and "Await". You may refer to the following links if you wish:
http://msdn.microsoft.com/library/hh191443%28vs.110%29.aspx
https://msdn.microsoft.com/en-us/library/hh191564%28v=vs.110%29.aspx
This is an older thread, but I figured I'd add to it anyway as I recently needed to address this. If you want to use the ThreadPool to call a method with parameters, you can modify #Timiz0r's example as such:
System.Threading.ThreadPool.QueueUserWorkItem(Sub() MethodName( param1, param2, ...))

Simultaneous threads only one seems to function at a time?

Threads a + b, (both are trying to delete files).
a gets called first, then b while a is still running.
b deletes the file successfully but a doesn't.
If I run a on its own, a's file deletes fine.
When I step through the code I can see that a's MultiAttemptFilename gets overwritten with b's.
I don't understand.
I have an ajax call pointing to a generic handler which passes the filename along with it.
In my handler I have the following code:
Dim Dc As New Document
Dim MyThread As New Thread(AddressOf Dc.DeleteFileMulitAttempt)
Dc.MulitAttemptFilename = Filename
MyThread.Start()
From my 'Document' class I'm calling the following:
#Region "Delete"
Public MulitAttemptFilename As String = ""
Public Sub DeleteFileMulitAttempt()
Dim TimeBetweenAttempts As Integer = 2000
Dim NumberOfAttempts As Integer = 60
Dim AttemptNumber As Integer = 0
Dim Success As Boolean = False
While (AttemptNumber < NumberOfAttempts)
Try
Success = (DeleteFile(MulitAttemptFilename) = "Ok")
Catch ex As Exception
Success = False
End Try
If (Success) Then Exit While
Thread.Sleep(TimeBetweenAttempts)
AttemptNumber += 1
End While
End If
End Sub
...
This is to handle cancelled/failed uploads as they don't always delete right away (server locks etc), hence the loop.
Am I missing something fundamental here?
It seems like you might be missing the fundamental concept of multi-threaded concurrency. There are books dedicated to this, and often sections of .NET books will address this issue. Here's just one article by Microsoft on the topic.
One short answer is you need to use VB's "lock" keyword. You create an object and you do roughly something like
lock(yourLockObject)
{
//any code that might access a shared resource like the variable
//MulitAttempFilename [sic] would go here.
}
I don't speak VB but it looks like you're making the one thing that really needs to be protected a global variable. Global data is pretty much a bad idea in any form and when it comes to multi-threading it's a really, really bad idea. You'll have to rewrite your code to protect access to the name of the file being deleted. While you're reading up on multi-threading you might also want to learn about thread pools.