Solved

C# listbox show added items for 5 seconds and leave app

Posted on 2014-03-13
2
340 Views
Last Modified: 2014-03-13
Hello experts,

I have a simple application where i send some data to a webserver by post. And while i do it i'm adding items to a listbox, just to inform the steps that are being done.
And after all finished the form with the listbox should show the information for 5 seconds and exit application.

My problem is, all the steps are working great with no problems, but while the steps are being done and i'm adding items to the listbox, the listbox doesn't show nothing, just blank. And in the end after all steps are done, wait's the 5 seconds with the listbox continuously  blank  and finally closes...

The code is inside the Form.Shown event, something like this:
private void Form1_Shown(object sender, EventArgs e)
{
    try
    {
        string[] lines = System.IO.File.ReadAllLines(@"C:\tmp\printers.txt");

        foreach (string line in lines)
        {
            responsefromserverRACC = "";
            while (responsefromserverRACC == "")
            {
                listBoxReport.Items.Add("Initiating: " + line);
                // code code
                }
            }
        }


        webClient.Dispose();
        webClientRACC.Dispose();

        System.Threading.Thread.Sleep(10000);
        System.Windows.Forms.Application.Exit();
    }
    catch (Exception em)
    {
        Console.WriteLine("error:");
        MessageBox.Show(em.Message);
    }
}

Open in new window


Should i do this different? In another event?
Or i just can't do it? :)

Thx in advanced,
Michael
0
Comment
Question by:justaphase
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39926716
The quick solution would be to simply call Refresh() on the listbox after adding each item:
...

listBoxReport.Items.Add("Initiating: " + line);
listBoxReport.Refresh();

...

Open in new window

0
 
LVL 1

Author Closing Comment

by:justaphase
ID: 39926772
Oh for god sake... that simple..
Thx, it worked
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question