Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Returning custom IPrincipal from background thread to main thread

40 views
Skip to first unread message

dele...@blueyonder.co.uk

unread,
Jul 10, 2007, 10:26:05 AM7/10/07
to
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

Peter Ritchie [C# MVP]

unread,
Jul 10, 2007, 4:56:02 PM7/10/07
to
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.
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#

dele...@blueyonder.co.uk

unread,
Jul 11, 2007, 3:47:01 AM7/11/07
to
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.


> 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.
> --

> Browsehttp://connect.microsoft.com/VisualStudio/feedback/and vote.http://www.peterRitchie.com/blog/

Peter Ritchie [C# MVP]

unread,
Jul 11, 2007, 1:30:02 PM7/11/07
to
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?

http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#

dele...@blueyonder.co.uk

unread,
Jul 12, 2007, 4:52:45 AM7/12/07
to
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


On 11 Jul, 18:30, Peter Ritchie [C# MVP] <PRS...@newsgroups.nospam>
wrote:


> 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?
> --

> Browsehttp://connect.microsoft.com/VisualStudio/feedback/and vote.http://www.peterRitchie.com/blog/


> Microsoft MVP, Visual Developer - Visual C#
>
>
>
> "deler...@blueyonder.co.uk" wrote:
> > 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.
>
> > > 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.- Hide quoted text -
>
> - Show quoted text -


0 new messages