Click here to Skip to main content
15,891,744 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone....

i have two listViews,named listViewEven and listViewSquare, where listViewEven adds even number by looping and listViewSquare adds square of every items of listViewEven ,
means square of every items of listViewEven should be added to listViewSquare .

and i want to run two methods concurrently.

i have added two functions

C#
void even()
{
   for (int i = 10; i < 20; i++)
    {
        listViewEven.Items.Add(new ListViewItem(i.ToString()));
        listViewEven.Update();
    }
}
void square()
{
    foreach (ListViewItem list in listViewEven.Items)
    {
        string no = list.Text;
        int no1 = Convert.ToInt32(no) * Convert.ToInt32(no);
        listViewSquare.Items.Add(new ListViewItem(no1.ToString()));
        Thread.Sleep(100);
        listViewSquare.Update();
    }
}


but how can i implement those functions ?

Is any modification is needed ?

Please provide some solution ...
Posted

1 solution

You can't since square() depends on the result of even().
So you will have to merge them into one method with one loop. Inside that loop, do the 'even' stuff first and then the 'square' part.

[Edit]
C#
// Method names should start with a capital letter and begin with a verb
void ProcessEvenAndSquare()
{
   for (int i = 10; i < 20; i++)
    {
        listViewEven.Items.Add(new ListViewItem(i.ToString()));

        int square = i * i;
        listViewSquare.Items.Add(new ListViewItem(square.ToString()));
    }
}

[/Edit]
 
Share this answer
 
v2
Comments
krushna chandra jena 26-Nov-12 6:06am    
ok..i understand but in my problem one listview is depend upon another listview. that's why i have written that types of function .
please provide some solution ...
lukeer 26-Nov-12 7:40am    
I thought I had been clear about what to do. But I improved my solution with a sample in code.
If that's not what you need, please elaborate on your question.
krushna chandra jena 26-Nov-12 8:38am    
thanks for your suggestion but i want to use different loop . because i have two threads . by first thread one listview will add items(even no.) and by second thread another listview will add items(square of those even no.). and i want to run these two process parallely . What i have to do ?
lukeer 26-Nov-12 8:47am    
You can't.

That's what I told you in the first two words of this solution.
Maybe "can't" is a bit too harsh, but there is no practical use for that, since the second method depends on the first one. This leads to so much synchronisation overhead between the two potential threads that you're better off sinlge-threaded.

Adding to that, you're using the main thread (a.k.a. GUI thread) in between. That will slow things down even further.

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