Keep loop running whilst scrolling or moving the window

  • Thread starter Thread starter Wicksy
  • Start date Start date
W

Wicksy

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, 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. I have already tried putting DoEvents() and Thread.Sleep()
commands into the loop.

Many thanks in advance,
Wicksy.
 
PS. 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?
 
Wicksy said:
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. I have already tried putting DoEvents() and Thread.Sleep()
commands into the loop.

But you can! You just need to make sure you marshal the call to update
the UI to the UI thread.

The simplest way is to create a sub that updates the control and then
use a MethodInvoker delegate in conjunction with the ListView's Invoke
method to call it:

'ListViewUpdate contains code to update the list view.
Dim ListViewUpdaterDelegate As New MethodInvoker(AddressOf
ListViewUpdate)

Then call the Invoke method of the ListView and pass the delegate in:
MyListView.Invoke(ListViewUpdaterDelegate)

This may not be the best method to use, depending on your needs, but
look up the Control.Invoke and MethodInvoker in the docs.
 
Hi Chris...
How would I pass parameters to that delegate function?
(this is where i start getting confused...)
 
Wicksy said:
Hi Chris...
How would I pass parameters to that delegate function?
(this is where i start getting confused...)

Using that method, you can't. You would have to create a wrapper class
with properties that also has the delegate. Then you set the
properties of the class. Perhaps something like this (untested):

Public Class ListViewChanger

Private _ListViewToChange As ListView
Private _SomeString As String
Private _SomeInteger As Integer

'The control we want to change is passed into the class
'In this case it is a listview
Public Sub New(ByVal lvtochange As ListView)
_ListViewToChange = lvtochange
End Sub

'This is the method that you call that will use a delegate to
upadate the
'ListView.
Public Sub ChangeIt(ByVal s As String, ByVal i As Integer)
'These variables hold the data that you need passed in.
_SomeString = s
_SomeInteger = i

Dim UpdateDelegate As New MethodInvoker(AddressOf DoTheWork)

_ListViewToChange.Invoke(UpdateDelegate)
End Sub

Private Sub DoTheWork()
'Here you would do whatever to update the ListView. You would
then
'access any properties that were passed in.
End Sub
End Class

You would create an instance of this class and pass your ListView into
the constructor:

Dim Changer As New ListViewChanger(MyListView)
Changer.ChangeIt("Hello", 123)

Hope this gives you some ideas.
 
brilliant!
very clear - probably the best explanation i've seen yet!
thanks very much.
 
Back
Top