JG Vimalan's Blog

It is all about coding!

The calling thread cannot access this object because a different thread owns it.

I got this error message when I was implementing BackgroundWorker in my WPF application. This error message got caught when I tried to access a dropdownlist value in the ‘DoWork’ event of worker process. So, instead of accesing the controls directly in ‘DoWork’ event, I passed the necessary values as arguments in the RunWorkerAsync method. Invoking the RunWorkerAsync method calls the ‘DoWork’ event. So, the result code is as follows,

object[] args = new object[3];
            args[0] = cmbGender.SelectedValue;
            args[1] = cmbState.SelectedValue;
            args[2] = cmbCountry.SelectedValue;
                       
            if (!bgWorker.IsBusy)
                bgWorker.RunWorkerAsync(args);

and, in the ‘DoWork’ method, I will get the parametes and use them as shown below,

void bgWorker_DoWork(object sender, DoWorkEventArgs e
{
 var args = e.Argument as object[];
if (Convert.ToInt32(args[2]) != 0)
{

this will solve the issue.

Advertisements

November 19, 2010 - Posted by | C#.NET

9 Comments »

  1. great! thanks for this post.

    Comment by Jennifer | November 24, 2010 | Reply

  2. cool, it works!

    Comment by Calvin | November 26, 2010 | Reply

  3. thank you.

    Comment by Hemanth | November 26, 2010 | Reply

  4. keep blogging! useful code blocks.

    Comment by kelly hawk | November 26, 2010 | Reply

  5. thank you

    Comment by Janet | November 27, 2010 | Reply

  6. Can I use multiple background worker on the same webpage?

    Comment by Johnson | November 27, 2010 | Reply

  7. sir, i am new to software development. The code snippets given in this blog is really useful. Thanks.

    Comment by vijay | December 4, 2010 | Reply

  8. I definitely like your line of blogging. I added it to my favorites. Thanks.

    Comment by Mr. Bob | December 8, 2010 | Reply

  9. thanks to google I found

    Comment by joria | August 18, 2011 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: