Help with scolling a listbox while data is being added

J

Jason

Hello

I've got a situatuion were I'm adding data to a list box, maybe 50,000 items
(strings). While I add this data I would like to be able to scroll up/down
to view the data. I've got a small example which is similar to what I'm
doing. I need help trying to figure out how to scroll while data is being
added. In this example I have a for loop that passes a string to my list
box up to 5000000 times in an attempt to simulate my real life program.
Alll works expect the scroll bar on my list box won't let me scroll until
the for loop has completed.

Any idea how I can fix this?


sing System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Threading;



namespace MyListBoxthread

{

public partial class Form1 : Form

{

public delegate void AddListItem(String myString);

public AddListItem myDelegate;

public Form1()

{

InitializeComponent();

myDelegate = new AddListItem(AddListItemMethod);

}

private void Form1_Load(object sender, EventArgs e)

{

}

public void AddListItemMethod(string mystring)

{

myListBox.Items.Add(mystring);

//myListBox.Update();

}

private void button1_Click(object sender, EventArgs e)

{

Thread myThread = new Thread(new ThreadStart(ThreadFunction));

myThread.Start();

}

private void ThreadFunction()

{

MyThreadClass MyThreadObject = new MyThreadClass(this);

MyThreadObject.run();

}

}

public class MyThreadClass

{

Form1 myform;

public MyThreadClass(Form1 form)

{

myform = form;

}

string myString;

public void run()

{

for (int i = 1; i < 500000; i++)

{

myString = "Step number " + i.ToString() + "
excuted";

myform.Invoke(myform.myDelegate, new object[] {
myString });

}

MessageBox.Show("Done");

}

}

}
 
S

SyntaxError

If a new thread is not required, lose it. You can try adding an
Application.DoEvents() statement after the listbox Add.
 
J

Jeff Johnson

I've got a situatuion were I'm adding data to a list box, maybe 50,000
items (strings).

Forgetting the scrolling issue, I say the REAL problem is trying to add that
many items to a list box. Do you really want to scroll through all that? I
highly recommend you reconsider your design.
 
J

Jason

The Application.DoEvents did the trick

In that example I used that amount of data was used to help show the issue,
in my application, the data added to the listbox isn't that much but however
the data will scroll off the viewable screen.

In my mind, if you have a windows form and a listbox, a user should be able
to scroll at anytime.

If there are better ways, please let me know so for future projects I can
implement them

Thanks
 
P

Peter Duniho

I've got a situatuion were I'm adding data to a list box, maybe 50,000
items
(strings). While I add this data I would like to be able to scroll
up/down
to view the data. I've got a small example which is similar to what I'm
doing. I need help trying to figure out how to scroll while data is
being
added. In this example I have a for loop that passes a string to my list
box up to 5000000 times in an attempt to simulate my real life program.
Alll works expect the scroll bar on my list box won't let me scroll until
the for loop has completed.

Any idea how I can fix this?

The code you posted is a little awkward, but ought to work fine for the
most part. Probably you're just running into saturation of the message
queue as well as suffering a huge penalty due to thread context switching,
due to the large number of calls to Control.Invoke() during the loop.
This all prevents the mouse input messages related to dragging the scroll
bar from having any signficant effect. That is, you probably _can_
actually scroll a little, but the code adding items to the ListBox
dominates the message processing and so it seems like nothing works.

Given the large number of items involved (half a million! that's a lot for
a lowly ListBox), you may find it works better to batch up items, adding
(for example) 1000 at once so that you're calling Control.Invoke() fewer
times. Also, call the ListBox.BeginUpdate() before actually adding those
1000 items, and then EndUpdate() after, to avoid unnecessary redrawing
while the additions happen.

For example:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
new Thread(ThreadFunction).Start();
}

private void ThreadFunction()
{
List<string> strings = new List<string>(1000);

for (int i = 1; i < 500000; i++)
{
strings.Add("Step number " + i.ToString() + "executed");

if (strings.Count == 1000)
{
Invoke((MethodInvoker)delegate
{
myListBox.BeginUpdate();
foreach (string str in strings)
{
myListBox.Items.Add(str);
}
myListBox.EndUpdate();
}

strings.Clear();
}
}

MessageBox.Show("Done");
}
}

Whatever you do, don't waste time with Application.DoEvents(). All it's
doing is adding even more overhead and temporarily hiding the more
fundamental problem.

Pete
 
P

Peter Duniho

The Application.DoEvents did the trick

No, not really. It just fooled you into thinking you'd fixed the
problem. Your program will run much better if you fix it more cleanly.
In that example I used that amount of data was used to help show the
issue,
in my application, the data added to the listbox isn't that much but
however
the data will scroll off the viewable screen.

Well, any amount of data large enough for the user to have time to scroll
while it's being added is really a very large amount for a ListBox.
Still, you know the user scenario better than anyone; just keep in mind
the realities of trying to present that much information to a user at once.
In my mind, if you have a windows form and a listbox, a user should be
able
to scroll at anytime.

I do agree with that. I don't think anyone's suggesting they shouldn't.
It's just good to make sure that you do that correctly, as well as that
you don't present the user with difficult-to-navigate user interface
design.
If there are better ways, please let me know so for future projects I can
implement them

Please see my other reply. Also, I notice from another message you've
posted that you've decided to switch to the ListView class. Note that
ListView has a "virtual" mode where the entire control doesn't even need
to be populated at once. This is probably a much better approach to your
situation than trying to maintain enough ListView items for your entire
collection.

One thing I should point out is that having thumbnails indexed by string
rather than int is mutually exclusive with virtual mode for ListView. For
some reason, you can only use one or the other. See previous threads in
this newsgroup for more detailed discussion of that, as well as an example
work-around for the issue.

Pete
 
J

Jason

Thanks for the info

I spend most of my time doing embedded C++ code, but I will be getting into
some real involved windows app that are C#. I know enough to understand
that Windows programming is vastly different then what I'm use to. To be a
good Windows programmer, I believe you need to have a good understanding of
the framework and Windows events and threads of which I do not. Can you
recommend a good book that will help me gain a better understanding of
Windows Programming?

Thanks
 
P

Peter Duniho

Thanks for the info

I spend most of my time doing embedded C++ code, but I will be getting
into
some real involved windows app that are C#. I know enough to understand
that Windows programming is vastly different then what I'm use to. To
be a
good Windows programmer, I believe you need to have a good understanding
of
the framework and Windows events and threads of which I do not. Can you
recommend a good book that will help me gain a better understanding of
Windows Programming?

I agree that it helps a lot to already have experience with how the
unmanaged Windows API works, especially with respect to window messaging
(events) and threading. Unfortunately, I don't have first-hand knowledge
of a good text for learning those topics.

Many people enjoy Charles Petzold's books, and they have been the de facto
standard guides for Windows programming over the years. But I don't know
if he's got a basic .NET book, and my own impressions of what I have read
of his books have been varied. Some are better than others. :)

There are other .NET introductory books...you might look at the ratings on
Amazon. It's not a perfect way to evaluate a book, but there will be
reviews there that help understand why the reviewer gave the rating they
did.

Pete
 

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