0

i have a program that uses several background workers that continuously load and read text files (size can up to 1MB) and update to oracle database.

The problem i am having is whenever the background worker is reading files, the main thread will become not responding which it should not be. Is this the default behaviour when reading files, is there any way to solve it?

Thanks, any help will be very much appreciated.

Update: [brief view of my program code]

    private void program_Load(object sender, EventArgs e)
    {
        bw1.RunWorkerAsync();
        bw2.RunWorkerAsync(); //same function as bw1 but different directory
    }

    private void bw1_DoWork(object sender, DoWorkEventArgs e)
    {
        while (true)
        {
            Thread.Sleep(5000);
            bw1.ReportProgress(0);
        }
    }

    private void bw1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        directoryInfo = /* directory url */;
        files = directoryInfo .GetFiles();

        foreach (FileInfo file in files)
        {
            /* 
               read file line by line 
               load data into database  
               update file loading status to UI
            */
        }
    }
4

1 回答 1

2

您所有的实际“工作”都在bw1_ProgressChanged事件处理程序中执行。此代码在 UI/主线程上执行。

的目的BackgroundWorker.ProgressChanged是允许 UI 更新进度条或其他小部件。实际工作(在本例中为加载文件)应在bw1_DoWork方法中执行。

于 2013-04-26T03:52:45.467 回答