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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:28:57+00:00 2026-06-18T12:28:57+00:00

So I’m working on my first multithreaded WPF app. Please bear in mind I

  • 0

So I’m working on my first multithreaded WPF app. Please bear in mind I have little to no understanding of how to implement multithreading – I’ve done it in a few apps and always work off the existing code. This is my first attempt at it in WPF which is apparently quite different to Windows Forms…

So basically I’m working on this example which oddly enough doesn’t mention that you have to instantiate a new thread and start it – I guess the author felt that was self evident even to newbies like myself.

In any case, it works okay until I get to the point where I want to update the properties of my UI controls at which point I’m hit with an InvalidOperationException that says the calling thread can’t access the control because the control belongs to a different thread

So basically this is not a thread safe way to work, but I’m at a loss as to how I should fix this…

Here’s my code:

string fn = "";

private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
    if (lblActivity.Visibility != System.Windows.Visibility.Hidden)
    {
        lblActivity.Visibility = System.Windows.Visibility.Hidden;
    }
    if (lstResult.Items.Count != 0)
    {
        lstResult.Items.Clear();
    }

    OpenFileDialog ofd = new OpenFileDialog();

    Nullable<bool> result = ofd.ShowDialog();

    if (result == true)
    {
        txtSource.Text = ofd.FileName;
        txtDest.Text = ofd.FileName;
        fn = ofd.SafeFileName;

        PopulateControlsDuingLongRefreshCall("read");
    }
}

private void PopulateControlsDuingLongRefreshCall(string action)
{
    ShowProcessing();

    ThreadStart ts = delegate
    {
        switch (action)
        {
            case "format":
                Populate();
                CleanData();
                break;
            case "read":
                TextReader tr = new StreamReader(txtSource.Text);
                txtPreview.Text = tr.ReadToEnd();
                break;
        }

        Dispatcher.BeginInvoke(DispatcherPriority.Normal, (EventHandler)delegate
        {
            HideProcessing();
        }, null, null);

    };
    Thread thr = new Thread(ts);
    thr.Start();
}

private void ShowProcessing()
{
    recProcessing.Visibility = Visibility.Visible;
}
private void HideProcessing()
{
    recProcessing.Visibility = Visibility.Collapsed;
}

If anyone can suggest a solution here or explain more about the concept I’m using (I’m still pretty fuzzy on it) so that I can understand enough to find a solution on my own, that’d be much appreciated.

Thanks in advance!

EDIT
The error also comes up if I’m getting the UI control properties TextReader tr = new StreamReader(txtSource.Text); also throws the exception.

  • 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-18T12:28:58+00:00Added an answer on June 18, 2026 at 12:28 pm

    In your delegate, rather than access txtSource directly, try using the following function to return a streamreader:

        private StreamReader GetTxtSource()
        {
            if (!this.Dispatcher.CheckAccess())
            {
                return this.Dispatcher.Invoke(new Func<StreamReader>(this.GetTxtSource)) as StreamReader;                
            }
            return new StreamReader(txtSource.Text);
        }
    

    You might need to use txtSource.Dispatcher.CheckAccess() instead.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.