1

i'm trying to update/paint a listbox in real time, but i'm having some problems. I got a button to start the process of populating the listbox button_traceroute_Click.

My problem is that the listbox is only painted/updated when the whole process (button click) has ended, i wanted the the items to be inserted(viewed) one by one. I already tried using ListBox.Update() but nothing happened. (this is a traceroute)

private void button_traceroute_Click(object sender, EventArgs e)
        {
            String target;
            int i = 0;
            target = textBox_target.Text;
            Mydata data = new Mydata();
            TraceRoute traceroute = new TraceRoute();
            while (i < 50 && !data.getReached() && !data.getError()) // i = hop count
            {
                data = traceroute.startTrace(target, data, i);               
                listBox_trace.Items.Add(data.getOutput());
                i++;
            }
        }

data.getOutput() returns (string) something like: "Hop X: 165.468.354.4 -> 50 ms" or "Hop X: Timeout"

Mydata{
 bool Finish flag;
 bool Error flag;
 int badcounter;
 String output;  
}

For now im populating the listbox with Strings, but the objective is to use a object.

3 Answers 3

4

You need to put the long running operation on it's own thread. then report the progress back to the UI intermittently.

You can see an example how to do this in another post of mine here.

Sign up to request clarification or add additional context in comments.

Comments

0

Also, you can use the BeginUpdate and EndUpdate method to speed up the repainting of the listbox. When BeginUpdate is called, any pending paint to the listbox is suspended, likewise, EndUpdate resumes the painting, this can help making your listbox look as if it is fast in loading the data into it and minimizes the number of painting to it when adding the data.

Hope this helps, Best regards, Tom.

Comments

0

Try this:

data = traceroute.startTrace(target, data, i);                                   
listBox_trace.Items.Add(data.getOutput());                
Application.DoEvents();
i++;

Its not ideal - Michael G's answer is best, but this could work as a quick fix.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.