简体   繁体   中英

Console Writeline with threaded C#

Hello I am trying to make threaded system for my console applications console output.

I did the KonsolStream like that.

public class KonsolStream
{
    ManualResetEvent _pauseEvent = new ManualResetEvent(true);
    ManualResetEvent _shutdownEvent = new ManualResetEvent(false);
    Thread _thread;

    private string _yazi;
    private int _tip;

    public int _Tip
    {
        get => _tip;
        set => _tip = value;
    }
    public string _Yazi
    {
        get => _yazi;
        set => _yazi = value;
    }

    public void KonsolaYaz()
    {
        switch (_Tip)
        {
            case 1:
                //uyarı
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(_Yazi);
                Console.ResetColor();
                IstemciDurdur();
                break;
            case 2:
                //başarı
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(_Yazi);
                Console.ResetColor();
                IstemciDurdur();
                break;
            case 3:
                //log
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(_Yazi);
                Console.ResetColor();
                IstemciDurdur();
                break;
            default:
                IstemciDurdur();
                break;
        }
    }
    private void IstemciDurdur()
    {
        _pauseEvent.Reset();
    }
    public void Start()
    {
        _thread = new Thread(DoWork);
        _thread.Start();
    }
    public void Resume()
    {
        _pauseEvent.Set();
    }

    public void DoWork()
    {
        while (true)
        {
            _pauseEvent.WaitOne(Timeout.Infinite);

            if (_shutdownEvent.WaitOne(0))
                break;

            KonsolaYaz();
        }
    }

}
public class KonsolMesaji : KonsolStream
{
    public KonsolMesaji(string yazi,int tip)
    {
        _Yazi = yazi;
        _Tip = tip;
    }
    public void Yaz()
    {
        Start();
    }
}

The Problem is when i start SistemBaslat() The messages are not synchronous. some times it starts with write message. and sometimes its start with Again I want to make the KonsolStream like, When i want to write some output to console i will use this stream lateron.

If you want the order of the message to be kept, you will have to use only one thread reading a single queue and have all the other threads push data into it. The good news is that your code will be much simpler; the bad news is that you have to have access to the queue wherever you want to log (you may create a static logger to ease this).

Edit: Instead of giving you the code, I suggest you check this link: Simple MultiThread Safe Log Class . You should find all your answers.

When reading your question I'm asking myself: do you just want to know about that for academical reasons (ie to learn how logging in a multi-threaded system works) or do you actually want to have multi-threaded logging in a productive application later on?

If your answer is the latter I would suggest using log4net as this can give you exactly what you want with just a little of configuration.

Update
After looking at the problem again I agree with PaulF that there's still a need to tie the results of the threads together and then write the different buckets to the console sequentially. In order to achieve this you could use a (static) instance of a class that holds all the logging messages for each thread (to distinguish the threads you can use the thread id as a key) and waits for the thread to finish, then writes all the log entries of this thread to the console sequentially.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM