-1

我写了以下代码。在那里,我创建了两个线程 ItmTh 和 RelTh。ItmTh 线程应该首先执行,当 ItmTh 线程执行完毕时,应该执行 RelTh 线程。有什么可能的解决方案?

`

                string[] filePaths = Directory.GetFiles(folderPath, "ARAS_IT*");
                bool isTemplateFile = true;
                int arasDefinitionFileCount = filePaths.Length;
                TotalTemplateFile = arasDefinitionFileCount + Directory.GetFiles(folderPath, "ARAS_Rel*").Length;

                //progressBar1.Value=0;
                //int progess = 0;

                if (arasDefinitionFileCount < 1)
                {
                    isTemplateFile = false;
                    //MessageBox.Show("Root Folder does not contain Template File");
                    //return;
                }

                ManualResetEvent syncEvent = new ManualResetEvent(false);

                Thread ItmTh = new Thread(()=> 
                    {


                        //Iterate over Item Type files in Root Folder
                        for (int i = 0; i < arasDefinitionFileCount; i++)
                        {
                            //progess++;
                            //UpdateProgress(Convert.ToInt32(Convert.ToDouble((progess * 100) / progressBarValue)));
                            string fileName = filePaths[i];
                            //Find Name of Item Type From File Name
                            string ItemType = Path.GetFileNameWithoutExtension(fileName);
                            int start = ItemType.LastIndexOf('-');
                            int end = ItemType.Length;
                            start++;
                            end = end - start;
                            ItemType = ItemType.Substring(start, end);
                            //UpdateProgress(0);

                            if (ItemType.Equals("Document", StringComparison.InvariantCultureIgnoreCase))
                            {
                                Thread th = new Thread(() =>
                                    {
                                        processDocumentDataDefinitionFile(fileName, folderPath + "\\ARAS_IT-" + ItemType + "-files\\");

                                    });
                                th.Start();
                                //th.Join();
                            }
                            else
                            {
                                Thread th = new Thread(() =>
                                    {
                                       processDataDefinitionFile(fileName, tbRootFolderPath.Text, ItemType, folderPath + "\\ARAS_IT-" + ItemType + "-files\\");

                                    });
                                th.Start();
                                //Wait for Previous thread To complete its task
                                //th.Join();
                            }



                        }

                     });

                ItmTh.Start();
                /*******************************************************************************************************************/
                //Process Relationship files
                //ItmTh.Join();
                Thread RelTh = new Thread(()=> 
                    {

                        syncEvent.WaitOne();
                        filePaths = null;
                        filePaths = Directory.GetFiles(folderPath, "ARAS_Rel*");

                        arasDefinitionFileCount = filePaths.Length;

                        if (arasDefinitionFileCount < 1 && isTemplateFile == false)
                        {
                            MessageBox.Show("Root Folder does not contain Template File");
                            return;
                        }

                        //Iterate over Relationships files in Root Folder
                        for (int i = 0; i < arasDefinitionFileCount; i++)
                        {
                            string fileName = filePaths[i];

                            //Find Name of Item Type From File Name
                            string ItemType = Path.GetFileNameWithoutExtension(fileName);
                            int start = ItemType.LastIndexOf('-');
                            int end = ItemType.Length;
                            start++;
                            end = end - start;
                            ItemType = ItemType.Substring(start, end);

                            //Process File
                            Thread th = new Thread(() =>
                            {
                                Cursor.Current = Cursors.WaitCursor;

                                processDataDefinitionFile(fileName, tbRootFolderPath.Text, ItemType, folderPath + "\\ARAS_IT-" + ItemType + "-files\\");

                            });
                            th.Start();
                            //Wait for Previous thread To complete its task
                            // th.Join();

                        }

                    });

                        RelTh.Start();`
4

1 回答 1

2

您可以使用Tasks而不是Threads- 创建许多线程只是一种开销 - 您的计算机很可能没有那么多 CPU 内核。TasksTask Parallel Library 尝试确保根据您的硬件运行正确数量的线程。

//... your code ...
Task ItmTask = new Task.Factory.StartNew(()=> 
    {
        Task[] subTasks = new Task[arasDefinitionFileCount];

        for (int i = 0; i < arasDefinitionFileCount; i++)
        {
            //... your code...

            if (ItemType.Equals("Document", StringComparison.InvariantCultureIgnoreCase))
            {
                subTasks[i] = new Task.Factory.StartNew(() =>
                    {
                        processDocumentDataDefinitionFile(fileName, folderPath + "\\ARAS_IT-" + ItemType + "-files\\");

                    });
            }
            else
            {
                subTasks[i] = new Task.Factory.StartNew(() =>
                    {
                       processDataDefinitionFile(fileName, tbRootFolderPath.Text, ItemType, folderPath + "\\ARAS_IT-" + ItemType + "-files\\");

                    });
            }
        }

         // Continue when all sub tasks are done
        Task.Factory.ContinueWhenAll(subTasks, _ => 
         {
            // Cursor.Current = Cursors.WaitCursor;

            // .... your code ....

            Task[] subTasks2 = new Task[arasDefinitionFileCount];

            for (int i = 0; i < arasDefinitionFileCount; i++)
            {
                subTasks2[i] = new Task.Factory.StartNew(() =>
                {
                    processDataDefinitionFile(fileName, tbRootFolderPath.Text, ItemType, folderPath + "\\ARAS_IT-" + ItemType + "-files\\");

                });

            }

            Task.Factory.ContinueWhenAll(subTasks2, __ => 
              {
                 // reset cursor to normal etc.
              });

         });
     });
于 2013-07-24T16:52:12.923 回答