Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Threading Application .NET4 , +
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
 
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 26-Nov-12 0:26am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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]
// 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]
  Permalink  
v2
Comments
krushna chandra jena at 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 at 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 at 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 at 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)

  Print Answers RSS
0 Maciej Los 310
1 OriginalGriff 285
2 Afzaal Ahmad Zeeshan 200
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 180
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100