好的,我有以下代码:
public partial class Form1 : Form
{
private void FileWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
{
ListViewItem newFile = new ListViewItem(new string[] { e.FullPath.ToString(), e.ChangeType.ToString() }, -1);
newFile.Tag = e.FullPath.ToString();
FileList.Items.Add(newFile);
}
private void CopyButton_Click(object sender, EventArgs e)
{
BackgroundWorker backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged +=
new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int TotalFiles = FileList.CheckedItems.Count;;
int CurrentFile = 1;
foreach (ListViewItem CheckedFile in FileList.CheckedItems)
{
backgroundWorker1.ReportProgress((CurrentFile / TotalFiles) * 100);
string FileBuilder = Settings.Default.Destination + Path.GetFileName(CheckedFile.Tag.ToString());
if (File.Exists(FileBuilder) == false)
{
File.Copy(CheckedFile.Tag.ToString(), FileBuilder);
}
CurrentFile++;
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
CopyProgressBar.Value = e.ProgressPercentage;
}
}
它一直告诉我,DoWork事件中的报告进度方法在当前上下文中不存在,有谁知道为什么?如果这是一个noob错误,请原谅我,我是新的。
答案 0 :(得分:5)
backgroundWorker1
是本地变量
它不存在于CopyButton_Click
之外。
您可以将其放在类字段中,也可以从sender
参数中转换它。
答案 1 :(得分:2)
private void CopyButton_Click(object sender, EventArgs e)
{
BackgroundWorker backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerAsync();
}
你BackgroundWorker是启动方法初始化并在完成后处理
尝试在类
中声明BackgroundWorkerusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using FileWatchDog.Properties;
using System.Text.RegularExpressions;
namespace FileWatchDog
{
public partial class Form1 : Form
{
BackgroundWorker backgroundWorker1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void FileWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
{
ListViewItem newFile = new ListViewItem(new string[] { e.FullPath.ToString(), e.ChangeType.ToString() }, -1);
newFile.Tag = e.FullPath.ToString();
FileList.Items.Add(newFile);
}
private void CopyButton_Click(object sender, EventArgs e)
{
backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int TotalFiles = FileList.CheckedItems.Count;;
int CurrentFile = 1;
foreach (ListViewItem CheckedFile in FileList.CheckedItems)
{
backgroundWorker1.ReportProgress((CurrentFile / TotalFiles) * 100);
string FileBuilder = Settings.Default.Destination + Path.GetFileName(CheckedFile.Tag.ToString());
if (File.Exists(FileBuilder) == false)
{
File.Copy(CheckedFile.Tag.ToString(), FileBuilder);
}
CurrentFile++;
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
CopyProgressBar.Value = e.ProgressPercentage;
}
}
}