使用C#中的线程更新RichTextBox。

huangapple go评论76阅读模式
英文:

update richtextbox using thread in c#

问题

Sure, here is the code with the requested translation:

我使用XAML创建了一个小的C#项目。
为了在线程函数中每1秒附加文本到RichTextBox,我使用了BeginInvoke
但它在线程完成后才更新。我需要每1秒更新一次。
Log是RichTextBox的名称

private void Worker3()
{
    int nCnt = 0;
    while (true)
    {
        nCnt++;
        Thread.Sleep(1000);

        this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
            (ThreadStart)delegate ()
            {                             
                Log.AppendText("更新中..." + nCnt.ToString()); 
            }
        );
    }
}

private void Thread_Click(object sender, RoutedEventArgs e)
{
    Thread thread1 = new Thread(new ThreadStart(Worker3));
    thread1.Start();

    // 等待工作线程完成
    thread1.Join();
}

希望这有所帮助,能够每1秒更新一次。

英文:

I made small c# project using xaml.
To append text at richtextbox every 1 second in thread function, i used BeginInvoke.
but it is updated after done thread. i need to update every 1 sec
Log is name of richtextbox.

private void Worker3()
{
    int nCnt = 0;
    while(true)
    {
        nCnt++;
        Thread.Sleep(1000);

        this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
            (ThreadStart)delegate ()
            {                             
                Log.AppendText("Updating..." + nCnt.ToString()); 
            }
        );

    }
}

private void Thread_Click(object sender, RoutedEventArgs e)
{

    Thread thread1 = new Thread(new ThreadStart(Worker3));
    thread1.Start();

    // Wait for the worker thread to finish
    thread1.Join();
}

help me. thanks

I want code that update every 1 second.

答案1

得分: 2

当您在线程上调用Join时,它将阻塞直到该线程完成。这意味着在Join调用返回之前,无法在UI线程上执行任何其他代码,因此所有您的BeginInvoke调用将只是排队等待执行,直到线程完成、Join返回并且UI线程再次空闲。请不要调用Join

您真的应该学习如何使用任务而不是线程。您可以等待任务而不会锁定UI线程。

英文:

When you call Join on the thread, that will block until that thread completes. That means that no other code can be executed on the UI thread until that Join call returns, so all your BeginInvoke calls will simply queue up waiting to be executed, which won't happen until the thread completes, Join returns and the UI thread is free again. DO NOT call Join.

You really ought to learn how to use tasks rather than threads. You can await a task without locking up the UI thread.

答案2

得分: 2

The correct way to do something periodically is to use a timer. If you are using WPF you should likely use DispatchTimer that invokes the event on the UI thread:

dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,1);
dispatcherTimer.Start();

...
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    nCnt++;
    Log.AppendText("Updating..." + nCnt.ToString()); 
}

There are other methods to handle things like long-running operations that produce a result, progress reporting, or a background thread that continuously updates. But for a simple "update text every second," a dispatch timer is the correct solution.

英文:

The correct way to do something periodically is to use a timer. If you are using WPF you should likely use DispatchTimer that invokes the event on the UI thread:

dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,1);
dispatcherTimer.Start();

...
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    nCnt++;
    Log.AppendText("Updating..." + nCnt.ToString()); 
}

There are other methods to handle things like long running operations that produce a result, progress reporting, or a background thread that continuously updates. But for a simple "update text ever second", a dispatchtimer is the correct solution.

huangapple
  • 本文由 发表于 2023年5月17日 17:24:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定