欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 循环时,操作另外一个进程直到操作完成,循环继续执行

程序员文章站 2022-08-09 19:20:37
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.... ......
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.threading;
using system.threading.tasks;
using system.windows.forms;

namespace threadtest
{
    public partial class form1 : form
    {
        public form1()
        {
            initializecomponent();
        }

        static eventwaithandle ehandle = new eventwaithandle(false, eventresetmode.manualreset);
        bool flag = false;

        private void button1_click(object sender, eventargs e)
        {
            richtextbox1.clear();
            for (int i = 1; i <= 100; i++) {
                richtextbox1.appendtext(i + "\r\n");
                if (i % 10 == 0) {
                    ehandle = new eventwaithandle(false, eventresetmode.manualreset);
                    thread currentthread = new thread(new threadstart(testprocess));//开辟一个进度条线程
                    currentthread.start();
                    ehandle.waitone();
                    richtextbox1.appendtext("i是10的倍数: i = " + i + "\r\n");
                    if (flag == true)
                    {
                        richtextbox1.appendtext("程序结束" + "\r\n");
                        break;
                    }
                }
            }
        }


        private void testprocess() {
            if (messagebox.show("确定要退出吗?", "message", messageboxbuttons.okcancel, messageboxicon.question) == dialogresult.cancel)
            {
                flag = false;
            }
            else
            {
                flag = true;
            }
            ehandle.set();
        }

        private void richtextbox1_textchanged(object sender, eventargs e)
        {
            richtextbox1.selectionstart = richtextbox1.text.length;
            richtextbox1.scrolltocaret();
        }
    }
}