-2

我的情况是:循环和线程并行工作..我想停止循环的执行,直到线程完成其功能,当线程状态停止时,那时我想进一步执行循环..

            for (int pp = 0; pp < LstIop.Count; pp++)
            {
                oCntrlImageDisplay = new CntrlImageDisplay();
                oCntrlImageEdit = new CntrlImageEdit();
                axAcroPDF1 = new AxAcroPDFLib.AxAcroPDF();
                int pages = ConvertFileIntoBinary(LstIop[pp].Input, oCntrlImageEdit);
                oCntrlImageDisplay.ImgDisplay.Image = LstIop[pp].Output;
                oCntrlImageDisplay.ImgEditor.Image = oCntrlImageDisplay.ImgDisplay.Image;


                if (t1 == null || t1.ThreadState.ToString() == "Stopped")
                {
                    t1 = new Thread(() => convert(pages, LstIop[pp].Input, LstIop[pp].Output, stIop[pp].Temp));
                    t1.SetApartmentState(ApartmentState.STA);
                    t1.IsBackground = true;
                    CheckForIllegalCrossThreadCalls = false;
                    t1.Start();

                }
            }
4

2 回答 2

-1

正如其他人所说,这里的线程没有意义,但如果你一心想要它,那就做异步。只需使用 .Invoke 或 .begininvoke 后跟 .endInvoke

前任:

    delegate void T2();

    static void Main(string[] args)
    {
        T2 Thread = new T2(Work);

        while (true)
        {
            IAsyncResult result = Thread.BeginInvoke(null, null);

            //OTHER WORK TO DO

            Thread.EndInvoke(result);
        }
    }

    static void Work()
    {
       //WORK TO DO
    }

使用委托很好,因为您可以指定返回数据并发送参数

    delegate double T2(byte[] array,string text, int num);

    static void Main(string[] args)
    {
        T2 Thread = new T2(Work);

        while (true)
        {
            IAsyncResult result = Thread.BeginInvoke(null, null);

            //OTHER WORK TO DO

            double Returned = Thread.EndInvoke(result);
        }
    }

    static double Work(byte[] array, string text, int num)
    {
        // WORK TO DO
        return(3.4);
    }
于 2012-09-24T16:04:27.223 回答
-2

要等待线程完成执行,请调用:

t1.join();
于 2012-09-24T15:47:38.230 回答