Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

How to cancel the population of a CListView?

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
wzhxxj
Asked:
wzhxxj
  • 7
  • 6
  • 2
1 Solution
 
Shay050799Commented:
did u try to have a progress dialog, with a cancel button on it ?
0
 
wzhxxjAuthor Commented:
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
 
Shay050799Commented:
try multythreaded
make the feeling of the listview another thread
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
wzhxxjAuthor Commented:
Any suggestions on how I would do this?
0
 
GlennDeanCommented:
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
 
Shay050799Commented:
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
 
GlennDeanCommented:
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
 
wzhxxjAuthor Commented:
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
 
Shay050799Commented:
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
 
wzhxxjAuthor Commented:
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
 
Shay050799Commented:
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
 
wzhxxjAuthor Commented:
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
 
Shay050799Commented:
did u try it ?
does it work ?
if not feel free to ask more questions
it won't cost u a dime :-)

Shay
0
 
wzhxxjAuthor Commented:
Yeah, I tried it.  Works like a charm!
0
 
Shay050799Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now