469,272 Members | 1,448 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,272 developers. It's quick & easy.

Keep loop running whilst scrolling or moving the window

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.
Nov 21 '05 #1
5 1820
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" <wi*****@nospam-yahoo.com> wrote in message
news:dh**********@news.freedom2surf.net...
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.

Nov 21 '05 #2
Wicksy wrote:
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.

Nov 21 '05 #3
Hi Chris...
How would I pass parameters to that delegate function?
(this is where i start getting confused...)
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Wicksy wrote:
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.

Nov 21 '05 #4
Wicksy wrote:
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.

Nov 21 '05 #5
brilliant!
very clear - probably the best explanation i've seen yet!
thanks very much.
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Wicksy wrote:
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.

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Vikram Bhatia | last post: by
4 posts views Thread by David | last post: by
2 posts views Thread by James CC | last post: by
3 posts views Thread by Fred Flintstone | last post: by
2 posts views Thread by Jonathan | last post: by
7 posts views Thread by DaVinci | last post: by
4 posts views Thread by Keith Bentrup | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.