Click here to Skip to main content
15,897,187 members

How to update a control on a different thread from which it was created without interrupting the main thread?

Edson Rodrigues da Silva asked:

Open original thread
I'm new in C # and I am developing a windows application in which I have a method that should be executed asynchronously, some tasks that this method has is to update some controls in the form. For this I am using a thread with an Invoke method to update the control that belongs to the form. This form has a button that starts this thread and one that should stop it. The problem is that because the use of the Invoke method the main thread, which is the form, is blocked until the other thread to finish, so I can not click on the button that stops the thread.

Below is a program that I created to try this. It has a class that adds the numbers from 1 to 100 in a listbox.

Main program.

C#
private Thread thrAdicionarItens;
        private clcInvoke clcInvokeObject;
 
        private void btnExibir_Click(object sender, EventArgs e) {
            clcInvokeObject = new clcInvoke(lstItens);
            thrAdicionarItens = new Thread(new ThreadStart(clcInvokeObject.AdicionarItens));
            thrAdicionarItens.Start();
        }//End btnExibir_Click()

        private void btnParar_Click(object sender, EventArgs e) {
            try {
                thrAdicionarItens.Abort();
            }
            catch (Exception) {
            }
        }//End btnParar_Click()


Class code


C#
class clcInvoke
   {
       private ListBox lstItens;

       private delegate void dlgAdicionarItens();

       public clcInvoke(ListBox lstItens) {
           this.lstItens = lstItens;
       }//End clcInvoke()

       public void AdicionarItens() {
           try {
               if (lstItens.InvokeRequired == true)
                   lstItens.BeginInvoke(new dlgAdicionarItens(AdicionarItens));
               else
                   for (byte contador = 1; contador <= 100; contador++) {
                       lstItens.Items.Add(contador.ToString());
                       lstItens.Update();
                       Thread.Sleep(100);
                   }
           }
           catch (Exception) {
           }
       }//End AdicionarItens()

   }//End class clcInvoke



How do I a thread to upgrade, dynamically, a control on a form without making the form to be locked until the completion of another thread?

[Edit - Unchecked the "Treat my content as plain text, not as HTML" checkbox so the code blocks work]
Tags: C#, Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900