Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7830017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:02:19+00:00 2026-06-02T11:02:19+00:00

I try to open an wpf window in my c# test application. But when

  • 0

I try to open an wpf window in my c# test application.
But when I open the window, it is immediately closed again.

Whats wrong in my code?

Main.cs (also available here):

namespace Project1
{
    class TestClass
    {
        public static MainWindow _mainWindow = null;

        static void Main(string[] args)
        {
            Thread t = new Thread(new ThreadStart(ThreadProc));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();

            while (true)
            {
                System.Threading.Thread.Sleep(1000);
                _mainWindow.ToString();
            }
        }

        public static void ThreadProc()
        {
            TestClass2 testClass = new TestClass2();
            testClass.Open();
        }
    }

    class TestClass2
    {
        public void Open()
        {
            TestClass._mainWindow = new MainWindow();
            TestClass._mainWindow.Show();
            Console.WriteLine("=)");
        }
    }
}

MainWindow.xaml:

http://paste.ubuntu.com/943800/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:02:22+00:00Added an answer on June 2, 2026 at 11:02 am

    Your code is doing some strange things for which there is no apparent reason:

    • Why create a new thread and then put the one you already have into an endless loop?
    • Why call .ToString() on your Window, which additionally is owned by a different thread? (I ‘m not sure if this would cause your program to crash due to the ownership issue like most other operations would, but it’s probable).

    Furthermore, you do not create a message loop anywhere so even if the program worked it would be completely unresponsive to user input. After creating your Window, in whatever thread you do it, you should call

    System.Windows.Threading.Dispatcher.Run();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

when I try to open a file for reading in my console application i
When I try to open a file in write mode with the following code:
Whenever I try to open a file with ifstream, it compiles fine, but will
We have an application using the IBM Informix driver. Whenever we try to open
In my application (WPF) i have this window: public partial class Window1 : Window
I am working on a WPF application and I wish to open sip:Username@company.com links.
Usually I open a new window like this: new Sensors(this).Show(); this.Hide(); But this method
I'm evaluating several obfuscators for protecting code in a WPF application. For checking results
I try open and play .wav files with NAudio lib. private OpenFileDialog openFileDialog =
When I try to open the following video: http://streaming1.hss-win.rpi.edu/ondemand/cogsci/issues_in_cs/issues_in_cogsci_02_04_2009.asf I can get it but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.