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

  • Home
  • SEARCH
  • 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 8497153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:03:01+00:00 2026-06-11T00:03:01+00:00

I am working on converting a console application into a windowed format and as

  • 0

I am working on converting a console application into a windowed format and as someone who knows little about it but has had experience with a similar application already in window format in the past I figured it wouldn’t be too hard.

So I created a form and added a textbox to it just to get the logging information to start with.

This console app used to run in a single thread, I have since added a second thread to allow the form to run side by side with the console for testing. (it runs fine in a single thread strangely now too).

This is the code I am using to write text to the form except that I am not getting ANYTHING at all on the form.

    static Form1 f = new Form1();
    delegate void SetTextCallback(string s);

    private void SetText(string text)
    {
        // InvokeRequired required compares the thread ID of the 
        // calling thread to the thread ID of the creating thread. 
        // If these threads are different, it returns true. 
        if (f.textBox1.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            f.textBox1.Invoke(d, new object[] { text });
        }
        else
        {
            f.textBox1.AppendText(text);
        }
    }

I can confirm that there is text entering the “text” variable but it is not getting to the form.

Any help would be appreciated.

This is the full file:

using System;
using System.Windows.Forms;
using Chraft.Properties;
using System.IO;
using Chraft.Plugins.Events.Args;
using Chraft.Plugins.Events;

namespace Chraft
{
public class Logger
{
    private StreamWriter WriteLog;
    private Server Server;

    internal Logger(Server server, string file)
    {
        Server = server;
        try
        {
            WriteLog = new StreamWriter(file, true);
            WriteLog.AutoFlush = true;
        }
        catch
        {
            WriteLog = null;
        }
    }

    ~Logger()
    {
        try
        {
            WriteLog.Close();
        }
        catch
        {
        }
    }

    public void Log(LogLevel level, string format, params object[] arguments)
    {
        Log(level, string.Format(format, arguments));
    }

    public void Log(LogLevel level, string message)
    {
        //Event
        LoggerEventArgs e = new LoggerEventArgs(this, level, message);
        Server.PluginManager.CallEvent(Event.LOGGER_LOG, e);
        if (e.EventCanceled) return;
        level = e.LogLevel;
        message = e.LogMessage;
        //End Event

        LogToConsole(level, message);
        LogToForm(level, message);
        LogToFile(level, message);
    }

    private void LogToConsole(LogLevel level, string message)
    {
        if ((int)level >= Settings.Default.LogConsoleLevel)
        {
            Console.WriteLine(Settings.Default.LogConsoleFormat, DateTime.Now, level.ToString().ToUpper(), message);
        }
    }
    static Form1 f = new Form1();
    delegate void SetTextCallback(string s);

    private void SetText(string text)
    {
        // InvokeRequired required compares the thread ID of the 
        // calling thread to the thread ID of the creating thread. 
        // If these threads are different, it returns true. 
        if (f.textBox1.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            f.textBox1.Invoke(d, new object[] { text });
        }
        else
        {
            f.textBox1.AppendText(text);
        }
    }

    private void LogToForm(LogLevel level, string message)
    {
        if ((int)level >= Settings.Default.LogConsoleLevel)
        {
            SetText(DateTime.Now + level.ToString().ToUpper() + message);
        }
    }

    private void LogToFile(LogLevel level, string message)
    {
        if ((int)level >= Settings.Default.LogFileLevel && WriteLog != null)
            WriteLog.WriteLine(Settings.Default.LogFileFormat, DateTime.Now, level.ToString().ToUpper(), message);
    }

    public void Log(Exception ex)
    {
        //Event
        LoggerEventArgs e = new LoggerEventArgs(this, LogLevel.Debug, ex.ToString(), ex);
        Server.PluginManager.CallEvent(Event.LOGGER_LOG, e);
        if (e.EventCanceled) return;
        //End Event

        Log(LogLevel.Debug, ex.ToString());
    }


    public enum LogLevel : int
    {
        Trivial = -1,
        Debug = 0,
        Info = 1,
        Warning = 2,
        Caution = 3,
        Notice = 4,
        Error = 5,
        Fatal = 6
    }
}
}
  • 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-11T00:03:03+00:00Added an answer on June 11, 2026 at 12:03 am

    The problem is that you are creating two Form objects. One that is created in your Program.cs file:
    Application.Run(new Form1());

    And the one you created in your logger class

    Form f = new Form1();

    The one passed to Application.Run is the one that the user is interacting with. It has become visible and responds to user interaction because of the Application.Run call.

    The one you created on your logger class just sits there in memory. Its TextBox is happily adding the text you ask it to, but that one isn’t visible anywhere.

    There are many ways to handle this situation. You could gain access to the correct Form object through Application.OpenForms, but a more appropriate way to handle it would be to add an event on the logger that the form can subscribe to and it can handle updating the TextBox in response to the event.

    Updated

    class LoggerLogEventArgs : EventArgs
    {
        public LoggerLogEventArgs(string message)
        {
            this.message = message;
        }
        private string message;
        public string Message { get { return message; } }
    }
    
    class Logger
    {
        public event EventHandler<LoggerLogEventArgs> Logged;
    
        protected virtual void OnLogged(LoggerLogEventArgs e)
        {
            EventHandler<LoggerLogEventArgs> handler = Logged;
            if (handler != null)
                handler(this, e);
        }
    
        // I would change this method name to LogToEvent
        private void LogToForm(LogLevel level, string message)
        {
            if ((int)level >= Settings.Default.LogConsoleLevel)
            {
                OnLogged(new LoggerLogEventArgs(message));
            }
        }
    }
    
    class Form1 : Form
    {
        // Subscribe to the logger only when we are ready to display text
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            GetLog().Logged += new EventHandler<LoggerLogEventArgs>(logger_Logged);
        }
    
        // Unsubscribe from the logger before we are no longer ready to display text
        protected override void OnHandleDestroyed(EventArgs e)
        {
            GetLog().Logged -= new EventHandler<LoggerLogEventArgs>(logger_Logged);
            base.OnHandleDestroyed(e);
        }
    
        private void logger_Logged(object sender, LoggerLogEventArgs e)
        {
            if (InvokeRequired)
                BeginInvoke(new EventHandler<LoggerLogEventArgs>(logger_Logged), e);
            else
                textBox1.AppendText(e.Message);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on converting a Backbone application into an Ember application using Ember
I'm trying to get a console application working with the SignalR .Net Client but
I am presently working on converting a 32-bit application into a 64-bit application in
I'm working on converting my standard PHP project to OOP but I ran into
Currently working with converting SQLException error messages into messages that are more useful for
I'm working on converting a very simple java desktop application to run in java
I'm working on converting a mathematical formula into a program. This formula is called
If we are writing about power-wise option - is using a console-based application in
I am working on converting a CVS repository that has the following symbols (among
I am working on a project that involves converting data into dos date and

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.