2
Hello, my dear friends! I’m looking for the best way to work with simultaneous tasks in a graphical environment.
I have tried using: (Thread), (Task) and (Backgroundworker). But in all of them I had the same problem: not having permission to manipulate an object from another thread and if I use the invoke method the application is very slow. In addition to this problem there is another: I cannot receive the return of a function using these tools.
I need to do a very fast application, I want it to be as fluid as possible. I would like you to share how you use simultaneous tasks... I want to know if there are techniques or even other libraries like these...
My focus is to perform a certain function "n" times and, this function will return a message in a component as a Listbox. I need the information to be fast.
Thank you in advance!
I took a look at how Synclock works, in fact I had never heard of it and even interested myself, but I still have the same problem... I’ll post a piece of my code so you can analyze and tell me which would be the best option
A class 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 one msgbox
to show the result would work, but I need it to be on Listbox
, and the Thread
is not allowed... any idea?
This does not solve the cross-thread permission problem.
– dcastro
I think it’s now clear why I use Synclock Mas and as for the permissions problem, what options do I have ?
– Wesley Silva
I don’t know exactly what error you are having, but in its place, I would pass to the function "worker" the reference to the list you want to update on screen.
– Leonel Sanches da Silva
Could you send me a little example of how to pass the listbox reference ?
– Wesley Silva
@Wesleysilva I edited the answer.
– Leonel Sanches da Silva