Solved

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

Posted on 2009-07-15
5
1,396 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now