线程化,让一个线程知道其他线程的进度 [英] Threading, let one thread know the others progress

查看:73
本文介绍了线程化,让一个线程知道其他线程的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,好一阵子了,我决定只使用线程.我正在使用语法荧光笔,但通常使用的文件大小却使我的性能一直很差.因此,我做了两种形式,第一种形式以纯文本形式显示文件,并且当您单击以启动这样的新线程时,按钮上显示"openincolor"

Ok, well I have been at it for a while now and I decided to just use threads. I am making a syntax highlighter but I keep getting terrible performance with the file sizes that it will usually be used for. So I made two forms, the first shows the file in plain text and has a button that says "openincolor" when you click that I start a new thread as such

    private void button1_Click(object sender, EventArgs e)
    {
        ColoringThread colorer = new ColoringThread(this.m_bruteView.Text);
        Thread theThread = new Thread(new ThreadStart(colorer.OpenColorWindow));
        theThread.Start();
    }

    public class ColoringThread
    {
        string text;
        public ColoringThread(string initText)
        {
            text = initText;
        }
        public void OpenColorWindow()
        {
            Form2 form2 = new Form2(text);
            form2.ShowDialog();
        }
    };

我希望此表单在每次完成x线填色时都发送回一条消息.然后,我将考虑并确定进度并将其显示给用户.

I want this form to send back a message each time it has complete say x lines of coloring. Then I will take that and figure out the progress and display it to the user.

我该如何发送消息或事件(...?我可以做到这一点)到我的第一个表单,以使其了解其他进程吗?

How might I go about sending a message, or event(...? can I do that) to my first form to let it know of the others progress?

推荐答案

一种简单的方法是使用事件来报告进度.

One very simple way to do this is with BackgroundWorker. It already provides an event to report progress.

这篇关于线程化,让一个线程知道其他线程的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