Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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()
        {
            bw.Start();
        }
    }

    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)
        {
            _ph.CheckHost();
        }

        public void Start()
        {
            _bw.RunWorkerAsync();
        }
    }

    class PrintHost
    {
        public PrintServer _hostingServer;
        public PrintHost()
        {
            _hostingServer = new PrintServer(@"\\" + Environment.MachineName, PrintSystemDesiredAccess.AdministrateServer);
        }

        public void CheckHost()
        {
            _hostingServer.Refresh();
        }
    }
}


The line containing:
_hostingServer.Refresh();

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

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 vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
Comments
Simon Bang Terkildsen 25-Aug-11 23:52pm    
Important information indeed +5
Sergey Alexandrovich Kryukov 25-Aug-11 23:54pm    
Thank you, Simon.
--SA
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.
 
Share this answer
 
Comments
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?
[no name] 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900