How do I set Task or Thread privileges?

-1

How do I set Task or Thread privileges ?

I ask because I use Task Scheduler Managed Wrapper and when I call it in another thread (that apparently has lower privileges) I get exception.

I saw this SO question, but didn't manage to use it successfully (Thread.CurrentPrincipal.Identity as WindowsIdentity is null)

Edit:

This is how I start the thread:

await Task.Factory.StartNew(() => {
    /*...*/
}, cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);

Edit 2:

These are the exception's details:

COMException: Interface not registered (Exception from HRESULT: 0x80040155)

    at Microsoft.Win32.TaskScheduler.V2Interop.IRegisteredTask.get_Definition()
    at Microsoft.Win32.TaskScheduler.Task.get_Definition()
    at TCM.TaskShedulerStartUp.Update() in c:\...\TaskShedulerStartUp.cs:line 61
c#
multithreading
task-parallel-library
asked on Stack Overflow Aug 4, 2013 by Tar • edited May 23, 2017 by Community

1 Answer

3

This COM error code has nothing to do with thread privileges, the linked article is quite wrong about that. An interface is registered in the HKLM\Software\Classes\Interface registry key. COM will look there when it needs to marshal the call from one thread to another, it needs to know what proxy is going to copy the function argument values. Which is certainly a very likely scenario in your program, the only thing we know is that you do start a thread.

It seems there's something wrong with the registry on your machine. Or the COM server simply isn't registered correctly. Or the COM server just doesn't support being used from a thread. Or your program is a 32-bit process on the 64-bit version of Windows and the interface key is not present in Wow6432Node. Or the server was registered incorrectly, writing the keys only in HKCU but not HKLM, which does have something to do with the user account used to run the code.

There's nothing in your question that would help us help you narrow that down. Having a problem with the task scheduler is a long-distance explanation, it losing a registry key would be a rather gross problem with your machine.

Start diagnosing this problem by running SysInternals' ProcMon utility. You'll see your program searching for the key in Interface and not finding it. Update your question with the full key name to get more help.

answered on Stack Overflow Aug 4, 2013 by Hans Passant • edited Aug 4, 2013 by Hans Passant

User contributions licensed under CC BY-SA 3.0