Returning custom IPrincipal from background thread to main thread

D

delerium

Hi there,
Working on a project porting from dotnet 1.1 to 2.0

Basically I've setup a background thread which performs a login
creating a custom principal which we want to copy back to our main
thread.

In dotnet 1 we would simply assign the Thread.CurrentPrincipal in the
worker completed event handler, getting the custom principal from the
work completed event args. This worked fine (using WinFormsEx to
mimick the behaviour of the net 2.0 background worker).

the problem we are having in net 2.0 is that when we set the
Thread.CurrentPrincipal in the work completed event handler, it is
lost with the next line of code that executes in the main thread (we
blocked this using a messagebox, when you click ok, the main thread
continues but the principal identity is lost).

I've tried even using the WinFormsEx instead of net 2.0's background
worker but the behaviour is the same.

Anyone have any ideas?
Thanks
Paul
 
G

Guest

There were some changes made in .NET 2.0 with thread principles; which may be
what you're seeing.

I would pass the principle in on the RunWorkerCompletedEventArgs.Result
property, which is accomplished by setting the DoWorkEventArgs.Result
property before exiting your DoWork event handler.
 
D

delerium

Thanks for your reply.
That is actually what is being done at the moment, the problem is not
getting the principal from the do work method call to the work
completed call.
What we do is as you say, set the RunWorkerCompletedEventArgs.Result
to the principal in the DoWork event handler and in the WorkCompleted
event handler we take this and assign it to Thread.CurrentPrincipal.
What we are seeing is that on exiting the WorkCompleted eventhandler,
the principal is reverting back to what it was before the background
worker performed the login.
 
G

Guest

I'm seeing that same thing as you. Whenever I set Thread.CurrentPrinciple I
do it before the form is loaded. It appears the framework is restoring the
principle after each event handler is completed. I can't find any
documentation on this, or why it's happening...

Is there any way you can change the principle before the form is displayed?
 
D

delerium

As a work around what I've been able to do is store the principal in a
member variable during my work completed event handler. Once the event
has exited and I have lost the principal, I can read from this member
variable and restore to the main Thread.Principal.

It just strikes me as odd behaviour, as I understand the work
completed event is performed in the main thread - indeed visual studio
confirms this is the case (I also double checked in code getting the
threadid from AppDomain.GetCurrentThreadID() method). Which is even
more confusing, its as if the principal is being disposed with the
thread even though the main Thread.CurrentPrincipal has a reference.

Like yourself I have found nothing in the way of documentation to
explain any of this.

Cheers
Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top