0

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[] 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) 
{ 
    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...

internetmw
  • 679
  • 4
  • 13
  • 33

2 Answers2

1

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.

Community
  • 1
  • 1
Wonko the Sane
  • 10,226
  • 7
  • 59
  • 86
  • Looking at your code above - is the Dropaudio method just the event handler? And if so, what makes this multithreaded? – Wonko the Sane Aug 17 '10 at 19:02
  • it's just the event handler, I tried your solution above but I don't want it in a seperate class. Now I just declare a new background worker and apply your code in the drop eventhandler, the grid shows up but then nothing happens – internetmw Aug 17 '10 at 20:12
  • The example above isn't a separate class - I was just showing that in your class (which I just called MyClass, because I don't know the name of your actual class), you need to declare a BackgroundWorker, and wire up the events in your constructor. Did you add the events? And if so, you did replace the comment line that says "// Do other stuff from above" with the rest of the XML processing code, right? – Wonko the Sane Aug 17 '10 at 21:05
  • Hi, yes you I now have the code setup right, but the weird thing is, somewhere in the xml processing, the code just suddenly stops executing. I set two breakpoints, the first is reached but the second is not... – internetmw Aug 18 '10 at 11:11
  • Do you have any kind of exception handling, either around this code, or a global UnhandledException handler? If so, check there; if not, and you need more help, it would be better to ask a new question to start a thread based on that question. – Wonko the Sane Aug 18 '10 at 12:53
  • Hold on: The calling thread cannot access this object because a different thread owns it. – internetmw Aug 18 '10 at 13:50
  • Is the exception outside the BackgroundWorker? If so, this makes sense - something in the code running inside the BackgroundWorker (i.e. the code in DoWork, running on a different thread) is probably throwing an exception. Put an exception handler inside there, and/or do some step through debugging to find what is failing. – Wonko the Sane Aug 18 '10 at 14:18
  • No, the exception is inside the if statement within the DoWork part of the backgroundworker. I'll look into it. – internetmw Aug 18 '10 at 18:37
  • One comment from looking at your updated code. You probably want to move the hooks to the event from the DropAudio event handler to your constructor. Otherwise, if you drop files twice, you'll be hooking up the event twice, doing twice as much work. – Wonko the Sane Aug 18 '10 at 18:54
1

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.

Wonko the Sane
  • 10,226
  • 7
  • 59
  • 86