1

我正在使用后台工作人员进行处理,同时我需要更新进度条。我使用 backgroundworker.ReportProgress 更新了 progressBar 值,但是在后台线程完成后,这些值会更新给用户。但是我需要在 backgroundworker 运行时显示进度条更新。

我的代码是

    public bool IsClosed()
    {
      bool profileClosed = false;           
        for (int i = 0; i < originalEntityList.Count; i++)
        {
            if (originalEntityList[i] is Core.DatabaseServices.Point)
                entityList.RemoveAt(i);
        }         
        CoreApp.ModelSpace.customProgressValue = 0;
        CoreApp.ModelSpace.TotalPercentage = OriginalEntityList.Count;          

        if (!CoreApp.ModelSpace.backgrdWorker.IsBusy)
            CoreApp.ModelSpace.backgrdWorker.RunWorkerAsync(); 

        CoreApp.ModelSpace.IndicateProgress(true, new EventArgs()); 

        if (!profileClosed)
                UtilitiesLocalization.ShowMessage(UtilitiesLocalization.ProfileNotClosedMessage, UtilitiesLocalization.InvalidProfileTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);

        return profileClosed;
    }  



     private void backgrdWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    { 
        if (!checkProfileInvalid)
        {
            Thread overlapThread = new Thread(new ThreadStart(CheckOverlapEntities)); 
            overlapThread.IsBackground = true;
            overlapThread.Start();
            overlapThread.Join();
        }
    }  




    public static bool CheckOverlapEntities()
    {
        OverlapedEntities.Clear();
        OriginalCollection = new List<Entity>(EntityCollection);
        TempCollection = new List<Entity>(EntityCollection);

        List<Thread> threadList = new List<Thread>();
        for (int id = 0; id < TempCollection.Count; id++)
        {
            CoreApp.ModelSpace.backgrdWorker.ReportProgress(id+1);          

            Entity currentEntity = TempCollection[id];
            if (currentEntity is PolyLine3d && (currentEntity as PolyLine3d).IsClosed) continue;

            Thread tempThread = new Thread(new ParameterizedThreadStart(FindOverlapInParallel));
            threadList.Add(tempThread);
            tempThread.Start(currentEntity);

            if (threadList.Count == MaxThread)
            {
                for (int i = 0; i < threadList.Count; i++)
                {
                    threadList[i].Join();
                    if (checkProfileInvalid) break;
                }
                threadList = new List<Thread>();
            }           
        }                     
        CoreApp.ModelSpace.backgrdWorker.ReportProgress(100);
        if (OverlapedEntities.Count > 0)
            return true;
        return false;
    }  




    void backgrdWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
    {             
            colorProgressBar1.Value = e.ProgressPercentage;
            colorProgressBar1.PerformStep(); 
           if (e.ProgressPercentage == colorProgressBar1.Maximum)             
                colorProgressBar1.Value = 0;                                     
    }

提前致谢..

4

0 回答 0