Keep loop running while moving window or scrolling

wicksee

New member
Joined
Oct 1, 2005
Messages
1
Location
Plymouth, UK
Programming Experience
3-5
Hi all.

I have a VB.NET app with a loop running that is continually managing a number of threads AND updating a ListView object depending on the results of the worker threads.

The problem I have is that if I try to move (drag) the form's window around the screen, or to scroll the ListView control, all code (notably, the loop) pauses until I stop dragging or scrolling. I want the loop to continue whilst I'm doing trivial things like moving the window around the screen.

Is there a way to do this?

I cannot move the loop to another thread, as I need the loop to update the ListView, and it must therefore run in the same thread that created the ListView object (ie, the form's main thread). I have already tried putting DoEvents() and Thread.Sleep() commands into the loop, but as you'd expect, this didn't help.

Many thanks in advance.

[EDIT] I think what I'm asking is is there a way to stop the form (or the ListView) from blocking whilst it is being dragged/resized/whatever?[/EDIT]
 
Last edited:
The loop does not have to run in the same thread as the listview in order to update it. You can call the listview's Invoke method from another thread and pass a delegate and some parameters. That is the thread-safe way of interacting with a control on another thread.

DelegateSub UpdateListViewDelegate(ByVal s AsString)

PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
Dim t As Threading.Thread = New Threading.Thread(AddressOf DoTheLoop)
EndSub

PrivateSub DoTheLoop()
Do
'Do stuff
ListView1.Invoke(New UpdateListViewDelegate(AddressOf UpdateListView), _
NewObject() {"New item to add"})
Loop
EndSub

PrivateSub UpdateListView(ByVal s AsString)
ListView1.Items.Add(s)
EndSub
 
Back
Top