简体   繁体   English

带有线程 C# 的控制台 Writeline

[英]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.我这样做了 KonsolStream。

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.问题是当我启动 SistemBaslat() 消息不是同步的。 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.有时它以再次开始,我想让 KonsolStream 像,当我想将一些输出写入控制台时,我将稍后使用此流。

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 .编辑:我建议您检查此链接: 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.如果您的答案是后者,我建议您使用log4net,因为这可以通过少量配置为您提供您想要的内容。

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.再次查看问题后,我同意 PaulF 的观点,即仍然需要将线程的结果绑定在一起,然后将不同的桶按顺序写入控制台。 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.为了实现这一点,您可以使用一个类的(静态)实例来保存每个线程的所有日志消息(为了区分线程,您可以使用线程 ID 作为键)并等待线程完成,然后写入该线程的所有日志条目按顺序发送到控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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