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

First about my app

i have two listviewitem that have numbers value and i want the first item in the first listview sum with the first 5 number in the second listview (number by number)but between this operation i want this operation sleep 1 min and when it sleep , the second number in the first listview sum with the second 5 number in the
second listview (number by number) and ........etc

this is my code but without using threading and calculate only message box to check the code work correctly

C#
private void button3_Click(object sender, EventArgs e)
        {
 int inc = 0;

            for (int i = 0; i < listView1.Items.Count; i++)
            {

                for (int j = 0; j < 5; j++)
                {
                    
                    MessageBox.Show(listView1.Items[j].SubItems[0].Text + "  To  " + listView2.Items[inc].SubItems[0].Text);
                   
                    //i want to sleep the above line 1 min and containu the loop 
                    if (inc >= listView2.Items.Count - 1)
                    {
                        inc = 0;
                    }
                    else
                    {
                        inc++;
                    }


                }
Posted
Comments
Sergey Alexandrovich Kryukov 17-Oct-14 20:00pm    
What is your question? What should it do, how is that possibly related to threading and what's the problem?
—SA

1 solution

You don't want to use Sleep at all - that will block the main UI thread, so your application will become completely unresponsive. And you can't easily move your code to a secondary thread as you should only access UI components from the UI thread - or you will get a cross threading error.

So instead, set up a Timer, and use the Tick event to do one cycle round the loop - set the loop variables up yourself, and just "do" one loop in the Tick handler.
 
Share this answer
 
Comments
beljk 18-Oct-14 5:00am    
Can you give me example ...
OriginalGriff 18-Oct-14 5:19am    
Oh, come on!
You know how to set up a Timer, yes?

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