How to run Bootstrapper in BackgroundWorker.DoWork event

Topics: Prism v4 - WPF 4
Aug 13, 2013 at 10:13 AM
Edited Aug 13, 2013 at 10:18 AM
Hi,

I am using Prism 4.1 with MEF. What i want is to run Bootstrapper on background silently while my login window shows up. So that user can continue to input username and password while the Bootstrapper is loading a lot of modules.

In order to get this, I am using BackgroundWorker.However i get error saying "The calling thread must be STA, because many UI components require this.". I tried to use a thread with STA mode to run the BackgroundWorker, however it caused other issues that i could't figure out. in order to avoid this issue, i return null as a result of CreateShell()
when the Bootstrapper is running. and after Bootstrapper is loaded completely, i set the Shell property via SetShell() as shown in below. However, I got error saying "The region manager does not contain the ShellApplicationMdiContainerRegion region." . The "ShellApplicationMdiContainerRegion" is the region name I defined in ShellView xaml. Seems to me that this region is not registered in the prism.

Could someone shred some light on this?

By the way, I neither want to use a thread with STA mode nor Application.Current.Dispatcher.Invoke method to run Bootstrapper as any of them will cause other big issues.

in my Bootstrapper class:
    protected override DependencyObject CreateShell()
    {
        return null;
    }
in my login class:
    private void Backgroundworkder_DoWork(object sender, DoWorkEventArgs e)
    {
        bootstrapper = new Bootstrapper();
        bootstrapper.Run();
    }
    private void Backgroundworkder_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        bootstrapper.SetShell();
    }
    public void SetShell()
    {
        this.Shell = this.Container.GetExportedValue<ShellView>();
    }
Aug 13, 2013 at 5:59 PM
Hi,

I've seen that your question was answered in a previous thread. Therefore, it would be helpful to know why you are trying to run the Login Window in the main thread and the Bootstrapper on a BackgroundWorker instead of doing the opposite (Bootstrapper on main thread and the Login Window on a STA thread).

Regards,

Federico Martinez
http://blogs.southworks.net/fmartinez
Aug 14, 2013 at 2:50 PM
Edited Aug 14, 2013 at 2:56 PM
Yes. I used that approach as Damian suggested. It did work but the problem was that it took a long time to show up the Login Window as it's running on a seperate STA thread(my customer don't like this as it's a bad user experience).

Anyway, Thanks Federico and your colleagues for helping me all the time....

Now i use BackgroundWorker back and fix the error "The region manager does not contain the ShellApplicationMdiContainerRegion region." by calling below method and like to share it here.
    public void RegisterRegion(DependencyObject regionControl, string regionName)
    {
        if (null == regionControl || string.IsNullOrEmpty(regionName))
            return;

        if (false == regionManager.Regions.ContainsRegionWithName(regionName))
        {
            RegionManager.SetRegionManager(regionControl, regionManager);
        }
    }
Aug 15, 2013 at 5:40 PM
Hi,

I'm glad you found an approach to solve this problem. Thanks for sharing your solution with the community as it would be useful for other users facing a similar scenario.

Regards,

Federico Martinez
http://blogs.southworks.net/fmartinez