Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8446471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:56:37+00:00 2026-06-10T09:56:37+00:00

I am making a WinForms program, which requires separate threads For readability and maintainability,

  • 0

I am making a WinForms program, which requires separate threads
For readability and maintainability, i have separated all non-GUI code out into different classes. This class also ‘generates’ another class, which does some processing. However, i have now run into the issue where i need to change a WinForms control (append a string to textbox) from a thread that was initiated in a different class

I have searched around, and found solutions for different threads, and in different classes, but not both and the solutions provided seem incompatible (to me)

This may be the biggest ‘lead’ however: How to update UI from another thread running in another class

Class Hierarchy example:

class WinForm : Form
{
    ...
    Server serv = new Server();
}

// Server is in a different thread to winform
class Server
{
    ...
    ClientConnection = new ClientConnection();
}

// Another new thread is created to run this class
class ClientConnection
{
    //Want to modify winform from here
}

I understand that eventhandlers are probably the way to go, but i can’t work out how to do so in this situation (I am also open to other suggestions 😉 )

Any help appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:56:38+00:00Added an answer on June 10, 2026 at 9:56 am

    It does not matter from which class you are updating the form. WinForm controls have to be updated on the same thread that they were created on.

    Hence, Control.Invoke, allows you to execute a method on the control on its own thread. This is also called asynchronous execution, since the call is actually queued up and executed separately.

    Look at this article from msdn, the example is similar to your example. A separate class on a separate thread updates a list box on the Form.

    —– Update
    Here you do not have to pass this as a parameter.

    In your Winform class, have a public delegate that can update the controls.

    class WinForm : Form
    {
       public delegate void updateTextBoxDelegate(String textBoxString); // delegate type 
       public updateTextBoxDelegate updateTextBox; // delegate object
    
       void updateTextBox1(string str ) { textBox1.Text = str1; } // this method is invoked
    
       public WinForm()
       {
          ...
          updateTextBox = new updateTextBoxDelegate( updateTextBox1 ); // initialize delegate object
        ...
        Server serv = new Server();
    
    }
    

    From the ClientConnection Object, you do have to get a reference to the WinForm:Form object.

    class ClientConnection
    {
       ...
       void display( string strItem ) // can be called in a different thread from clientConnection object
       {
             Form1.Invoke( Form1.updateTextBox, strItem ); // updates textbox1 on winForm
       }
    }
    

    In the above case, ‘this’ is not passed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a custom button (Winforms Control Library) and have the code below so
I am making Winforms application. I have put gridview in usercontrol. Which is another
I am making an application in C# which uses a winform as the GUI
I have found a similar question to mine in Making a WinForms TextBox behave
I am making a multi-lingual (computer languages) notepad in WinForms. I have a menu
I'm learning winforms and I have set myself a simple goal of making a
I have a Winforms app which is being deployed to the employees as a
(WinForms .net 2.0) I'm making a form which, much like the standard MessageBox ,
I am making a personal WinForms app. In my scenario say I have a
i am making a music player in winforms. i have a progress bar and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.