刷新工作线程中的UI元素

时间:2014-02-20 08:16:15

标签: c# multithreading winforms

问题1:我想通过委托和调用工作线程刷新标签。它很好用,直到我试图关闭表格。在结束事件中,我在发生异常时停止工作线程,然后停止UI线程(对象处理异常)。看来form1被处理掉了。我不知道出了什么问题。

问题2:运行此代码时,内存使用量不断增加。我不认为它应该占用这么多的内存空间。您可以通过检查任务管理器查看此信息。

这是我的代码: (.Net Framework 4,winforms)

调度程序:

class Scheduler
{
    private Thread[] workThreads = null;
    //Scheduler started flag
    public static bool bSchedulerStarted = false;        
    //Work threads stop event
    public static EventWaitHandle stopWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);

    private static Scheduler self = null;
    public static Scheduler getInstance()
    {
        if (self == null)
        {
            self = new Scheduler();
        }
        return self;
    }
    private Scheduler()
    {
        workThreads = new Thread[1];
    }

    private void CreateThread()
    {
        workThreads[0] = new Thread(Worker.doWork);
        workThreads[0].IsBackground = true;
    }

    public void startUp()
    {
        if (!bSchedulerStarted)
        {
            stopWaitHandle.Reset();

            CreateThread();

            //Start all work threads
            for (int i = 0; i < 1; i++)
            {
                workThreads[i].Start();
            }
            bSchedulerStarted = true;
        }
    }
    public void stop()
    {
        if (!bSchedulerStarted)
            return;

        //Send stop event
        stopWaitHandle.Set();

        bSchedulerStarted = false;

        if (workThreads != null)
        {
            //wait for all work threads to stop
            for (int i = 0; i <1; i++)
            {
                if (workThreads[i] != null && workThreads[i].IsAlive)
                    workThreads[i].Join();
            }
        }
    }
}

工人:

class Worker
{       
    public static void doWork()
    {
        while (true)
        {
            if (Scheduler.stopWaitHandle.WaitOne(10, false) == true)
            {
                break;
            }

            Form1.count++;
            Form1.sysMsgEvent.Set();
            Thread.Sleep(10);
        }
    }
}

并形成:

public partial class Form1 : Form
{
    public static int count = 0;

    public static EventWaitHandle sysMsgEvent = new EventWaitHandle(false, EventResetMode.AutoReset);
    public static EventWaitHandle stopEvent = new EventWaitHandle(false, EventResetMode.ManualReset);

    private EventWaitHandle[] waitEvent = null;
    Thread UIThread = null;

    private delegate void ShowMsg();

    public Form1()
    {
        InitializeComponent();
        waitEvent = new EventWaitHandle[2];

        waitEvent[0] = stopEvent;
        waitEvent[1] = sysMsgEvent;

    }

    public void UpdateUI()
    {
        while (true)
        {
            switch (EventWaitHandle.WaitAny(waitEvent))
            {
                case 0: //Stop UI thread
                    return;
                case 1: //Refresh UI elements
                    updateLabel();
                    break;
                default:
                    return;
            }//switch
        }//while
    }

    private void updateLabel()
    {
        if (label1.InvokeRequired)
        {
            ShowMsg d = new ShowMsg(updateLabel);
            this.Invoke(d, null);
        }
        else
        {
            label1.Text = count.ToString();
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        UIThread = new Thread(new ThreadStart(UpdateUI));
        UIThread.Start();

        Scheduler sc = Scheduler.getInstance();
        sc.startUp();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        Scheduler sc = Scheduler.getInstance();
        sc.stop();

        //stop UI thread
        Form1.stopEvent.Set();
    }
}

0 个答案:

没有答案
相关问题