用C#创建一个线程

时间:2012-07-01 14:53:25

标签: c# .net

好的,我有以下代码:

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错误,请原谅我,我是新的。

2 个答案:

答案 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是启动方法初始化并在完成后处理

尝试在类

中声明BackgroundWorker
using 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;

    }
  }
}