Moving application context between backgroun worker and UI thread

Moving application context between backgroun worker and UI thread

Old forum URL:

Saman posted on Wednesday, July 06, 2011


i have searched a lot about  this issue but i didn't find my answer

i have a login form in my application and i used background worker in my login process. but when ui thread gets the control the application context is not initiated, in other word it has initiated in backgrounworker thread.

therefor i fetched the login information in the background worker thread and then create the principal object in the worked completed event as rocky mentioned in some posts "You can never interact with UI elements in the actual work method - you can only interact with the UI in the progress or complete event handlers because those are on the UI thread" but  still i have not user credential in my UI thread! and when my ui forms try to create csla objects they got unauthorized exception

Thank you


RockfordLhotka replied on Thursday, July 07, 2011

This is a pretty common issue in WPF apps, and the Csla.ApplicationContext.User property is designed to overcome the way WPF "manages" the application principal object.

I haven't encountered the issue in other UI environments. What UI technology are you using?

Saman replied on Thursday, July 07, 2011

Thanks Rocky

i used  Winforms technology

JonnyBee replied on Saturday, July 09, 2011

Have you tried/are you using the Csla.Threading.BackgroundWorker?

The default System.Threading.BackgroundWorker will NOT transfer the ApplicationContext to the background thread and is the reason we added our own BackgroundWorker in the Csla.Threading namespace.

Saman replied on Saturday, July 09, 2011

thanks a lot jonny thats it

i did not know about csla's background working

Saman replied on Sunday, July 10, 2011

Jonny i used CSLA.Threading.BW but the still i have that problem

what should i do please?

JonnyBee replied on Monday, July 11, 2011

Could you create a sample project to illustrate your problem?
It's much easier if we can see the code.

You should set Csla.ApplicationContext.User in your worker completed and make sure to tell Csla that your AuthenticationMode (app.config) is different from "Windows" (which in turn makes Csla send the ApplicationContext.User over the wire on DataPortal calls). Have a look at the ProjectTracker sample for how to use a custom principal and settings in app.config.

Remember also that your UI thread/ process will continue to run and possibly make other DataPortal calls before the BW has completed.

I'd also recommend to initialize the ApplicationContext.LocalContext, ClientContext and GlobalContext in the startup method.
If not initialized before - then be aware that your WorkerCompleted EventArgs offer the GlobalContext as returned from the server (and your code must decide if it is going to update theGlobalContext on the UI thread in any way after the async process is completed).

Copyright (c) Marimer LLC