Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-07-15
5
Medium Priority
?
1,542 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 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 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…
Suggested Courses

824 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