Solved

How to cancel the population of a CListView?

Posted on 2000-05-01
15
241 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
  • 7
  • 6
  • 2
15 Comments
 
LVL 3

Expert Comment

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

Author Comment

by:wzhxxj
Comment Utility
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
Comment Utility
try multythreaded
make the feeling of the listview another thread
0
 

Author Comment

by:wzhxxj
Comment Utility
Any suggestions on how I would do this?
0
 
LVL 3

Expert Comment

by:GlennDean
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:wzhxxj
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yeah, I tried it.  Works like a charm!
0
 
LVL 3

Expert Comment

by:Shay050799
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now