.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

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

Posted By:      Posted Date: September 11, 2010    Points: 0   Category :WPF
this is my code I want to put the win.show in another thread but win object in the main thread. because I need to do some verification to the win.content in main thread when the win ui is refreshing in another thread. however, I hit the exception The calling thread cannot access this object because a different thread owns it.   namespace ConsoleApplication1 {     public delegate void AsyncDelegate();     public class Program     {                 [STAThread]         static void Main(string[] args)         {             Window win = new Window();                        AsyncDelegate call = new AsyncDelegate(()=>win.Show());             call.BeginInvoke(null, null);                     }             }     }

View Complete Post

More Related Resource Links

Closing a WPF form from another thread?? The calling thread cannot access this object because a diff


I have a WPF form with a Close buttom which just does


I also have a public method in this WPF form which goes like this

public void doClose()

doClose() is supposed to be called by other classes but I keep getting this runtime error when this method is called:

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

Please tell me how to solve it.



1 + 1 = 3

Dynamic controls added at runtime, threading, The calling thread cannot access this object because a




My problem is on load I initialise a new thread. This thread create a new control then tries to add the control to a stackapnel and the error I receive is -

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

Heres is my code e-




t = new

Unable to cast COM object error when calling COM from .NET C# worker thread


I'm having an issue calling a COM object function from .NET C# client program. The COM object is written in C++ unmanaged. The problem only appears when I'm calling from a worker thread, it works fine if I call it from the UI thread. This is the error:

Unable to cast COM object of type 'gvAgileFileReaderLib.gvAgileFileReaderClass' to interface type 'gvAgileFileReaderLib.IgvMpiRead'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{616B5C39-C7FB-4C8C-85D4-27559DD83304}' failed due to the following error: Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL)).

I have tried the following methods but all of them fails:

1) Create the COM object on UI thread, and call its function via its interface (IgvMpiRead) in the worker thread

2) Create the COM object on the worker thread & access its function from worker thread

3) Tried with worker thread set to STA & MTA

I need to use the COM function is a worker thread to prevent a long processing from holding up my UI, but it just doesn't work when I try to call the COM function inside the worker thread.

The weird thing is that doing the same from a unmanaged C++ client works fine (I can create the com object on UI, and then call its function from worker thread). Is this problem caused by inter

InvalidCastException when calling JavaScript from C# control using a second thread.

Original Issue: I need to be able to call functions in the JavaScript of an HTML page (not ASP.NET) from a C# object embedded in that page.  The c# object has to be .NET 2 level. So I created a C# DLL assembly in VS 2010 which contains a class called NoUIControlClass. set the target framework to .NET 2.0 and made sure to check "Make assembly COM-Visible".  I also signed the assembly so it would have a strong name so I could install it in the GAC.  So far so good. Here is the batch file I run after the project is built to register the control and place it into the GAC: copy "C:\Users\x\Documents\Visual Studio 2010\Projects\NoUIControl\NoUIControl\bin\Debug\NoUIControl.dll" C:\Exploration\NoUIControl C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm NoUIControl.dll /unregister C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm NoUIControl.dll NoUIControl.dll C:\"Program Files (x86)"\"Microsoft SDKs"\Windows\v7.0A\Bin\gacutil.exe /u NoUIControl.dll C:\"Program Files (x86)"\"Microsoft SDKs"\Windows\v7.0A\Bin\gacutil.exe /i NoUIControl.dll This works and all is good.  Next I've turned the security level in IE all the way down for intranet and trusted sites.  Just to make sure that doesn't get in the way.  Here is the HTML file I serve from the web server. <html> <head

calling thread must be STA, because many UI components require this

