Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Thread with ListView

Thread with ListView

Scheduled Pinned Locked Moved C#
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mm310
    wrote on last edited by
    #1

    Hi All How I can Use Thread with ListView To Add this Items ListViewItem lvi = new ListViewItem(new string[] { splitString[1], splitString[2].Substring(0, 12), splitString[2].Substring(17, 12), tempstring }); this.listView1.Items.Add(lvi);

    C 1 Reply Last reply
    0
    • M mm310

      Hi All How I can Use Thread with ListView To Add this Items ListViewItem lvi = new ListViewItem(new string[] { splitString[1], splitString[2].Substring(0, 12), splitString[2].Substring(17, 12), tempstring }); this.listView1.Items.Add(lvi);

      C Offline
      C Offline
      coolestCoder
      wrote on last edited by
      #2

      Hi, You can create new object of Thread class and assign a function which will perform the above said operation for you. Remember that the function should not return any thing (ie return type = void ) and should not accept any arguments. In case if you want to pass some arguments when the thread will actually start then you can use fields or properties which would be accessible from inside the function. For example -- Thread objThread = new Thread(new ThreadStart(this.FunctionName)); objThread.Start(); the FunctionName would be your function defined as below-- public void FunctionName() { ListViewItem lvi = new ListViewItem(new string[] { splitString[1], splitString[2].Substring(0, 12), splitString[2].Substring(17, 12), tempstring }); this.listView1.Items.Add(lvi); } If you want now to pass any arguments to FunctionName then use fields or properties. Means before u call objThread.Start() assign the right fields with the values you want to pass to the Thread. Hope i am clear !


      "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


      Anant Y. Kulkarni

      M 1 Reply Last reply
      0
      • C coolestCoder

        Hi, You can create new object of Thread class and assign a function which will perform the above said operation for you. Remember that the function should not return any thing (ie return type = void ) and should not accept any arguments. In case if you want to pass some arguments when the thread will actually start then you can use fields or properties which would be accessible from inside the function. For example -- Thread objThread = new Thread(new ThreadStart(this.FunctionName)); objThread.Start(); the FunctionName would be your function defined as below-- public void FunctionName() { ListViewItem lvi = new ListViewItem(new string[] { splitString[1], splitString[2].Substring(0, 12), splitString[2].Substring(17, 12), tempstring }); this.listView1.Items.Add(lvi); } If you want now to pass any arguments to FunctionName then use fields or properties. Means before u call objThread.Start() assign the right fields with the values you want to pass to the Thread. Hope i am clear !


        "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


        Anant Y. Kulkarni

        M Offline
        M Offline
        mm310
        wrote on last edited by
        #3

        thanks Anant Y. Kulkarni I am use this code private void loadSubToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog1.Filter= "Srt File|*.srt"; OpenFileDialog1.Title = "Open SubTitle File"; try { if (OpenFileDialog1.ShowDialog() == DialogResult.OK) { Thread ReadThread = new Thread(new ThreadStart(ReadData)); ReadThread.Start(); } } catch (Exception) { MessageBox.Show("Can't Read this File"); } } and this Function private void ReadData() { listView1.Items.Clear(); StreamReader s = new StreamReader(OpenFileDialog1.FileName, Encoding.Default); ArrayList a = new ArrayList(); while (s.Peek() >= 0) { string alltext = ""; string text = ""; while ((text = s.ReadLine()) != "" && text != null) { alltext += ("|" + text); } a.Add(alltext); } s.Close(); foreach (string text in a) { string[] splitString = text.Split('|'); string tempstring = ""; for (int i = 3; i < splitString.Length; i++) { tempstring += splitString[i]; } ListViewItem lvi = new ListViewItem(new string[] { splitString[1], splitString[2].Substring(0, 12), splitString[2].Substring(17, 12), tempstring }); this.listView1.Items.Add(lvi); } a.Clear(); } when i Run this Program this message Dispaly Exception System.InvalidOperationException was thrown in debuggee: Cross-thread operation not valid: Control 'listView1' accessed from a thread other than the thread it was created on

        C 1 Reply Last reply
        0
        • M mm310

          thanks Anant Y. Kulkarni I am use this code private void loadSubToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog1.Filter= "Srt File|*.srt"; OpenFileDialog1.Title = "Open SubTitle File"; try { if (OpenFileDialog1.ShowDialog() == DialogResult.OK) { Thread ReadThread = new Thread(new ThreadStart(ReadData)); ReadThread.Start(); } } catch (Exception) { MessageBox.Show("Can't Read this File"); } } and this Function private void ReadData() { listView1.Items.Clear(); StreamReader s = new StreamReader(OpenFileDialog1.FileName, Encoding.Default); ArrayList a = new ArrayList(); while (s.Peek() >= 0) { string alltext = ""; string text = ""; while ((text = s.ReadLine()) != "" && text != null) { alltext += ("|" + text); } a.Add(alltext); } s.Close(); foreach (string text in a) { string[] splitString = text.Split('|'); string tempstring = ""; for (int i = 3; i < splitString.Length; i++) { tempstring += splitString[i]; } ListViewItem lvi = new ListViewItem(new string[] { splitString[1], splitString[2].Substring(0, 12), splitString[2].Substring(17, 12), tempstring }); this.listView1.Items.Add(lvi); } a.Clear(); } when i Run this Program this message Dispaly Exception System.InvalidOperationException was thrown in debuggee: Cross-thread operation not valid: Control 'listView1' accessed from a thread other than the thread it was created on

          C Offline
          C Offline
          coolestCoder
          wrote on last edited by
          #4

          Hi, Try using the BackgroundWorker component. here is a msdn article http://msdn2.microsoft.com/en-us/library/c8dcext2.aspx


          "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


          Anant Y. Kulkarni

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups