我如何才能在标签上显示,而后台工作人员正在为每个变量工作的进度

本文关键字:变量 工作人员 工作 标签 显示 后台 | 更新日期: 2023-09-27 18:03:38

代码:

private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bgw = (BackgroundWorker)sender;
            if (bgw.CancellationPending == true)
            {
            }
            else
            {
                Image img;
                CreateMainDirectory();
                CreateResolutionDirectories();
                Walkdirectoryfulldepth(mainpath);
                for (int i = 0; i < 1; i++)
                {
                    img = Image.FromFile(myGifList[i]);
                    imgWidth = img.Width;
                    imgHeight = img.Height;
                }
                DirectoryInfo di1;
                FileInfo[] fi1;
                for (int x = data.Count() - 1; x >= 0; x--)
                {
                    int reduceBy = (51 - x) * numeric;
                    for (int i = 0; i < myGifList.Count; i++)
                    {
                        img = Image.FromFile(myGifList[i]);
                        img = resizeImage((imgWidth - reduceBy), (imgHeight - reduceBy), myGifList[i]);
                        img.Save(mainpath + "''" + data[x] + "''" + Path.GetFileName(myGifList[i]), System.Drawing.Imaging.ImageFormat.Gif);
                    }
                    di1 = new DirectoryInfo(mainpath + "''" + data[x]);
                    fi1 = di1.GetFiles("*.gif");
                    List<string> newImages = new List<string>();
                    for (int i = 0; i < fi1.Length; i++)
                    {
                        newImages.Add(fi1[i].FullName);
                    }
                    unfreez.MakeGIF(newImages, mainpath + "''" + data[x] + "''" + "animated.gif", 8, true);
                }
            }
        }
        private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
        }
        private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            pictureBoxImage(path + "''" + "animated.gif");
            label10.Text = FileSize(path + "''" + "animated.gif");
            label12.Text = ImageResolution(path + "''" + "animated.gif");
            label7.Visible = true;
            label7.ForeColor = Color.Green;
            label7.Text = "Operation Have Been Completed";
            button1.Enabled = false;
            progressBar2.EndColor = Color.FromArgb(0, 211, 040);
            numericUpDown1.Enabled = true;
            button3.Enabled = true;
            timer2.Stop();
        }

例如,在Do Work事件中,我想在标签上实时显示创建了多少动画gif文件,所以我在Do Work事件中有这一行创建了动画gif:

unfreez.MakeGIF(newImages, mainpath + "''" + data[x] + "''" + "animated.gif", 8, true);

我想计数有多少动画文件创建在实时使用标签的进度改变事件,当后台工作者完成显示在这个标签上的东西,如:

我有label15,我想在label15上实时显示文件的计数,然后在最后完成时显示两个标签,一个带有文本:创建的整体动画gif文件:和label15的数字。

编辑

尝试:

unfreez.MakeGIF(newImages, mainpath + "''" + data[x] + "''" + "animated.gif", 8, true);
timesCreated += 1;
backgroundWorker1.ReportProgress(timesCreated);

创建动画gif后,向前移动一个int变量,然后报告它。然后在进行中更改事件我做了:

private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            label16.Text = timesCreated.ToString();
        }

但是在Program.cs中得到异常,我所做的是错误的。

异常:TargeInvocationException异常已被调用的目标抛出

System.Reflection.TargetInvocationException was unhandled
  HResult=-2146232828
  Message=Exception has been thrown by the target of an invocation.
  Source=mscorlib
  StackTrace:
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       at System.Reflection.RuntimeMethodInfo.UnsafeInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Delegate.DynamicInvokeImpl(Object[] args)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at mws.Program.Main() in d:'C-Sharp'Download File'Downloading-File-Project-Version-012'Downloading File'Program.cs:line 26
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.IO.FileNotFoundException
       HResult=-2147024894
       Message=Could not find file 'c:'temp'newimages'Changed_Resolution_By_10'SecondProcess_-Width = 2 Height = 2'animated.gif'.
       Source=mscorlib
       FileName=c:'temp'newimages'Changed_Resolution_By_10'SecondProcess_-Width = 2 Height = 2'animated.gif
       StackTrace:
            at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
            at System.IO.FileInfo.get_Length()
            at mws.Animation_Radar_Preview.FileSize(String FileName) in d:'C-Sharp'Download File'Downloading-File-Project-Version-012'Downloading File'Animation_Radar_Preview.cs:line 340
            at mws.Animation_Radar_Preview.backgroundWorker2_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e) in d:'C-Sharp'Download File'Downloading-File-Project-Version-012'Downloading File'Animation_Radar_Preview.cs:line 515
            at System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
            at System.ComponentModel.BackgroundWorker.AsyncOperationCompleted(Object arg)
       InnerException: 

我如何才能在标签上显示,而后台工作人员正在为每个变量工作的进度

DoWork事件的解决方案:

timesCreated += 1;
bgw.ReportProgress(timesCreated);

然后在progressChanged事件中:

private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            label16.Text = e.ProgressPercentage.ToString();
        }

没有任何问题。谢谢。