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 7188831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:10:13+00:00 2026-05-28T19:10:13+00:00

I was using multithreading in my window form. Following is my class. I have

  • 0

I was using multithreading in my window form. Following is my class.
I have a label control in my designer and under the newly created thread I am updating it’s text property.

Following is the case when I encapsulated the function “UpdateLabel” in a delegate “Del”.
and it works fine.

public partial class DynamicType : Form
    {
        delegate void Del(String x);

        public DynamicType()
        {
            InitializeComponent();
        }

        private void DynamicType_Load(object sender, EventArgs e)
        {
            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(StartThread));
            t.Start();
        }

        private void StartThread()
        {
            this.Invoke(new Del(UpdateLabel), new object[] { "Hi" });
        }

        private void UpdateLabel(String str)
        {
            label1.Text = str;
        }
    }

I have a another case called Anonymous Methods. Upon implementing the Anonymous methods
in the following methods. Following are the definitions.

private void StartThread()
        {
            UpdateLabel("Hi");
        }



private void UpdateLabel(String str)
        {
            Del Label = delegate(String k)
             {
                 label1.Text = k;

             };
            Label("hi");
        }

Cross-thread operation not valid: Control ‘label1’ accessed from a thread other than the thread it was created on.

My query is in the previous case I was using the delegates and it worked fine and in case of anonymous methods I removed the extra functions by directly instantiating the delegates and tried to update the label control text property and it gave the crash as mentioned above. Reason?

  • 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-05-28T19:10:15+00:00Added an answer on May 28, 2026 at 7:10 pm

    In the first example, you are calling the label update in the UI thread (via Form.Invoke). This is correct and works:

    this.Invoke(new Del(UpdateLabel), new object[] { "Hi" });
    

    In the second example, you are creating a delegate in the background thread and starting that delegate in the background thread. That doesn’t work, because the background thread is not allowed to update the UI:

    Del Label = delegate(String k) { label1.Text = k; }; 
    Label("hi");   // runs in the same thread
    

    To solve this, you have to use this.Invoke to execute the delegate in the UI thread:

    Del Label = delegate(String k) { label1.Text = k; }; 
    this.Invoke(Label, new object[] {"hi"});   // runs in the UI thread
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write this produce consumer application using multithreading. I wrote the following
I have a extension that uses multithreading using worker thread as shown here .
I have an application which performs 30 independent tasks simultaneously using multithreading, each task
Using online interfaces to a version control system is a nice way to have
Hello i have problem with putting logs using multithreading, my function loks like this
I need to develop an app that is using multithreading. Basicly, I have a
I am using multithreading in my C# code as follow: Thread startThread; public void
In my multithreading application I am using some variables that can be altered by
Using ASP.NET MVC there are situations (such as form submission) that may require a
I want to implement a multithreading environment using Qt4. The idea is as follows

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.