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

  • SEARCH
  • Home
  • 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 8451713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:18:46+00:00 2026-06-10T11:18:46+00:00

I have a GUI-thread for my form and another thread that computes things. The

  • 0

I have a GUI-thread for my form and another thread that computes things.

The form has a richtTextBox. I want the worker-thread to pass strings to the form, so that every string is displayed in the textbox.

Everytime a new string is generated in the worker thread I call an event, and this should now display the string.
But I don’t know how to pass the string! This is what I tried so far:

///// Form1
private void btn_myClass_Click(object sender, EventArgs e)
{
    myClass myObj = new myClass();
    myObj.NewListEntry += myObj_NewListEntry;
    Thread thrmyClass = new Thread(new ThreadStart(myObj.ThreadMethod));
    thrmyClass.Start();
}

private void myObj_NewListEntry(Object objSender, EventArgs e)
{
    this.BeginInvoke((MethodInvoker)delegate
    {
        // Here I want to add my string from the worker-thread to the textbox!
        richTextBox1.Text += "TEXT"; // I want: richTextBox1.Text += myStringFromWorkerThread;
    });
}

///// myClass (working thread...)
class myClass
{
    public event EventHandler NewListEntry;

    public void ThreadMethod()
    {
        DoSomething();
    }

    protected virtual void OnNewListEntry(EventArgs e)
    {
        EventHandler newListEntry = NewListEntry;
        if (newListEntry != null)
        {
            newListEntry(this, e);
        }
    }

    private void DoSomething()
    {
        ///// Do some things and generate strings, such as "test"...
        string test = "test";


        // Here I want to pass the "test"-string! But how to do that??
        OnNewListEntry(EventArgs.Empty); // I want: OnNewListEntry(test);
    }
}
  • 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-10T11:18:48+00:00Added an answer on June 10, 2026 at 11:18 am

    Like this

    public class NewListEntryEventArgs : EventArgs
    {
        private readonly string test;
    
        public NewListEntryEventArgs(string test)
        {
            this.test = test;
        }
    
        public string Test
        {
            get { return this.test; }
        }
    }
    

    then you declare your class like this

    class MyClass
    {
        public delegate void NewListEntryEventHandler(
            object sender,
            NewListEntryEventArgs args);
    
        public event NewListEntryEventHandler NewListEntry;
    
        protected virtual void OnNewListEntry(string test)
        {
            if (NewListEntry != null)
            {
                NewListEntry(this, new NewListEntryEventArgs(test));
            }
        }
    }
    

    and in the subscribing Form

    private void btn_myClass_Click(object sender, EventArgs e)
    {
        MyClass myClass = new MyClass();
        myClass.NewListEntry += NewListEntryEventHandler;
        ...
    }
    
    private void NewListEntryEventHandler(
        object sender,
        NewListEntryEventArgs e)
    {
        if (richTextBox1.InvokeRequired)
        {
            this.Invoke((MethodInvoker)delegate
                {             
                    this.NewListEntryEventHandler(sender, e);
                });
            return;
        }
    
        richTextBox1.Text += e.Test;
    }
    

    I’ve taken the liberty of making the NewListEntryEventArgs class immutable, since that makes sense. I’ve also partially corrected your naming conventions, simplified and corrected where expedient.

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

Sidebar

Related Questions

Possible Duplicate: How to update GUI from another thread in C#? I have form
I have a MFC application, which has a worker thread, what I want to
I have problem, when I want add Node to my GUI from other Thread.
I have a worker thread spawned from a GUI (for GUI performance), how do
I have a GUI app that is getting to be quite slow. I want
I have a GUI window form. I want to set image to pictureBox and
I have a GUI C# application that has a single button Start/Stop. Originally this
I have a GUI program , that has a QLocalServer inside , each time
I have application with two thread. One of them (T1) is main GUI form,
i have read this question How to Create Form from within non gui thread

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.