473,395 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do i get a cross threaded call using the paralell extensions to update progress

Hi

I have been struggling to get a toolbar to update its progress information from an event raised ina parallel for loop. I have tried using delegates to get it to work but it just hangs after about the iteration.

The code that replicates the problem is below. It s just a windows form with a toolstrip and a toolstripprogressbar with a button to start the test.

I am relativly new to multi threading but in all the examples i have tried from various sources on the net I cannot get this to work.


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         delegate void ProgressDelegate(int progress);
  16.  
  17.  
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.             // A nasty HACK but the delegate method invocation seems to be recursive and not work.
  22.             //Control.CheckForIllegalCrossThreadCalls = false; 
  23.  
  24.         }
  25.  
  26.         private void button1_Click(object sender, EventArgs e)
  27.         {
  28.             Class1 test = new Class1();
  29.             test.progress += new EventHandler<ProgressArgs>(test_progress);
  30.             test.DoWork();
  31.             this.toolStripProgressBar1.Value = 0; // reset
  32.  
  33.         }
  34.  
  35.         void test_progress(object sender, ProgressArgs e)
  36.         {
  37.             //if (e.ProgressValue>this.toolStripProgressBar1.Value)
  38.             //    this.toolStripProgressBar1.Value = e.ProgressValue;
  39.  
  40.             SetProgress(e.ProgressValue);
  41.         }
  42.  
  43.         public void SetProgress(int progress)
  44.         {
  45.             if (this.InvokeRequired)
  46.             {
  47.                 ProgressDelegate d = new ProgressDelegate(SetProgress);
  48.                 this.Invoke(d, new object[] { progress });
  49.             }
  50.             else
  51.             {
  52.                 this.toolStripProgressBar1.Value = progress;
  53.             }
  54.  
  55.         }
  56.  
  57.     }
  58.  
  59.     class Class1
  60.     {
  61.         public event EventHandler<ProgressArgs> progress;
  62.  
  63.         public void DoWork()
  64.         {
  65.             Parallel.For(0, 100, i =>
  66.             {
  67.                 Thread.Sleep(25);
  68.                 this.progress(this, new ProgressArgs(i));
  69.             });
  70.         }
  71.     }
  72.  
  73.     class ProgressArgs : EventArgs
  74.     {
  75.         int _progressvalue = 0;
  76.  
  77.         public ProgressArgs(int progressvalue)
  78.         {
  79.             _progressvalue = progressvalue;
  80.         }
  81.  
  82.         public int ProgressValue
  83.         {
  84.             get
  85.             {
  86.                 return _progressvalue;
  87.             }
  88.             set
  89.             {
  90.                 _progressvalue = value;
  91.             }
  92.  
  93.         }
  94.     }
  95. }
Best Regards
Jul 28 '08 #1
1 1427
TRScheel
638 Expert 512MB
After looking up Parallel for quite a while (I have never seen that call before) I came across a few different resources.

It seems Parallel has been updated and is now part of LINQ. In addition I had read that it actually only ran on one thread, per this article here.

My guess is that you call Thread.Sleep on the same thread over and over again until it hangs.
Jul 28 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: rob | last post by:
Dear All, I am getting an error "Illegal cross-thread operation". I am not creating any threads. I know internally there are many threads running, though. Does that mean that no form can call a...
2
by: bob | last post by:
Hello, In my appliction I try to pop up a progress dialog box while an analysis is being run: //method progress window public void Run(IWin32Window parent) { Thread analysisThread = new...
2
by: Jack Addington | last post by:
I am working on app that currently all resides on the same machine but plan to pull the database off and try to move all the datafunctionality to a remote machine. I have a data function that is...
9
by: Chris | last post by:
I have a form that take a bit to load up because of talking to a database and the amount of data process that has to take place before showing the screen. I have it working but I feel that I...
5
by: Paul Hasell | last post by:
Hi, I'm trying to invoke a web method asynchronously but just can't seem to get it to tell me when it has finished! Below is the code I am (currently) using: private void...
8
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item...
5
by: Rob R. Ainscough | last post by:
I'm using a BackgroundWorker to perform a file download from an ftp site. Per good code design practices where I separate my UI code from my core logic code (in this case my Download file method in...
5
by: Marc Gravell | last post by:
In the following example, the Bindings don't update the UI if the property change is triggered from the non-UI thread - see the async button - from the listbox contents (and observation) the change...
3
by: Jake Weller | last post by:
Hi everyone, Is it safe to call a GUI control's members directly from another thread. That is, if I launch a background thread when the user clicks a button on my form, can I safely retrieve...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.