Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to update UI control inside Parallel.ForEach ?

How to update UI control inside Parallel.ForEach ?

Scheduled Pinned Locked Moved C#
helpquestiondesignbeta-testingtutorial
3 Posts 2 Posters 28 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Martin Adams 2023
    wrote on last edited by
    #1

    Hi I am trying to use Parallel.ForEach with ConcurrentBag and it work but to display a feedback I used progessbar control to display fname value , problem after few seconds I get error and the error message is not visible instead a wight box appear because of using a thread . any idea what is the problem in this code ?

                    int row\_idx = 1;
                    ConcurrentBag<(int, string, float)> bag = new ConcurrentBag<(int, string, float)>();
                    Parallel.ForEach(elements, element =>
                    {
    
                        string fname = element.name;
                        float ftrack = (float)(element.track);
    
    
                        var elementsToAdd = new (int, string, float)\[\]
                        {
                            (row\_idx, fname, fsize)
                        };
    
                        bag.Add(elementsToAdd\[0\]);
                        
                        row\_idx++;
    
                        ProgressBar1.Text = fname; // <<---- error here
                        ProgressBar1.Update();
                        //Application.DoEvents();
    
                    });
    
    D 1 Reply Last reply
    0
    • M Martin Adams 2023

      Hi I am trying to use Parallel.ForEach with ConcurrentBag and it work but to display a feedback I used progessbar control to display fname value , problem after few seconds I get error and the error message is not visible instead a wight box appear because of using a thread . any idea what is the problem in this code ?

                      int row\_idx = 1;
                      ConcurrentBag<(int, string, float)> bag = new ConcurrentBag<(int, string, float)>();
                      Parallel.ForEach(elements, element =>
                      {
      
                          string fname = element.name;
                          float ftrack = (float)(element.track);
      
      
                          var elementsToAdd = new (int, string, float)\[\]
                          {
                              (row\_idx, fname, fsize)
                          };
      
                          bag.Add(elementsToAdd\[0\]);
                          
                          row\_idx++;
      
                          ProgressBar1.Text = fname; // <<---- error here
                          ProgressBar1.Update();
                          //Application.DoEvents();
      
                      });
      
      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      That's because you cannot touch a UI control from anything other than the UI (startup) thread. When you use tasks, you're using other threads that are not the UI thread. To change the text of a control, you have to marshal a call to a function back to the UI thread so that function updates the Text property and does it on the correct thread. You can see how it's done at How to make thread-safe calls to controls - Windows Forms .NET | Microsoft Learn[^]

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles. Dave Kreskowiak

      M 1 Reply Last reply
      0
      • D Dave Kreskowiak

        That's because you cannot touch a UI control from anything other than the UI (startup) thread. When you use tasks, you're using other threads that are not the UI thread. To change the text of a control, you have to marshal a call to a function back to the UI thread so that function updates the Text property and does it on the correct thread. You can see how it's done at How to make thread-safe calls to controls - Windows Forms .NET | Microsoft Learn[^]

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles. Dave Kreskowiak

        M Offline
        M Offline
        Martin Adams 2023
        wrote on last edited by
        #3

        Thanks

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups