Solved

How to cancel the population of a CListView?

Posted on 2000-05-01
15
244 Views
Last Modified: 2013-11-20
I have an SDI application and the view class is derived from CListView.  I populate the list view with potentially a large number of records which could take a minute or so.  I want the user to be able to cancel the operation while the list view is populating.  

How can I do this?  Is there some sort of call back function?
0
Comment
Question by:wzhxxj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
15 Comments
 
LVL 3

Expert Comment

by:Shay050799
ID: 2766172
did u try to have a progress dialog, with a cancel button on it ?
0
 

Author Comment

by:wzhxxj
ID: 2766500
No, what I really want to do is to have a button on the toolbar that is enabled when the list box is populating.  Then the user has the option of pressing this cancel button and terminating the population of the list view.  Problem is that when the list view is populating, the application has not transferred control back to the application.

Would a progress bar help me with this?  If so, how?
0
 
LVL 3

Expert Comment

by:Shay050799
ID: 2766511
try multythreaded
make the feeling of the listview another thread
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:wzhxxj
ID: 2766743
Any suggestions on how I would do this?
0
 
LVL 3

Expert Comment

by:GlennDean
ID: 2766834
Spin off a worker thread via AfxBeginThread("Function to populate list",&paramsNeededByThread);

Then, in the paramsNeededByThread structure store the "this" pointer of the View class.  

You do have to be careful about some stuff like
1.  Make sure that the worker thread function, if defined as a member function in a class is defined as static so it doesn't receive the hidden this pointer.
   Glenn
0
 
LVL 3

Expert Comment

by:Shay050799
ID: 2766841
i still think the easiest way is just to create a progress dialog, and check for cancel...
you do not have to hassle with thread sync at all !!!
0
 
LVL 3

Expert Comment

by:GlennDean
ID: 2766868
You make a good point.  It does get messy, because one would have to also create an event or a variable so that the worker thread would have to check periodically to see if it should stop populating and exit.  

wzhxxi: What function are you populating the list?  It sounded like in a message handler, which is why pressing a button to cancel wouldn't work?
   Glenn

0
 

Author Comment

by:wzhxxj
ID: 2769073
Yes, I am populating the list in a message handler - i'm using the OnUpdate() event handler.  How would I do it with a progress dialog?? Are you guys referring to the progress bar??
0
 
LVL 3

Expert Comment

by:Shay050799
ID: 2769082
in onUpdate you are running through the list in a do while loop or a like ?
if so, create the CProgressCtrl and check the cancel button every time u get to the While loop again

while
{
   check for cancel
   ...
}
0
 

Author Comment

by:wzhxxj
ID: 2769121
In this case, I would have to create a modeless dialog box with a CProgressCtrl and a cancel button on it?  Is this what you are suggesting?

0
 
LVL 3

Accepted Solution

by:
Shay050799 earned 100 total points
ID: 2769152
well progressctrl does that for you
go to Project->Add To Project->Components and controls
and chose the visual studio controls

add the progress dialog into the project
it already has the functionality to check cancel and everything

make sure that u do the following

CProgressDlg progDlg(FF_IDS_PROGRESS_CAPTION); //the dialog
progDlg.Create(this);                  
progDlg.SetStatus("Processing...");      

while(...)
{
   if(progDlg.CheckCancelButton())
   {                              break;
   }            
   ... do ur stuff here ...
}

any question ? ask !
0
 

Author Comment

by:wzhxxj
ID: 2769178
Shay, thanks for all your help !!  
There are obviously lots of ways to do a particular thing - I try to find the simplest solution to the problem.

Thanks again.
0
 
LVL 3

Expert Comment

by:Shay050799
ID: 2769191
did u try it ?
does it work ?
if not feel free to ask more questions
it won't cost u a dime :-)

Shay
0
 

Author Comment

by:wzhxxj
ID: 2771161
Yeah, I tried it.  Works like a charm!
0
 
LVL 3

Expert Comment

by:Shay050799
ID: 2773926
well wzhxxj there is another way to do it without the progressbar.
i m sorry i didn't mentioned it earlier, i just went a cross the same problem, and since just like u i do not want the progress bar to show up  
i did the following:

while(1)
{
    m_MyList.AddString(somestring);
    somestring = readstringfromfile();
   
    MSG msg;
    while ( ::PeekMessage( &msg, NULL,  0, 0, PM_NOREMOVE ) )                if ( !AfxGetApp()->PumpMessage());      
}


this message loop will let u (the application) to proccess new message while it process ur function

try it out , it works for me

take care

Shay
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to retreive data from .TFS file exported by DVR 8 244
Fibonacci challenge 11 138
Updating statistics with error notification email in SQL server 4 120
Message not shown 5 87
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

730 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