Edit a Forms control data from a second thread?

J

JamesB

Hello

I have a form which contains a Listview control that is filled with data as
the program runs. This all works fine, but I want to also then do a certain
process on this data at the same time.
Because of the way the data is received into the control (from an event out
of my hands) my processing causes problems as it is time-intensive. So, I
figure my processing should go in a separate thread so it can work without
stopping the information coming into the 'box. So far so good.
But how do I read and modify the items in the ListView from the other
thread? I have just started moving the code into another class that will
become the new thread and my control name gets underlined as not being
declared. Putting the form name in front (i.e. myForm.lstProgress) also
won't work.
Any thoughts? I saw an article on oassing data to and from threads with
callbacks and so on but will this work to link directly to a control?
Thanks
James.
 
H

Herfried K. Wagner [MVP]

JamesB said:
I have a form which contains a Listview control that is filled with data
as the program runs. This all works fine, but I want to also then do a
certain process on this data at the same time.
Because of the way the data is received into the control (from an event
out of my hands) my processing causes problems as it is time-intensive.
So, I figure my processing should go in a separate thread so it can work
without stopping the information coming into the 'box. So far so good.
But how do I read and modify the items in the ListView from the other
thread? I have just started moving the code into another class that will
become the new thread and my control name gets underlined as not being
declared. Putting the form name in front (i.e. myForm.lstProgress) also
won't work.

You'll have to pass a reference which points to the form's or control's
instance to the second class. Note that you must not access the
form/control from another thread directly. Instead, use
'Control.InvokeRequired', 'Control.Invoke', and 'Control.BeginInvoke':

<URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithreading&lang=en>
 
K

Ken Tucker [MVP]

Hi,

Since you can pass a refernce to the listview when you start a
thread you need to create a class that will handle it for you. Try
something like this.

Public Class MyThreadHelper

Dim mlv As ListView

Dim trd As Threading.Thread

Public Sub New(ByRef lv As ListView)

mlv = lv

End Sub

Public Sub startthread()

If trd Is Nothing Then trd = New Threading.Thread(AddressOf mylongprocess)

trd.Start()

End Sub

Private Sub mylongprocess()

' use mlv to access the listview

End Sub

End Class



Ken
 
J

JamesB

Ken Tucker said:
Hi,

Since you can pass a refernce to the listview when you start a
thread you need to create a class that will handle it for you. Try
something like this.

Thanks to both of you, the reference idea seems to work a treat. I'll now
look through the link Herfried gave to make sure it's reliable.
James.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top