Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Guys,

I am using listview items like mp3 player playlist, using for loop i select and move items one by one. but while its running my application other buttons not enable. i could n't do anything my application while running that forloop.When i stop the program of it will stops. How can I stop that for loop using button click in my application.

Regards
Vasanth
Posted
Updated 17-Aug-12 8:37am
v2
Comments
Matt T Heffron 17-Aug-12 14:37pm    
This is essentially a duplicate of http://www.codeproject.com/Answers/442410/List-view-Selection-one-by-one
[no name] 17-Aug-12 14:50pm    
This is acutally the 4th reposting I think.
[no name] 17-Aug-12 14:38pm    
99% of the trouble that you are having with your "application" is that you are using your event handlers to try and do things that you should not be doing. You are NOT going to click a button have some for loop in another button event handler stop running. You need to get a basic book on programming and work through it instead of asking this same "question" over and over.

You have to move the copy loop to its own thread.
The easiest way to do that is using the backgroundworker.
There is an example here on the code project :
BackgroundWorker Threads and Supporting Cancel[^]
 
Share this answer
 
Comments
Matt T Heffron 17-Aug-12 14:38pm    
This OP is essentially a duplicate of his http://www.codeproject.com/Answers/442410/List-view-Selection-one-by-one
vasanthkumarmk 17-Aug-12 14:51pm    
Thank you very much for that link.............
fjdiewornncalwe 28-Aug-12 9:17am    
+5. Definitely the correst solution.
There is an easiest way:

first define a bool field:
C#
private bool StopLoop = true;

then add this condition to your loop:
C#
for (int i = 0; i < 30; i++)
{
    if (!StopLoop)
        // Your actions
}

C#
private void Button_Click(object sender, EventArgs e)
{
    StopLoop = false;
}
 
Share this answer
 
Comments
vasanthkumarmk 18-Aug-12 1:05am    
When i start the for loop,i couldn't able to access other buttons.
fjdiewornncalwe 28-Aug-12 9:17am    
My vote of 1: Philip's solution 1 is the correct analysis of the situation. The problem is that the loop itself is locking down the UI because they run on the same thread.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900