Click here to Skip to main content
15,867,939 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hai to all,

I have done one task but it is not proper, just you guys fine tune my task.

I am using Windows Application with C# Languvage.

I have one Listview (Listview contain 5 Items), 2 Buttons (Play, Pause)

Aim : My goal is when I click the Play Button the listview top first item will select and the selection will moves one by one of some particular timing interval.

I have done: What I have done is, Listview item will select using forloop, it will moves one by one for particular timing interval using thread.sleep(3000) method , and the same time, From listview selection change event I have to select that particular item.

Error :
1. But when I start Play Button listview items will select and moves one by one, But I am using messagebox (See coding below) in listview selection change event, For that only it will select one by one, Without messagebox it won't select one by one.

(I need without using messagebox it will select one by one)

2. Secondly, When I Click the Pause Button the process won't stop. It is continuous running.

(I need to Stop the process when I click the pause Button)


My Coding:

------------------------------- Coding Starts --------------------------------------
// Play Button Click Event

C#
private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0, j = 0; i < listView1.Items.Count && j < listView1.Items.Count; j++)
            {
                if (j != 0)
                {
                    listView1.Items[j - 1].Selected = false;
                }

                listView1.Items[j].Selected = true;                            

                Thread.Sleep(3000);

                if (j == listView1.Items.Count - 1)
                {
                    listView1.Items[j].Selected = false;                    
                    j = -1;

                }
               
            }
        }


//List view Selection index Change coding

C#
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {              
                listView1.Select();
                MessageBox.Show(listView1.SelectedIndices[0].ToString());
            }
        }



//Pause Button Click Event

C#
private void button2_Click(object sender, EventArgs e)
        {
             //I Don't Know what coding write to Stop that process.

        }


--------------------------- Coding Finished ---------------------------

I Hope Every one Understand what I am asking, If any one not able to understood i am ready to explain once again. But I need the Solution.

Regards,
Vasanth
Posted
Updated 13-Mar-21 9:57am
Comments
[no name] 17-Aug-12 8:31am    
Stop what process? You do not have a process to stop. Your "play" is probably working like you want but you are artificially introducing a delay in there that allows you to see what is going on.

The first problem I see is that the loop through the listView1 items (in button1_Click) is happening in the UI thread! This is probably why you don't see the listView1 selection updating without the MessageBox. (Showing the MessageBox allows the UI thread to process the selection change visual changes.)

What I'd suggest is that button1_Click start a background/worker thread that changes the listView1 selections at the appropriate intervals. (I'm guessing from the "Play Button Click Event" comment, that you will be playing an audio or video file based on each item in the list, not simply sleeping for awhile.)
You cannot directly update the listView1 selection across threads, so you will need to use listView1.Invoke().
Inside the loop, at each iteration check to see if it is "paused", and exit the loop.

The thread should be held in a class field (not just local to button1_Click method) so it exists after the method exits.
The Pause button Click handler can just set the "paused" class field.

(By the way, your for loop declares and checks the variable i, but never uses it anywhere else!)
 
Share this answer
 
Comments
vasanthkumarmk 17-Aug-12 13:22pm    
can you post the codings please?
vasanthkumarmk 17-Aug-12 13:29pm    
Make alteration of my code, and implement your ideas please put the coding here. I try out but it shows some errors. I know its my probs. If you give example coding i have understand. So I request give the coding what you said above solution.
Matt T Heffron 17-Aug-12 14:32pm    
You said you tried... What did you try?
Post your code as a new solution.
Instead of me re-implementing the whole thing, I may be able to help you with your implementation. And you'll learn more doing it yourself.
Sergey Alexandrovich Kryukov 17-Aug-12 17:54pm    
That is correct, my 5.
--SA
you can add a tracking flag:

1. Add a global flag (Boolean) to your code behind. This will default to "false".
2. In your "Play" event handler, before the for-loop, set the tracking flag to "true".
3. In the for-loop of the "Play" event handler, add a condition to only do the processing if the tracking flag is "true". If it is "False", then "break" out of the for-loop.
4. In your "Pause" event handler, set this flag to "false".

This will give you start-stop functionality that you are asking for.

The problem is, this doesn't "Pause" it.."Stops". If you want "Pause" functionality, you will need to keep track of the currently selected item and then go to that selected item when the user clicks "Play" again.
 
Share this answer
 
Comments
vasanthkumarmk 17-Aug-12 13:21pm    
k i can try this one.
vasanthkumarmk 17-Aug-12 13:33pm    
I hope it will working, But i couldn't understand your explanation. I suggest you post the example coding what you said the above solution.
You are doing it in the UI thread, which will freeze de window / form until the work is finnished. this design wont work.

it is very challanging to create a multithreaded application, that is why i suggest to choose a different aprouch that does not require long running code. make your code finnish within miliseconds.

another alternative is the Timer Control. Add it from the ToolBox onto your form, and you can set it to a certain interval time in the properties panel.

and you can start and stop it like so:

Timer1.start();

Timer1.stop();

and you need to catch the Tick Event to run your code. just like how a Button has a Click event.

in the Tick event. select the next item. dont use loops, the timer control repeats itself.
 
Share this answer
 
v2

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