Мой индикатор выполнения не закрывается, когда процесс завершается 100%
Привет,
Я добавил индикатор выполнения в свой код c#, чтобы отслеживать выполнение работы.
Индикатор выполнения открывается и отлично работает, но он не закрывается, когда процесс завершен. и это бросает меня "
System.InvalidOperationException: 'A task may only be disposed if it is in a completion state (RanToCompletion, Faulted or Canceled).'"
Что я уже пробовал:
{ var t = Task.Run(() => Showprogress()); //Graphics g = e.Graphics; int i = 0; int j = 1; DirectoryInfo dinfo = new DirectoryInfo(filePath); FileInfo[] Files = dinfo.GetFiles("*.jpg"); List<mat> imageStructure = new List<mat>(); Dictionary<string, int> noOfObjects = new Dictionary<string, int>(); foreach (FileInfo file in Files) { Image<Bgr, Byte> image = new Image<Bgr, Byte>(file.FullName); if (image != null) { if (i < myprogress.getMaxValue()) myprogress.updateprogress(i); else { j++; myprogress.setMaxValue(j*100); } i++; myRectangles.Clear(); var detections = this._yoloWrapper.Detect(ImageToByte(image.Bitmap)).ToList(); foreach (var dect in detections) { if (CheckMarkingForDisplay(dect)) if (noOfObjects.Keys.Contains(dect.Type)) noOfObjects[dect.Type] += 1; else noOfObjects.Add(dect.Type, 1); } } } String csv = String.Join(Environment.NewLine, "Object Type, Count" + Environment.NewLine); csv = csv + String.Join(Environment.NewLine, noOfObjects.Select(d => d.Key + "," + d.Value)); if (!Directory.Exists(@"C:\CountingObject\" + dinfo.Name)) Directory.CreateDirectory(@"C:\CountingObject\" + dinfo.Name); System.IO.File.WriteAllText(@"C:\CountingObject\" + dinfo.Name + @"\ObjectCount.csv", csv); myprogress.updateprogress(j*100); t.Dispose(); //the error is occurring here myprogress.Close(); }
Функции, которые я использую для индикатора выполнения, таковы
using System.Windows.Forms; namespace SIMSVisionTool { public partial class progressBar : Form { public progressBar(int maxvalue) { InitializeComponent(); this.progressBar1.Value = 0; this.progressBar1.Maximum = maxvalue; } public void updateprogress(int i) { this.progressBar1.Invoke((MethodInvoker)delegate { // Running on the UI thread this.progressBar1.Value = i; }); } public int getMaxValue() { return this.progressBar1.Maximum; } public void setMaxValue(int maxvalue) { this.progressBar1.Invoke((MethodInvoker)delegate { // Running on the UI thread this.progressBar1.Maximum = maxvalue; }); } } }
Richard MacCutchan
Сделайте то, что я предложил вчера, и используйте правильный класс ProgressBar, который предоставляется в файле .NET.