CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: ListBox problem

  1. #1
    Join Date
    Jul 2005
    Location
    Bremen, Germany
    Posts
    33

    Question ListBox problem

    Hello friends,
    I create ListBox using MFC, in which I want to display the Output (like Visual c++ IDE debug windowor Output window).
    I create a button after that it starts processing and ready to display the output.
    I used
    m_ListBoxResult.AddString(strlineCount); like that for all the functions and so on.. there will be lot of output.

    Everything works fine but It displays everything after all the process done.
    while processing nothing is displayed.

    Its not working in runtime. Hope I perfectly project my question.

    If anybody needs the code , I will attach them

    forexample:

    CFinderView::Onbuttonclick()
    {

    PrintSystemUsage();

    ReadFiles();
    }
    void PrintSystemUsage()
    {
    m_ListBoxResult.AddString("Crawling the Tree....");
    strlineCount ="something";
    m_ListBoxResult.AddString(strlineCount);

    }

    and in each function i have hundreds of output. I used only AddString().. with UpdateData(false).
    But the List Box displays only after finishing Read Files.

    Looking forward to your reply.

    thnx in advance .

    mfg,
    kingsly
    kings4u

  2. #2
    Join Date
    Mar 2006
    Posts
    15

    Re: ListBox problem

    i think you could sendmessage with paramter "LB_INSERTSTRING",
    and count index correctly

  3. #3
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: ListBox problem

    Adding a string into the listbox means a message is sent to the listbox. To see the change the window holding the listbox needs to refresh itself and allow the listbox to to the same.

    But with this algorithm you are using the application's main thread ( the UI thread) is blocked in the Onbuttonclick function so no message gets processed until the function finishes.

    You have 2 options:
    1. the nice way: use a thread to do the file processing and from that thread sent messages to the UI thread to add strings into the listbox
    2. the old way: process the window messages yourself. See this article for more details
    Har Har

  4. #4
    Join Date
    Jul 2005
    Location
    Bremen, Germany
    Posts
    33

    Re: ListBox problem

    thnx PadexArt and Junma for your replies.

    PadexArt , I will try out ur nice way hint.

    I am gonna do thread for the first time and its really interesting .

    Get back to you, if i have a serious problem.

    once again thanks

    mfg,
    kingsly
    kings4u

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: ListBox problem

    Quote Originally Posted by kingslee
    ......
    and in each function i have hundreds of output. I used only AddString().. with UpdateData(false).
    But the List Box displays only after finishing Read Files.
    1. The UpdateData(false) call is not needed here, moreover, it only slows the apllication performance down - remove this line.
    2. The problem List Box does not display until finishing Read Files is because the CPU is busy reading the files - there is no time to update the GUI. To solve this problem you could create a worker thread and move all "Read Files" operations to it, then PostMessage a user defined message from a worker thread to the View window with some data to update the GUI. Tke a look at this article: WorkerThreads

    3. to junma: sendmessage with paramter "LB_INSERTSTRING" is the same as callinf CListBox::InsertString - the same as CListBox::AddString (minus sorting, plus setting the position in list box).

    PS. PadexArt was about 20 minutes faster!
    Last edited by VictorN; March 31st, 2006 at 11:47 AM.

  6. #6
    Join Date
    Jul 2005
    Location
    Bremen, Germany
    Posts
    33

    Re: ListBox problem

    Hallo PadexArt,
    I use seperate thread for all my function. Inside these functions, I need to use this Listbox control right. But these member variables are not declared according to the thread and its declared in Form View class.cpp

    I include the respective header files to it also.

    What is the problem?

    pls clarify me.

    waiting for your reply

    thnx

    kingsly
    kings4u

  7. #7
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: ListBox problem

    POst the code you are using. Even better would be to zip and upload your project.
    Har Har

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: ListBox problem

    Quote Originally Posted by kingslee
    Hallo PadexArt,
    I use seperate thread for all my function. Inside these functions, I need to use this Listbox control right. But these member variables are not declared according to the thread and its declared in Form View class.cpp
    ...
    Again, check it out: WorkerThreads

  9. #9
    Join Date
    Jul 2005
    Location
    Bremen, Germany
    Posts
    33

    Re: ListBox problem

    Hi,
    I am attaching one I did before and also one with thread.

    waitn for your reply

    kings
    Attached Files Attached Files
    kings4u

  10. #10
    Join Date
    Jul 2005
    Location
    Bremen, Germany
    Posts
    33

    Re: ListBox problem

    Thanks VictorN for your reply ..
    I just going through the article ..
    I used the UI thread in my attached file ..
    .. I didnt finish the article .. yet..
    get back to you guys .. soon ..
    thnx
    kings
    kings4u

  11. #11
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: ListBox problem

    Another, perhaps simpler solution would be to change your code as shown here in bold:
    Code:
    void PrintSystemUsage()
    {
    m_ListBoxResult.AddString("Crawling the Tree....");
    strlineCount ="something";
    m_ListBoxResult.AddString(strlineCount); 
    m_ListBoxReslult.UpdateWindow(); 
    }
    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  12. #12
    Join Date
    Jul 2005
    Location
    Bremen, Germany
    Posts
    33

    Re: ListBox problem

    Thanks Krmed+..
    It's worked ..

    Such a simple solution.

    Anyway I really wanna know about UI or Worker Thread implementation.

    Looking forward to get reply from PAdexArt and VictorN.

    As I read the article , its pretty tricky while dealing with mfc controls like these.

    Once again thankyou so much Krmed+

    mfg,
    kingsly
    kings4u

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured