Background
I am working on an application which use listView, and I have write some functions for make actions with items in lisyView. I have actions as OnClickCopyAllDomains and OnClickMarkAllDomains. This functions works with domains (listView items) and when listView contain more then 100.000 item I feel how working speed goes down. So below I will show you functions, and I want you to give me advance how I can improve speed of functions?
OnClickMarkAllDomains
This function set listView items checked state to true.
System::Void MainForm::OnClickMarkAllDomains(System::Object^ sender, System::EventArgs^ e) {
for( int i=0; i<listViewDomainsInfo->Items->Count; ++i )
listViewDomainsInfo->Items[i]->Checked = true;
}
OnClickCopyAllDomains
In this function I copy listView items (domains) to the clipboard, I if domains count is more then 100.000 I wait for 5 minutes till action ends.
System::Void MainForm::OnClickCopyAllDomains(System::Object^ sender, System::EventArgs^ e) {
int itemsCount = listViewDomainsInfo->Items->Count;
System::String^ outputClipboard = System::String::Empty;
for( int i=0; i<itemsCount; ++i ) {
outputClipboard += listViewDomainsInfo->Items[i]->Text + "\r\n";
}
if( !System::String::IsNullOrEmpty( outputClipboard ) ) {
try {
System::Windows::Forms::Clipboard::SetText( outputClipboard );
}
catch( ... ) { }
}
}
Question
How I can make my functions better, increase their speed ?
Call
listViewDomainsInfo->BeginUpdate()before your loops andlistViewDomainsInfo->EndUpdate()after.Also, string concatenation as you’re using it is extremely inefficient. Make
outputClipboarda System::Text::StringBuilder rather than a System::String.