Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

705 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