修复拖放冻结浏览器

本文关键字:浏览器 冻结 拖放 | 更新日期: 2023-09-27 18:18:08

我有一个应用程序,它允许在对其执行可能非常长的操作之前将数据拖放到其中。这工作得很好,但是,浏览器窗口冻结,而我的应用程序正在处理。有什么方法可以在我拿到文件列表副本后立即"发布"它吗?

我当前的代码是:

    private void MainForm_DragDrop(object sender, DragEventArgs e)
    {
        ClearTempFiles(); //Clear all files before populating
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); //Get the filepaths for the dragdrop data
        List<string> toParse = new List<string>();
        foreach (string file in files) 
        {
            FileAttributes attr = File.GetAttributes(file);
            if (attr.HasFlag(FileAttributes.Directory)) //If a folder has been included, add all files from within
            {
                toParse.AddRange(DirSearch(file));
            }
            else
            {
                toParse.Add(file); //Add files
            }
        }
        CurrentJobData = new JobData(toParse); //Create new JobData from these files <---- Takes up to a few minutes with hundreds of files.
        CurrentJobData.ToTree(treeView1); //Push this data to the TreeView
    } //Handles the dragdrop of data, populating the solution

修复拖放冻结浏览器

您需要让事件运行到完成。由于DragDrop操作的性质,涉及的两个应用程序都需要确保操作已经完成。它们只能判断DragDrop消息是否由消息泵处理。只要不发生这种情况,浏览器就只能假设用户仍在拖放。因此,正如已经在评论中建议的那样,使用BackgroundWorker:

private void Form1_DragDrop(object sender, DragEventArgs e)
{    
    // gets all data from Explorer
    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); //Get the filepaths for the dragdrop data
    // don't block the UI thread, that prevents the 
    // processing of both Windows messages in Explorer and your app
    // to complete, effectively blocking both of them
    backgroundWorker1.RunWorkerAsync(files);
    // if you want your form to be unavailable
    // while processing takes place
    // set Enabled to false on your form
    this.Enabled = false;
}

你的DoWork事件处理程序现在将做繁重的工作。注意我是如何设置DoWorkEventArgsResult属性来将JobData发送到完成的事件的。

// this all runs on a background, non-UI threed
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    ClearTempFiles(); //Clear all files before populating
    string[] files = (string[])e.Argument; // this is passed in from RunWorkerAsync
    List<string> toParse = new List<string>();
    foreach (string file in files)
    {
        FileAttributes attr = File.GetAttributes(file);
        if (attr.HasFlag(FileAttributes.Directory)) //If a folder has been included, add all files from within
        {
            toParse.AddRange(DirSearch(file));
        }
        else
        {
            toParse.Add(file); //Add files
        }
    }
    e.Result = new JobData(toParse); //Create new JobData from these files <---- Takes up to a few minutes with hundreds of files.
}

RunWorkerCompleted事件处理程序你回到UI线程,在那里你可以填充和更新你的TreeView。请注意,最后表单是如何再次启用的,以确保用户可以与应用程序交互。

// this runs on the UI thread
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    CurrentJobData = (JobData) e.Result;
    CurrentJobData.ToTree(treeView1); //Push this data to the TreeView
    // set Enabled to true on your controls
    this.Enabled = true;
}

如果你想报告进度,确保使用ProgressChanged事件处理程序,这样你就在UI线程上,并调用后台工作者的ReportProgress方法来调用该处理程序。