Threading + ListView

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1234 Views - Last Post: 12 January 2012 - 12:19 PM Rate Topic: -----

#1 Devil589  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-January 12

Threading + ListView

Posted 11 January 2012 - 12:16 PM

Hi, i have a problem on threading which is based on my ListView.

What do i want to do:

I have a thread, inside this thread ill go through a listview with a foreach loop.
And inside that foreach loop i want to edit / update the GUI.

My problem is, i dont know how i can access the items inside the ListView from my thread.
Because ill get all the time the error message that i cannot access the ListView from my thread.

I hope someone can help me, which would be great.
And sorry for my bad english. :(

Here's some of my code which i tried:

private void Button1_Click(object sender, EventArgs e)
{
     Thread t = new Thread(new ThreadStart(DoIt));
     t.Start();
}

private void DoIt()
{
     foreach (ListViewItem item in ListView1.Items)
     {
          Label1.Text = item.SubItems[1].Text;
          Thread.Sleep(500);
     }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Threading + ListView

#2 Devil589  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-January 12

Re: Threading + ListView

Posted 11 January 2012 - 12:22 PM

Sorry for this double post, but i havent found the Edit button (or im blind o.O)

I tried this in the DoIt function too:

private void DoIt()
{
    foreach (ListViewItem item in ListView1.Items)
    {
        MethodInvoker guiUpdate = delegate
        {
            Label1.Text = item.SubItems[1].Text;
        }

        Invoke(guiUpdate);
        Thread.Sleep(500);
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Threading + ListView

Posted 11 January 2012 - 12:24 PM

Simple answer you shouldn't be.

Why does it need to access them? You can do what you trying do with a timer, and it tick event.

Threads should be as independent (and GUI agnostic) as possible, raising events when need to communicate.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Threading + ListView

Posted 11 January 2012 - 12:54 PM

See FAQ: 12

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2



FAQ (Frequently Asked Questions - Updated Jan 2012

Spoiler

Was This Post Helpful? 0
  • +
  • -

#5 Devil589  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-January 12

Re: Threading + ListView

Posted 11 January 2012 - 01:49 PM

Thanks for your replies.

Well why it needs the access, i want to get the content from the ListView in the thread to work with the items from that ListView.

Thats why i have a foreach loop in that thread.
But as i said, i have the problem that i cant access the ListView in that foreach loop.

How can i do that?
Or whats the best solution to handle that problem?

Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Threading + ListView

Posted 11 January 2012 - 02:26 PM

The problem is that's just bad architecture.

Instead of the thread trying to pull from the GUI - BAD...
Consider having the GUI thread create the the worker thread and push the listView content to the thread as a parameter.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Threading + ListView

Posted 11 January 2012 - 02:31 PM

Devil589: Not how, but Why? Why does in need to access them? What is going to do with them.

Think pipeline.

my collection of stuff -> The thing I'm "conceptually" going to do with them -> transformed stuff

eg.

List of URLs -> Parse of Picture urls -> Download Pictures -> Picture collection.

This post has been edited by AdamSpeight2008: 11 January 2012 - 02:33 PM

Was This Post Helpful? 0
  • +
  • -

#8 Devil589  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-January 12

Re: Threading + ListView

Posted 11 January 2012 - 03:02 PM

Thanks tlhIn`toq and AdamSpeight2008, now i understand what you guys mean.

But can someone show me a example or something on how to start a new thread with 1 or more parameters (for example with a listview or something)?


And i tried something like that (i hope thats the thing what you mean tlhIn`toq):

private void Button1_Click(object sender, EventArgs e)
{
     Thread t = new Thread(new ThreadStart(test));
     t.Start(ListView1);
}

private void test(ListView lv)
{
    foreach (ListViewItem item in lv.Items)
    {
         MessageBox.Show(item.SubItems[1].Text + "\r\n" + item.SubItems[2].Text);
    }
}



But that code wont work.
Error Message (No overload for "test" is consistent with the delegates' System.Threading.ThreadStart)

Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Threading + ListView

Posted 11 January 2012 - 03:09 PM

You want to be using a Parameterizedthreadstart
http://msdn.microsof...hreadstart.aspx
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Threading + ListView

Posted 11 January 2012 - 03:09 PM

Look at the documentation, is there an overload that has Listview as parameter type?
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Threading + ListView

Posted 11 January 2012 - 03:11 PM

But don't send it ListView1 - that is still a GUI object that belongs to the GUI thread.

Get whichever data you need from it and send that to the new thread.

If the first thread should have the name found in ListView1.Items[1] then send that.

Basically loop through all the items of the ListView and send the data to a new thread.
Was This Post Helpful? 0
  • +
  • -

#12 morsev  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 70
  • Joined: 26-September 10

Re: Threading + ListView

Posted 11 January 2012 - 03:48 PM

i have that exception before .
you can write this code at the constructor of your app form
Control.CheckForIllegalCrossThreadCalls = false;  


Was This Post Helpful? -1
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Threading + ListView

Posted 11 January 2012 - 04:00 PM

morsev that's the worse thing you can do. Ignore the problem.

You need to send the DATA across.
  Dim t As Threading.Thread
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Me.Clear_All_Textboxes()
    t = New Threading.Thread(AddressOf DoStuff)
    Dim myDATA = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5}
    t.Start(myDATA)

  End Sub

  Private Sub DoStuff(ByVal params As Object) ' <-- Notice the type is Object'
    Dim ns = TryCast(params, Integer()) ' <-- So I need to check to see if it IS s type I expect '
    If ns Is Nothing Then Exit Sub
    For Each i In ns
      Threading.Thread.Sleep(i * 100)
      Beep()
    Next
  End Sub


This post has been edited by AdamSpeight2008: 11 January 2012 - 04:02 PM

Was This Post Helpful? 1
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Threading + ListView

Posted 11 January 2012 - 04:18 PM

Or use the BackgroundWorker object which has a ReportProgress method, that is in the correct syncronisation context to update controls.
Was This Post Helpful? 0
  • +
  • -

#15 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: Threading + ListView

Posted 11 January 2012 - 04:20 PM

Translation:

Threading.Thread t;
private void Button1_Click(object sender, EventArgs e)
{
   this.Clear_All_Textboxes();
   t = new Threading.Thread(DoStuff); //I'm not sure I converted the AdressOf part correctly, correct me if I'm wrong.
   var myDATA = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
   t.Start(myData);
}

private void DoStuff(object params)
{
   int ns = params as int;
   if (ns == null)
      return;

   foreach (int i in ns)
   {
      Threading.Thread.Sleep(i * 100);
      Interaction.Beep();
   }
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2