Click here to Skip to main content
11,930,048 members (44,303 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# threads
Here is a dummed-down version of my problem.
I replicated the exact class structure and only included the necessary code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Printing;
using System.Threading;
using System.ComponentModel;
namespace ThreadAndSpool
    class MyMainProcessClass
        MyBackgrounWorkerClass bw = new MyBackgrounWorkerClass();
        public MyMainProcessClass()
    class MyBackgrounWorkerClass
        private BackgroundWorker _bw = new BackgroundWorker();
        private PrintHost _ph;
        public MyBackgrounWorkerClass()
            _ph = new PrintHost();
            _bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        public void Start()
    class PrintHost
        public PrintServer _hostingServer;
        public PrintHost()
            _hostingServer = new PrintServer(@"\\" + Environment.MachineName, PrintSystemDesiredAccess.AdministrateServer);
        public void CheckHost()

The line containing:
Causes the following runtime error:
"The calling thread cannot access this object because a different thread owns it."

I'm guessing that calling process is "MyMainProcesssClass" and that I have to implement a Dispatcher, but so far I've not been able to get around this error.

At request I have this dummed-down code in a VS2008 c# project.
Posted 25-Aug-11 2:41am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Answering one of the follow-up questions: InvokeRequred can be called on any of the controls involved in the currently running UI. Usually it is called on the control which is used in the invocation following under "if".

Now, InvokeRequred is not really needed in most cases. If you call it from non-UI thread is will always return true. This method is needed only in one case: you put it in some method which sometimes is called from UI thread, sometimes in some other thread. People mistakenly attribute too much importance to this function.

I explained the mechanism of invocation in detail in my past solutions.

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in[^],
Control events not firing after enable disable + multithreading[^].

Simon Bang Terkildsen 25-Aug-11 23:52pm
Important information indeed +5
SAKryukov 25-Aug-11 23:54pm
Thank you, Simon.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

BackgroundWorker executes your tasks in a different thread than the one that creates it (and your PrintHost instance). You'll need to use InvokeRequired and Invoke to make cross-thread method calls.
Abhinav S 25-Aug-11 7:58am
Good answer. 5.
MatthysDT 25-Aug-11 8:23am
Thank you Shameel, I'm new to Threading, on which object should I check the InvokeRequired property?
Shameel 25-Aug-11 9:59am
I'm not sure if you are using UI elements here. This error typically occurs when you try to access UI elements from a thread other than the one that created them. In that case you'll use Control.InvokeRequired.

Where is the PrintServer class declared? What does it derive from?
MatthysDT 25-Aug-11 10:03am
I don't have UI element in this project. I declare the PrintServer object globally in my PrintHostClass and instantiate it in the constructor.
PrintServer is a .Net class in System.Printing
(C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\System.Printing.dll)

I'm beginning to think that that class is not thread safe.

If I declare the PrintServer (_hostingServer) within the CheckHost() method, I don't get the same error, but this is not ideal.
MatthysDT 25-Aug-11 8:40am
If I discard the BackgroudWorker and run _ph.CheckHost within the Timer_Elapsed event of a System.Timers.Timer, I get the exact same issue, eventhough I'm no longer using threading.
Shameel 25-Aug-11 9:53am
Because Timer too uses a different thread to run your code.
SAKryukov 25-Aug-11 23:46pm
Agree, my 5 for the answer, but... please see my solution.
I answered about InvokeRequired and added detailed explanation to invocation itself. Some of this information is little known.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 25 Aug 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100