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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:43:03+00:00 2026-05-26T12:43:03+00:00

I am using the following code. public partial class SettingApp { public SettingApp() {

  • 0

I am using the following code.

public partial class SettingApp
{
    public SettingApp()
    {
        InitializeComponent();
        Parallel.Invoke(SetDataInTextBox);
    }

    private void SetDataInTextBox()
    {
        txtIncAns.Text = Properties.Settings.Default.IncludeAN;
        txtIncAuthor.Text = Properties.Settings.Default.IncludeAutt;
        txtIncQuo.Text = Properties.Settings.Default.IncludeQU;
        txtIncSpBegin.Text = Properties.Settings.Default.IncludeSP;
    }
}

The program gives the following error

The calling thread cannot access this object because a different

thread owns it.

Which is the right way

update :

Whether this is right :

   public partial class SettingApp
{
    private delegate void SetDataInTextBoxDelegate();
    public SettingApp()
    {
        InitializeComponent();

        txtIncAns.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetDataInTextBoxDelegate(SetDataInTextBox));
    }

     private void SetDataInTextBox()
    {
        txtIncAns.Text = Properties.Settings.Default.IncludeAN;
        txtIncAuthor.Text = Properties.Settings.Default.IncludeAutt;
        txtIncQuo.Text = Properties.Settings.Default.IncludeQU;
        txtIncSpBegin.Text = Properties.Settings.Default.IncludeSP;
    }
}
  • 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-26T12:43:04+00:00Added an answer on May 26, 2026 at 12:43 pm

    Only the UI thread can access UI elements, which I’m guessing is what those txt things are. Parallel.Invoke is in your case not the UI thread, so the exception is thrown when you try to access the .Text property on the controls.

    You need to marshal the call across to the UI thread. In WinForms, controls have various ways to help you do this:

    if (myControl.InvokeRequired)
    {
        myControl.Invoke(...);
    }
    else
    {
        myControl.Text = "something";
    }
    

    MSDN has an article with examples on it here (VS2010):

    http://msdn.microsoft.com/en-us/library/757y83z4(v=VS.100).aspx

    Update 1:

    For WPF the model is similar, but includes the Dispatcher:

    myControl.Dispatcher.Invoke(...);
    
    • MSDN Forum Post
    • MSDN Article

    Update 2: Of course, it looks like you don’t even need to use multi-threaded code here. I would guess the overhead of using the multi-threaded portion is more than the code you eventually call. Simply remove the use of multiple threads from this section and set the properties directly:

        public SettingApp()
        {
            InitializeComponent();
            SetDataInTextBox();
        }
    
        private void SetDataInTextBox()
        {
            txtIncAns.Text = Properties.Settings.Default.IncludeAN;
            txtIncAuthor.Text = Properties.Settings.Default.IncludeAutt;
            txtIncQuo.Text = Properties.Settings.Default.IncludeQU;
            txtIncSpBegin.Text = Properties.Settings.Default.IncludeSP;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code... public partial class DownloadFile : System.Web.UI.Page { protected void
I have the following code: using System.Configuration; namespace test { public partial class MyService
I was just trying to understand delegates using the following code. public class delegatesEx
The following code using System.Threading; class Test { volatile int counter = 0; public
I have the following code: using System; using System.Linq; using System.Linq.Expressions; public class Program
I have the following code in a partial class and I'm using LINQ to
I have in my Form constructor, after the InitializeComponent the following code: using (WebClient
I am using following code in rowdatabound function. Protected Sub gvwMileStone_RowDataBound(ByVal sender As System.Object,
I am using following code to check whether a check box on my website
I am using following code to design my home page. The output (as shown

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.