本文介绍了当流程完成100%时,我的进度条不会关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在c#代码中添加了一个进度条来监控完成的工作。
进度条打开并运行良好,但在完成此过程时不会关闭。它让我
Hi,
I have added a progress bar to my c# code to monitor a work getting completed.
The progress bar opens and works great, but it doesn't close when the process is completed. and it is throwing me "
System.InvalidOperationException: 'A task may only be disposed if it is in a completion state (RanToCompletion, Faulted or Canceled).'
我尝试过:
"
What I have tried:
{
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();
}
我用于进度条的功能是
The functions i am using for progress bar is
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;
});
}
}
}
推荐答案
using System.Windows.Forms;
namespace SIMSVisionTool
{
public partial class progressBar : Form
{
private Action _Action{ get; set; }
public progressBar(int maxvalue, Action _action)
{
InitializeComponent();
if(_action == null)
Throw new NullArgumentException("The worker thread is null");
this._Action = _action;
this.progressBar1.Value = 0;
this.progressBar1.Maximum = maxvalue;
}
public override void OnLoad(){
// Load the page on a separate thread
Task.Factory.StartNew(_Action).ContinueWith(t=>{t.Close();}, TaskScheduler.FromCurrentSynchronizationContext());
}
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;
});
}
}
}
这是在问题上实现它的方法/>
Here is the way to implement it on the Problem
class myWorkerClass{
public void StartWork(){
// Here you initialize the form passing in the maximum value and the method to execute asyncronously
myprogress = new progressBar(maxValue, getImages);
}
// The helper Method To Pass In as the action delegate
private void getImages(){
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);
}
}
这篇关于当流程完成100%时,我的进度条不会关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!