开发者

wpf drag drop show processing popup

开发者 https://www.devze.com 2023-01-12 04:19 出处:网络
I made a program in wpf c#. I made a drag and drop handler which adds some items to a listbox. While the program is doing that (it takes some time) I want a Grid to change its property visiblity to vi

I made a program in wpf c#. I made a drag and drop handler which adds some items to a listbox. While the program is doing that (it takes some time) I want a Grid to change its property visiblity to visible and I want to update a textbox to show the user which file is being processed. The code is as follows:

UPDATE: Solution implementation Try

            BackgroundWorker bgWorker = new BackgroundWorker(); 
private void Dropaudio(object sender, System.Windows.DragEventArgs e) 
{ 


    bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork); 
    bgWorker.ProgressChanged +=  
        new ProgressChangedEventHandler(bgWorker_ProgressChanged); 
    bgWorker.WorkerReportsProgress = true; 
    this.Drop += new DragEventHandler(Dropaudio);
    if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
    { 
        string[] droppedFil开发者_如何学编程ePaths =  
            e.Data.GetData(DataFormats.FileDrop, true) as string[]; 
        List<string> Jobs = new List<string>(droppedFilePaths); 
        bgWorker.RunWorkerAsync(Jobs); 
    } 
} 

void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 
    if (e.ProgressPercentage == 0) 
    { 
        Addingcues.Visibility = Visibility.Visible; 
    } 
    addcuepath.Text = e.UserState.ToString(); 
} 

void bgWorker_DoWork(object sender, DoWorkEventArgs e) 
{ 
    List<string> Jobs = e.Argument as List<string>; 
    bgWorker.ReportProgress(0, "Processing Data"); 
    double count = 0; 
    double total = Jobs.Length; 
    foreach (string droppedFilePath in Jobs) 
    {                 
        if (System.IO.Path.GetExtension(droppedFilePath) == ".mp3" ||  
            System.IO.Path.GetExtension(droppedFilePath) == ".wav" ||  
            System.IO.Path.GetExtension(droppedFilePath) == ".flac") 
        { 
            double pct = count / total; 
            // Report this file 
            bgWorker.ReportProgress((int) (pct * 100), droppedFilePath); 
            var provider = (XmlDataProvider)this.Resources["CUEData"]; 
            XmlDocument xmlcuelijst = provider.Document;
            XmlNode cueshow = xmlcuelijst.SelectSingleNode("CUEShow");
            XmlNode maincues = cueshow.SelectSingleNode("Maincues");
            XmlElement Maincue = xmlcuelijst.CreateElement("Maincue");
            XmlElement nr = xmlcuelijst.CreateElement("nr");
            XmlElement Description = xmlcuelijst.CreateElement("Description");
            XmlElement Cuetype = xmlcuelijst.CreateElement("Cuetype");
            XmlElement Name = xmlcuelijst.CreateElement("Name");
            XmlElement Path = xmlcuelijst.CreateElement("Path");
            XmlElement Duration = xmlcuelijst.CreateElement("Duration");
            XmlElement Type = xmlcuelijst.CreateElement("Type");
            XmlElement Fade = xmlcuelijst.CreateElement("Fade");
            XmlElement Fadein = xmlcuelijst.CreateElement("Fadein");
            XmlElement Fadeout = xmlcuelijst.CreateElement("Fadeout");
            XmlElement Delay = xmlcuelijst.CreateElement("Delay");
            XmlElement Delaytime = xmlcuelijst.CreateElement("Delaytime");
            XmlElement Loop = xmlcuelijst.CreateElement("Loop");
            XmlElement FX = xmlcuelijst.CreateElement("FX");
            XmlElement Filename = xmlcuelijst.CreateElement("Filename");
            Maincue.AppendChild(nr);
            Maincue.AppendChild(Cuetype);
            Maincue.AppendChild(Name);
            Maincue.AppendChild(Path);
            Maincue.AppendChild(Description);
            Maincue.AppendChild(Duration);
            Maincue.AppendChild(Type);
            Maincue.AppendChild(Fade);
            Maincue.AppendChild(Fadein);
            Maincue.AppendChild(Fadeout);
            Maincue.AppendChild(Delay);
            Maincue.AppendChild(Delaytime);
            Maincue.AppendChild(Loop);
            Maincue.AppendChild(FX);

            count += 1; 
        } 
    } 

} 

void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    Addingcues.Visibility = Visibility.Hidden; 
}

This works (changed array to a list) but at the Appendchild actions, the code just suddenly stops running, I set two breakpoints at the first and second Appendchild line and the first is triggered but the second isn't...


It is difficult to tell exactly what makes this code need Invoke/BeginInvoke - it looks like the Dropaudio method is just an event handler (on the GUI thread).

Perhaps the easiest thing to do is to use a BackgroundWorker, which is designed to make simple multi-threading easier.

class MyClass
{
    BackgroundWorker bgWorker = new BackgroundWorker();

    public MyClass()
    {
        bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
        bgWorker.ProgressChanged += 
            new ProgressChangedEventHandler(bgWorker_ProgressChanged);
        bgWorker.WorkerReportsProgress = true;
        this.Drop += new DragEventHandler(Dropaudio);
    }

    private void Dropaudio(object sender, System.Windows.DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            string[] droppedFilePaths = 
                e.Data.GetData(DataFormats.FileDrop, true) as string[];
            List<string> Jobs = new List<string>(droppedFilePaths);
            bgWorker.RunWorkerAsync(Jobs);
        }
    }

    void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        if (e.ProgressPercentage == 0)
        {
            Addingcues.Visibility = Visibility.Visible;
        }
        addcuepath.Text = e.UserState.ToString;
    }

    void bgWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        string[] Jobs = e.Argument as string[];
        bgWorker.ReportProgress(0, "Processing Data");
        double count = 0;
        double total = Jobs.Count;
        foreach (string droppedFilePath in Jobs)
        {                
            if (System.IO.Path.GetExtension(droppedFilePath) == ".mp3" || 
                System.IO.Path.GetExtension(droppedFilePath) == ".wav" || 
                System.IO.Path.GetExtension(droppedFilePath) == ".flac")
            {
                double pct = count / total;
                // Report this file
                bgWorker.ReportProgress((int) (pct * 100), droppedFilePath);
                var provider = (XmlDataProvider)this.Resources["CUEData"];
                XmlDocument xmlcuelijst = provider.Document;
                // Do other stuff from above
                count += 1;
            }
        }

    }

    void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Addingcues.Visibility = Visibility.Hidden;
    }       
}

Note that you could easily add a ProgressBar when using the ProgressChanged event. I'm assuming that "Addingcues" is the control you want visible during the processing, and "addcuePath" is the textbox (TextBlock?) that you want updated with the progress.


Instead of doing a BeginInvoke, which starts the Action on an asynchronous thread and continues processing, use an Invoke call, which executes synchronously on the same thread (i.e. waits to finish processing before moving on).

Jon Skeet explains it better in this post than I could.

0

精彩评论

暂无评论...
验证码 换一张
取 消