Quantcast
Viewing all articles
Browse latest Browse all 12583

add subitems to listview control with multiple threads

Hi

I wanted to add subitems to  listview control with multiple  background threads ( two threads)

for now but altogether will be ten equal to number of rows on the listview

public int ID{get;set;}

private void Form1_Load(object sender, EventArgs e) { this.JersyThread = new Thread(new ThreadStart(this.ThreadProcSafe)); this.YorkThread = new Thread(new ThreadStart(this.ThreadProcSafe)); this.JersyThread.Start(); this.YorkThread.Start(); }

                                                                  

 private void ThreadProcSafe()
        {
            // Wait two seconds to simulate some background work 
            // being done.
          //  Thread.Sleep(2000);


            string text = "Column"+ID;// "Written by the background thread.";
            // Check if this method is running on a different thread 
            // than the thread that created the control. 
           // lock (this.listView1) 
            if (this.listView1.InvokeRequired)
            {
                // It's on a different thread, so use Invoke.
                newJersyThread d = new newJersyThread(SetText);

                this.Invoke(d, new object[] { "sleeping" ,0 ,ID,"Nextrun"});
              ID++;

              newYorkThread d1 = new newYorkThread(SetText);
              this.Invoke(d1, new object[] { "sleeping",0,ID ,"Nextrun"});

            }
            else
            {
                // It's on the same thread, no need for Invoke 
                this.textBox1.Text = text + " (No Invoke)";
            }
           

        }

 private void SetText(string status, int services, int id, string nextrun)
        {
         
                listView1.Items[id].SubItems[4].Text = "Thread: "+id.ToString ()+" "+ status;
            listView1.Items[id].SubItems[5].Text = services.ToString() ;
            listView1.Items[id].SubItems[6].Text = id.ToString ();
            listView1.Items[id].SubItems[7].Text = DateTime .Now.ToString();
            ID++;
           
        }

out put looks like this

tignnj1	 2000	 1	 New York	 Thread: 0 sleeping	 0	 0	 6/3/2013 3:10:00 PM
tignnj3	 2000	 3	 New York	 	 	 	 
tignnj4	 2000	 4	 New York	 	 	 	 
tignnj5	 2000	 5	 New York	 Thread: 3 sleeping	 0	 3	 6/3/2013 3:10:00 PM
njdb1a	 2000	 101	 New York	 Thread: 4 sleeping	 0	 4	 6/3/2013 3:10:00 PM
tignmast 2005	 1	 Fairfield	 	 	 	 
tignmst2 2005	 2	 Fairfield	 	 	 	 
castle1a 2333	 1	 Coralville MDB	 	 	 	 
radgad1	 3000	 1	 GADLite	 	 	 	 
radgad2	 3000	 2	 GADLite	 	 	 	 

Note:

I have only 2 threads but there are three entries in listview 

the ID when incremented get the values 0,3,5 

I expected thread one (newJesrdyThread) to add its subitems row 1 and then increment ID from 0 to 1 and

the second thread (newYorkThread)  will pick up the ID=1 and add subitems to row 2

Is there anything I can do to make this code  such that thread one will add subitems to row 1

thread two will add subitems to row 2 and no third entry 



Viewing all articles
Browse latest Browse all 12583

Trending Articles