0

I'm trying to create a "Debug window" or "Output window" using windows forms, so if there is some error in the users input I can show that error in this "output window", code and the problem below:

public partial class DebugWindow : Form
    {
        public DebugWindow()
        {
            InitializeComponent();
        }

        public void WriteDebugWindow(string text)
        {
            DateTime dt = DateTime.Now;
            text = dt.ToString("[yyyy-MM-dd HH:mm:ss] ") + text;
            listBox1.Items.Add(text);
        }
    }

And the class

public class Debug
    {
        DebugWindow debugWindow;

        public Debug() { debugWindow = new DebugWindow(); }

        public void WriteDebugWindow(string text)
        {
            this.debugWindow.WriteDebugWindow(text);
            OpenWindow();
        }

        public void OpenWindow()
        {
            if (debugWindow.Visible)
                debugWindow.BringToFront();
            else
                debugWindow.Show(); //Problem here
        }
    }

So, if I try to run a code like:

int i = 10;
Debug.WriteDebugWindow(i.ToString());
i = 20;
Debug.WriteDebugWindow(i.ToString());

It will write the text to the listBox, but if I use the ".Show()" the debug window open, but it's freezes, I cant close/move/etc, if I use ".ShowDialog()" it does NOT freeze, but it will set the i to 20 and print it again only if I CLOSE the "debug window" form.

There is a solution for that? Because I tried everything, run it in a thread, create a new stance etc etc, but the probem persists, since I want to keep the listBox items in future "Debug.Write..." calls, I dont want a blank listBox everytime I call it(in case of using new DebugWindow()).

Thank you.

Servy
  • 202,030
  • 26
  • 332
  • 449
Kyore
  • 388
  • 2
  • 7
  • 29
  • You need to not block your UI thread if you want to update the UI. If you have long running non-UI work to do, do it in a non-UI thread. – Servy Jan 26 '15 at 21:21
  • I tried to add a Thread in the WriteDebugWindow() method, but it open and close the second form(debugWindow) – Kyore Jan 27 '15 at 12:21
  • Don't try to create a second UI thread. It's a terrible idea that will only cause you further problems down the road. Instead stop doing non-UI work in your UI thread, and do your non-UI work in a non-UI thread. Doing your UI work in a non-UI thread and your non-UI work in your UI thread is only going to cause headaches. – Servy Jan 27 '15 at 14:49
  • Additionally, don't edit potential solutions into your question. If you have an answer, post it as an answer. – Servy Jan 27 '15 at 14:50

2 Answers2

0

I would recommend you look at servys answer here:

Run two winform windows simultaneously

And the msdn article here:

https://msdn.microsoft.com/en-us/library/system.windows.forms.application.applicationexit.aspx

I'm not really sure how you are blocking the UI thread but as you can see showing two forms at the same time without separate threads is possible so perhaps its something your doing in the "on show" event of the debug form?

How many times are you calling "write to debug window"? I would write your debug to a separate list and have an "update debug form" ran at the end of the function instead.

You could even be fancy and use some kind of scope logic (use a temp logging class that when it goes out of scope updates the form with its contents)

Community
  • 1
  • 1
chrispepper1989
  • 2,100
  • 2
  • 23
  • 48
-1

Edit: Found the solution:

Created a Thread to the ShowDialog()

public void OpenWindow()
        {
            if (debugWindow.Visible)
                debugWindow.SafeBringToFront();
            else
                new Thread(() => debugWindow.ShowDialog()).Start();
        }

And the SafeBringToFront() in the second form:

public void SafeBringToFront()
        {
            this.Invoke(new MethodInvoker(delegate() { this.BringToFront(); }));
        }

Thanks everyone!

Kyore
  • 388
  • 2
  • 7
  • 29