0

I having terrible when i execute the same method second time.i am not getting WPF screen, I don't know why? refer my code

TestWindow Button click method(it is windows application project type) and i have removed STA thread in my

Main()

TestClass test;

private void button1_Click(object sender, EventArgs e)
{
    test =TestClass.Instance; //singleton pattern
    test.ShowScreen();
}

TestClass

public void ShowScreen()
{
    var thread = new Thread(() =>
        {
            Explorer explorer = new Explorer();
            explorer.Show();
            explorer.Closed += (s, args) =>
                explorer.Dispatcher.InvokeShutdown();

            System.Windows.Threading.Dispatcher.Run();
        });
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();              
}

Above the code working fine when i run 1st time. i can able view my explorer screen. But the problem is when i close 1st screen and call once again the same method(test.ShowScreen();) the explorer screen not showing

Note : I have noticed If i didn't close the 1st window(instance) then i can able open many explorer screen. using the same code. If i closed the 1st window(instance) and i am unable open explorer screen and i am not getting any error message.

Rajamohan Rajendran
  • 369
  • 1
  • 3
  • 13
  • It seems the instance of `TestClass` is not getting closed properly. What have you coded for the same? – boop_the_snoot Aug 21 '17 at 07:31
  • The TestClass i used **singleton pattern**, The real code like this `testObj= TestClass.Instance;` – Rajamohan Rajendran Aug 21 '17 at 08:53
  • That is the issue it seems, read this [a singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance. Most commonly, singletons don't allow any parameters to be specified when creating the instance - as otherwise a second request for an instance but with a different parameter could be problematic! (If the same instance should be accessed for all requests with the same parameter, the factory pattern is more appropriate.)](http://csharpindepth.com/Articles/General/Singleton.aspx) – boop_the_snoot Aug 21 '17 at 08:57
  • I Agree your point, but the TestClass nothing to hold the Explorer screen object. i have used this class for to invoke explorer screen. for your Point of view how come multiple window displayed If i didnt closed 1st one. – Rajamohan Rajendran Aug 21 '17 at 09:36
  • Your issue is not reproducible based on the (lack of) information you have provided. Please read this: https://stackoverflow.com/help/mcve – mm8 Aug 21 '17 at 09:54
  • The issue related to this InitializeComponent(); in my Explorer class. `[System.InvalidOperationException]: {"The Application object is being shut down."}` I have resolved using the following statement `private TestClass () { if (SW.Application.Current == null) { new SW.Application { ShutdownMode = SW.ShutdownMode.OnExplicitShutdown }; } }` in the TestClass Constructors – Rajamohan Rajendran Aug 21 '17 at 11:10

1 Answers1

1

The issue is resolved Adding the following line in the TestClass Constructors

using SW = System.Windows;

private TestClass()
{
    if (SW.Application.Current == null)
    {
        new SW.Application
        {
            ShutdownMode = SW.ShutdownMode.OnExplicitShutdown
        };
    }
}
Rajamohan Rajendran
  • 369
  • 1
  • 3
  • 13