Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

C# Console.WriteLine() not working in other thread after setting Console.SetOut()?

I'm working on a Windows Form application and within my Form, I've created a cross-thread safe TextWriter class that I use for Console.SetOut() to display writes to the Console in a RichTextBox, like so:

public class TextboxWriter : TextWriter
{
    private RichTextBox box;
    public TextboxWriter(RichTextBox b)
    {
        box = b;
    }

    public override Encoding Encoding => Encoding.ASCII;

    public override void Write(string value)
    {
        Debug.WriteLine(value);
        box.Invoke((MethodInvoker)delegate ()
        {
            box.Text += "[" + DateTime.Now.ToString("HH:mm:ss") + "] " + value + "
";
        });
    }
}

Console.SetOut() is called when the DiscordConsole class is created and loaded, and this is evident when I call Console.WriteLine() in the STAThread, or any other Thread, for example:

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        DiscordConsole f = new DiscordConsole();

        Thread t = new Thread(Separate);
        t.Start();

        Application.Run(f);
    }

    private static void Separate()
    {
        Thread.Sleep(1000);
        Console.Write("Loading...");
        // loading functions
        Console.Write("Load complete!");
    }

Those calls to Console.WriteLine() work fine. However, my loading function also creates multiple threads, yet in those threads, the call to Console.WriteLine() does not produce anything on the TextBox? Here's how I'm creating threads and starting them.

    public static void LoadRegistry(bool downloadAgain)
    {
        List <Thread> threads = new List<Thread>();
        string[] files = Directory.GetFiles(DataPath, "*.json");
        float groupamountload = files.Length / (float)Groups;

        for (int i = 0; i < Groups; i++)
        {
            int tmp = i;
            Thread t = new Thread(() =>
            {
                Console.WriteLine("loading");
                Debug.WriteLine("loading");
                // loading code
            });
            threads.Add(t);
        }

        Application.ApplicationExit += delegate (object info, EventArgs e)
        {
            threads.ForEach(t => t.Abort());
        };
        threads.ForEach(t => t.Start());
        threads.ForEach(t => t.Join());
    }

To verify that my threads are running and loading my files, I placed the Debug.WriteLine() AND Console.WriteLine() statements in the threads. I see the Debug.WriteLine() text outputted to Visual Studio, but the RichTextBox is not updated with my Console.WriteLine(). Does anyone know what I'm doing wrong here?

question from:https://stackoverflow.com/questions/65896770/c-sharp-console-writeline-not-working-in-other-thread-after-setting-console-se

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

SOLVED

The issue was almost unnoticeable, but I discovered why it wasn't working. My TextboxWriter class would only override the Write() function, but not the WriteLine() function. Adding an override to WriteLine(string) made it work!

I'm not sure why for some calls to Console.WriteLine() would work, and others would not but now they all work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...