0

使用 Visual Studio 2008。

主要形式是订阅类中的事件以报告进度。CSVReader 类中的 ReportProgress 事件总是出现空值,我似乎不明白为什么会这样。

主要形式:

//Constructor
    public CreateSQL()
    {
        InitializeComponent();
        csvReader = new CSVReader();
}

    private void btnCreateSQL_Click(object sender, EventArgs e)
    {


            if (backgroundWorker1.IsBusy != true)
            {                   

                backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
                backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
                **csvReader.ReportProgress += new EventHandler<CSVReader.ProgressArgs>(CSVReader_ReportProgress);**

                // Start the asynchronous operation.
                backgroundWorker1.RunWorkerAsync();

            }

    }

    protected void CSVReader_ReportProgress(object sender, CSVReader.ProgressArgs e)
    {
        // Call the UI backgroundworker
        backgroundWorker1.ReportProgress(e.Percentage, e.Message);
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
                            DataTable dt = csvReader.ReadCSVFile(sFile, _hasHeader);   

    }

CSVReader 类:

public class CSVReader : IDisposable
{
    // Event handler to bind to for reporting progress
    public EventHandler<ProgressArgs> ReportProgress;

    // Eventargs to contain information to send to the subscriber
    public class ProgressArgs : EventArgs
    {
        public int Percentage { get; set; }
        public string Message { get; set; }
    }
}

    public CSVReader()
    {

    }


    public DataTable ReadCSVFile(string filename, bool headerRow)
    {
             using (CSVReader reader = new CSVReader(new FileInfo(filename)))
            return reader.CreateDataTable(headerRow);


           }

    public DataTable CreateDataTable(bool headerRow)
    {
        // Read the CSV data into rows
        List<List<object>> rows = new List<List<object>>();
        List<object> readRow = null;
        while ((readRow = ReadRow()) != null)
            rows.Add(readRow);


        // Report progress if somebody is listening (subscribed)
        **if (ReportProgress != null)
        {
            ReportProgress(this, new ProgressArgs { Percentage = 33, Message = "Reading File" });
        }**
}

如果 ReportProgress 事件为 null 并且始终为 null,则在上面进行了测试。我把它放在这里只是为了测试让它发回几个变量,33和一个字符串,但它总是空的。

感谢您的任何和所有输入!

4

0 回答 0