Solved

Update Listview from  threads in a Threadpool in VB.net or C#

Posted on 2009-07-15
5
1,493 Views
Last Modified: 2013-12-17
I'm trying to update a listview from a threads in a threadpool.  Sequentially updating the listview is too slow, so I wanted to use threads.  Just using a single worker thread using a delegate to update the UI is still too slow.  So I want to spawn as many threads as rows in the listview(several hundred).  This will likely swamp the CPU, so I want to put them into a threadpool.  I've used threadpools before, but mainly just to insert data into databases, never to update a UI.

How do you update the UI from threads in a threadpool?

I'm using VB.NET 2005.  I'm also not using virtual listviews.  I would even take a C# method.
0
Comment
Question by:Pber
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24861363
"... using a single worker thread using a delegate to update the UI ..."

You've already answered your own question.  You use a Delegate with Invoke just like with any other Thread...

I seriously doubt using a ThreadPool and/or multiple threads is going to make the overall operation any faster!

A "virtual" ListView would probably work better as you then have less data to retrieve thus it executes faster.
0
 
LVL 15

Accepted Solution

by:
jinal earned 500 total points
ID: 24861411

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    public delegate void fun(string id);
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(test));
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(test));
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(test));
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(test));
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(test));
            
        }
 
        public void test(object o)
        {
            for (int i = 0; i < 10; i++)
            {
                if (this.listView1.InvokeRequired)
                {
                    this.listView1.Invoke(new fun(test1),i.ToString() + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
                }
            }
        }
 
        public void test1(string id)
        {
            listView1.Items.Add(id);
        }
 
        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            string locationText = String.Format("{0}, {1}", e.X, e.Y);
            listView1.Items.Add(locationText);
        }
 
      
    }
}

Open in new window

0
 
LVL 26

Author Comment

by:Pber
ID: 24861586
Idle_Mind
The problem is the interface that I'm getting the data from has 256 points to scan and it is slow.  Each point can return data within a range of 1 to 2 seconds.  So sequential processing takes on everage about 5 minutes.  Using a single background worker only gives me GUI control back as it still only sequentially returns the data.  
I've written similar programs that extract data from slow interfaces and collect the data into a dataset and bulk insert them into SQL and they complete in seconds using threadpools.  
"I seriously doubt using a ThreadPool and/or multiple threads is going to make the overall operation any faster!"
Why would it not be faster?  Would the thread marshalling back to the UI thread become a bottleneck?
 
 
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24861637
Sorry....your original description didn't quite paint enough of the picture.  Given the extra info I think the threadpool would actually help...

Thread marhsalling (context switching) can be come a serious bottleneck but it sounds like in your situation they won't exactly be coming in rapid succession so it will probaby be fine.
0
 
LVL 26

Author Closing Comment

by:Pber
ID: 31603728
Thanks.  Worked like a charm.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question