Hi all,             I created a simple WPF Application to display a Message. public Window1()         {               InitializeComponent();                              }           public void set(string s)         {                           Msg.Content = s; // Label         }   A Service will send a message for every five minutes. How can I access the set() function from that service?        

how to access a thread from other then the one it created.



am using the above code line to make a thread call within another thread in .net 3.5 compact framework project.
I keep getting the following error
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.

How do i get rid of this error. Please help

Thanks in advance

Getting SPRequest object was not disposed before the end of this thread in SP document library event


I am using a sharepoint event listener attached to a document library to export files when they are added or modified. Here is a partial stack trace from the SP log for the SPRequest object was not disposed:

This SPRequest was allocated at    at Microsoft.SharePoint.Library.SPRequest..ctor()     at Microsoft.SharePoint.SPGlobal.CreateSPRequestAndSetIdentity(SPSite site, String name, Boolean bNotGlobalAdminCode, String strUrl, Boolean bNotAddToContext, Byte[] UserToken, String userName, Boolean bIgnoreTokenTimeout, Boolean bAsAnonymous)     at Microsoft.SharePoint.SPWeb.InitializeSPRequest()     at Microsoft.SharePoint.SPFile.GetFileStream(OpenBinaryFlags openOptions, String etagNotMatch, String& etagNew)     at Microsoft.SharePoint.SPFile.OpenBinary(SPOpenBinaryOptions openOptions)     at Extract.SharePoint.Redaction.IdShieldFileListener.HandleSharePointFileEvent(SPItemEventProperties properties, FileEventType eventType)

Here is basically the code in the HandleSharePointFileEvent:

    public override void ItemUpdated(SPItemEventProperties properties)

Is XpathDocument thread safe?!? (FileShare.Read access?)


if you  have a private void function inside of main page of system.web.page.ui

            XPathNavigator nav;
            XPathDocument docNav;
            XPathNodeIterator NodeIter;
            docNav = new XPathDocument(@"D:\data\data.xml");
            nav = docNav.CreateNavigator();

and multiple users are now on the web application simultaneously using nav object, will it be thread safe?  In filestream, the solution was to use FileShare.Read parameter.  but does xpathdocument have something similar?


What will happen if two thread tried to access records from table


Hey Guys,

It might sound silly but pls answer me...

i would like to know what exactly happens when i try to access a table from two different thread at the exact same time..?

transfer ownership of object to UI thread


getting the error "the calling thread cannot access this object because a different thread owns it".  I have created a BitmapSource in a background thread.  The I assign that BitmapSource to a property in the WPF Window class so the UI thread can access the bitmap.  I understand Dispatcher.BeginInvoke.  The background thread has done its work. How do I make it so the UI thread can access what the background thread created?



Thread Performance: Resource Contention Concurrency Profiling in Visual Studio 2010


Visual Studio 2010 includes new resource contention profiling features that help detect concurrency contention among threads. We walk through a profiling investigation to demonstrate how you can pinpoint and fix resource contention problems in your code.

Maxim Goldin

MSDN Magazine June 2010

Thread Diagnostics: Performance Tuning with The Concurrency Visualizer in Visual Studio 2010


Understanding performance bottlenecks in multithreaded apps used to require significant instrumentation and analysis by expert developers. The Concurrency Visualizer in Visual Studio 2010 significantly reduces the burden of parallel performance analysis.

Hazim Shafi

MSDN Magazine March 2010

CLR Inside Out: Thread Management In The CLR


Getting the performance you want in concurrent applications is not as straightforward as you might think. See how common threading issues can affect your application.

Erika Fuentes and Eric Eilebrecht

MSDN Magazine December 2008

Editor's Note: new Thread(ReadEditorsNote).Start(); yourAttention.WaitOne();


Introducing the issue, Howard Dierking points out that you can't simply parallelize your code blindly if you expect to truly reap the benefits that parallelism promises.

Howard Dierking

MSDN Magazine October 2008

Pooled Threads: Improve Scalability With New Thread Pool APIs


The new threadpool APIs in Windows Vista and in the upcoming Windows Server 2008 provide advances that even native Windows developers can benefit from.

Robert Saccone

MSDN Magazine October 2007

Concurrent Affairs: Build a Richer Thread Synchronization Lock


In my last column, I showed the various thread synchronization mechanisms employed by the Microsoft® . NET Framework (see Concurrent Affairs: Performance-Conscious Thread Synchronization). I then examined the performance characteristics of all these mechanisms and determined that the Interlocked methods performed the best because the calling thread never has to transition to kernel mode.

Jeffrey Richter

MSDN Magazine March 2006

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend