3

i'm writing a program that opens customized wpf pop-ups from an external dll(Contains a Overloaded Show method to display different kind of popups). But when I try to open more than one popup at the same time I get an exception : Cannot create more than one System.Windows.Application Instance in the same AppDomain. Tried a lot of things but still not able to figure out what i'm doing wrong here. Below are the code snapshots.

This is the code that is used to load the resourcedirectory.

  //code to load the resourcedirectory.
  public class LoadResources
    {
        public static void LoadApplicationResources()
        {
            if (Application.Current == null)
            {
                new Application();
                Application.Current.Resources.MergedDictionaries.Add(
                       Application.LoadComponent(
                           new Uri("Presentation;component/dls/dls.verydark.xaml",
                           UriKind.Relative)) as ResourceDictionary);
            }
           
        }
    }

This is the code that is used to create the STA thread which then calls the MessageBox.Show()

//
public static MessageBoxResult ShowMessageBox(string message, string caption, MessageBoxButton buttons, MessageBoxImage icon)
            {
                MessageBoxResult result = MessageBoxResult.None;
                Thread gui_thread = new Thread(() =>
                    {
                        LoadResources.LoadApplicationResources();
                        result = Presentation.Controls.MessageBox.Show(message, caption, buttons, icon);
                    });
    
                gui_thread.SetApartmentState(ApartmentState.STA);
                gui_thread.Start();
               
                return result;
            }

Calling Code :

class Program
    {
        static void Main(string[] args)
        {
            CSharpLib.MessageBoxBase.ShowMessageBox("hello", "bye", MessageBoxButtons.OK, MessageBoxIcon.Error);

           CSharpLib.MessageBoxBase.ShowMessageBox("hello", "bye", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
alooparantha
  • 51
  • 1
  • 5
  • If you are calling these popups from other dlls, create an AppDomain and then call that ShowDialog() examples [here](https://stackoverflow.com/questions/3413807/how-can-i-run-a-wpf-application-in-a-new-appdomain-executeassembly-fails) [here](https://stackoverflow.com/questions/27328200/load-a-wpf-application-assembly-from-another-wpf-app-get-error-cannot-create-m) – XAMlMAX Dec 09 '20 at 11:45

0 Answers0