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 6771909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:29:15+00:00 2026-05-26T15:29:15+00:00

I am able to open a new window in a new thread by the

  • 0

I am able to open a new window in a new thread by the following code.

The following code is from MainWindow.xaml.cs

private void buttonStartStop_Click(object sender, RoutedEventArgs e)
{    

  Test test = new Test();

  Thread newWindowThread = new Thread(new ThreadStart(test.start));
  newWindowThread.SetApartmentState(ApartmentState.STA);
  newWindowThread.IsBackground = true;
  newWindowThread.Start();
}

and the following from test.start()

public void start()
{

  OutputWindow outputwindow = new OutputWindow();
  outputwindow.Show();


  Output.print("Begin");
  System.Windows.Threading.Dispatcher.Run();
  Output.print("FINAL");
  System.Windows.Threading.Dispatcher.Run();

}

And the following is from the Output class

public static void print(String str)
{
  Dispatcher uiDispatcher = OutputWindow.myOutputWindow.Dispatcher;
  uiDispatcher.BeginInvoke(new Action(delegate() { OutputWindow.myOutputWindow.textBoxOutput.AppendText(str + "\n"); }));
  uiDispatcher.BeginInvoke(new Action(delegate() { OutputWindow.myOutputWindow.textBoxOutput.ScrollToLine(OutputWindow.myOutputWindow.textBoxOutput.LineCount - 1); }));
}

public static void printOnSameLine(String str)
{
  Dispatcher uiDispatcher = OutputWindow.myOutputWindow.Dispatcher;
  uiDispatcher.BeginInvoke(new Action(delegate() { OutputWindow.myOutputWindow.textBoxOutput.AppendText(str); }));
  uiDispatcher.BeginInvoke(new Action(delegate() { OutputWindow.myOutputWindow.textBoxOutput.ScrollToLine(OutputWindow.myOutputWindow.textBoxOutput.LineCount - 1); }));
}

“Begin” Does get printed in the textbox but “FINAL” does not, I want the start method in Test class to update the textbox in outputwindow through out the program. What is the best way to do this?

Thank you in advance

  • 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-05-26T15:29:16+00:00Added an answer on May 26, 2026 at 3:29 pm

    I’m not sure what are you trying to do. It is normal that FINAL does not print because you called System.Windows.Threading.Dispatcher.Run(). This method keeps thread alive and listens for events. You can look at it like if you have while(true){} inside the Run method. Method will continue to run until Dispatcher is shutdown. You should keep background thread alive and call your static methods from another thread when you need to set a message. Here’s an example:

            // reference to window in another thread
            Window outputWindow = null;
    
            Thread thread = new Thread(() =>
            {
                // another thread
                outputWindow = new Window();
                outputWindow.Show();
                // run event loop
                System.Windows.Threading.Dispatcher.Run();
            }) { ApartmentState = ApartmentState.STA, IsBackground = true };
            thread.Start();
    
            while (outputWindow == null)
            {
                // wait until the window in another thread has been created
                Thread.Sleep(100);
            }
    
            // simulate process
            for (int i = 0; i < 10; i++)
            {
                outputWindow.Dispatcher.BeginInvoke((Action)(() => { outputWindow.Title = i.ToString(); }), System.Windows.Threading.DispatcherPriority.Normal);
                Thread.Sleep(500); // simulate some hard work so we can see the change on another window's title
            }
    
            // close the window or shutdown dispatcher or abort the thread...
            thread.Abort();
    

    EDIT:

    This could be quick & dirty generic solution. DoSomeHardWork creates another GUI thread for wait window which displays progress information. This window creates work thread which actually does the work. Work is implemented in method Action. 1st argument is wait window so you can change it from work thread. Of course, in the real world you should go through interface and not directly to window implementation but this is just an example. 2nd argument is object so you can pass whatever you need to the work thread. If you need more arguments pass object[] or modify method signature. In this example I simulate hard work with counter and sleep. You can execute this code on button click multiple times and you will see all wait windows counting their own counter without freezing. Here is the code:

        public static void DoSomeHardWork(Action<Window, object> toDo, object actionParams)
        {
            Thread windowThread = new Thread(() =>
            {
                Window waitWindow = new Window();
                waitWindow.Loaded += (s, e) =>
                {
                    Thread workThread = new Thread(() =>
                    {
                        // Run work method in work thread passing the
                        // wait window as parameter
                        toDo(waitWindow, actionParams);
                    }) { IsBackground = true };
                    // Start the work thread.
                    workThread.Start();
                };
                waitWindow.Show();
                Dispatcher.Run();
            }) { ApartmentState = ApartmentState.STA, IsBackground = true };
            // Start the wait window thread.
            // When window loads, it will create work thread and start it.
            windowThread.Start();
        }
    
        private void MyWork(Window waitWindow, object parameters)
        {
            for (int i = 0; i < 10; i++)
            {
                // Report progress to user through wait window.
                waitWindow.Dispatcher.BeginInvoke((Action)(() => waitWindow.Title = string.Format("{0}: {1}", parameters, i)), DispatcherPriority.Normal);
                // Simulate long work.
                Thread.Sleep(500);
            }
            // The work is done. Shutdown the wait window dispather.
            // This will stop listening for events and will eventualy terminate
            // the wait window thread.
            waitWindow.Dispatcher.InvokeShutdown();
        }
    
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            DoSomeHardWork(MyWork, DateTime.Now);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to launch a browser window from VBA code (that
I used to be able to open a new buffer in Emacs quickly using
I want to be able to click a button and open a new form
I'd like to be able to open a TDataSet asynchronously in its own thread
I can open a new window using window.open() in ExternalInterface.call() but then I want
I want to use response.redirect to open page in new window or tab. I
I need to be able to open a document using its default application in
I need to be able to open up an external URL in my website
I want to be able to open my website and use some kind of
I’d like to be able to open a text/html file and write its contents

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.