Thread with ListView
-
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);
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
-
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
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 Functionprivate 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 -
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 Functionprivate 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 onHi, 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