Solved

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

Posted on 2009-07-15
5
1,463 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
  • 2
  • 2
5 Comments
 
LVL 85

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 85

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

679 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