Hello, dear friends! I'm looking for the best way to work with concurrent tasks in a graphical environment.
I have tried to use: (Thread), (Task) and (Backgroundworker). But in all I had the same problem: not being allowed to manipulate an object from another thread and if I use the invoke method the application gets very slow. In addition to this problem there is still another one: I can not get the return of a function using these tools.
I need to make a very quick application, I want it to be as fluid as possible. I would like you to share how they use the concurrent tasks ... I want to know if there are techniques or even other libraries like these ...
My focus is to run a certain function "n" times, and this function will return a message in a component such as a Listbox. I need the information to be quick.
Thank you in advance!
I took a look at how SyncLock works, in fact I had never heard of it and it even interested me, but I still have the same problem ... I'm going to post a part of my code so that you can analyze and tell me what it would be best option
A class that I gave the connection name
Public Class Conexao
Public ip As String
Public porta As Integer
Public estado As Boolean
Public Sub conectar()
Dim cliente As New System.Net.Sockets.TcpClient
Try
cliente.Connect(ip, porta)
estado = True
Catch ex As Exception
estado = False
End Try
Form1.ListBox1.Items.Add("Porta: " & porta & " Estado: " & estado)
End Sub
End Class
And the button code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim porta As Integer
For index = 1 To 10
porta = index
Dim novaConexao = New Conexao
novaConexao.ip = "127.0.0.1"
novaConexao.porta = porta
Dim thread(10) As Threading.Thread
thread(index - 1) = New Threading.Thread(AddressOf novaConexao.conectar)
thread(index - 1).IsBackground = True
thread(index - 1).Start()
Next
End Sub
End Class
The error happens on this line:
Form1.ListBox1.Items.Add("Porta: " & porta & " Estado: " & estado)"
If I used a msgbox
to show the result would work, but I need it to be Listbox
, and Thread
is not allowed ... any idea?