0

I am trying to get events to occur one at a time within my button_Click, but for some reason everything seems to wait to run until everything is complete. For instance image changes don't occur until everything else has occured.

private void button1_Click(object sender, EventArgs e)
    {
            pictureBox1.Image = Properties.Resources.running;
            iTunesAppClass itunes = new iTunesAppClass();
            IITLibraryPlaylist mainLibrary = itunes.LibraryPlaylist;
            IITTrackCollection ittracks = mainLibrary.Tracks;


            List<string> files = new List<string>();
            List<string> musicfiles = new List<string>();
            List<string> badfiles = new List<string>();
            List<string> songlist = new List<string>();
            int state = ((int)itunes.PlayerState);

            string statestring = null;
            if (state < 1) { statestring = "Stopped"; };
            if (state == 1) { statestring = "Playing"; };

            if (state == 1)
            {
                do
                {
                    label5.Text = ("Pausing iTunes to maintain file integrity");
                    itunes.Pause();
                    state = (int)itunes.PlayerState;
                }
                while (state == 1);
            }
            if (state < 1) { statestring = "Stopped"; };
            if (state == 1) { statestring = "Playing"; };
            if (state != 1)
            {
                label5.Text = "Itunes is " + statestring;
            }

            string[] extensions = { "*.mp3", "*.mp4", "*.m4a", "*.m4v", "*.m4p", "*.m4b", "*.flac" };
            string filepath = label4.Text;
            foreach (string extension in extensions)
            {
                this.pictureBox1.Image = Properties.Resources.running;
                try
                {
                    files.AddRange(Directory.GetFiles(filepath, extension, SearchOption.AllDirectories));
                }
                catch (UnauthorizedAccessException) { }
            }

            foreach (string file in files)
            {
                try { string taglibfile = TagLib.File.Create(file).Tag.Title; musicfiles.Add(file); Console.WriteLine(taglibfile); }
                catch { badfiles.Add(file); }
            }
            XDocument baddoc = new XDocument
            (new XElement("Corrupt",
               badfiles.Select(badfile =>
               new XElement("File", badfile))));
            baddoc.Save(label4.Text + "\\badfiles.xml");
            // foreach(string musicfile in musicfiles)
            //{ String Title = (TagLib.File.Create(musicfile).Tag.Title); }
            this.pictureBox1.Image = Properties.Resources.skinitunes;
    }
TankCR
  • 61
  • 1
  • 7
  • Try using Application.DoEvents() after you change the picture. –  May 03 '14 at 03:35
  • ok, so the image changed but the spinning doesn't occur until it is done running the rest of the events, it just sits on the first frame of the gif. – TankCR May 03 '14 at 03:48
  • I suggest you to use a BackgroundWorker if you are doing heavy stuff on the UI thread. You can set your spinner, start the backgroundworker and listen when it finishes so you can remove the spinner. Read this answer, it shows a quick example of a backgroundworker http://stackoverflow.com/questions/14542466/updating-gui-from-other-thread/14542541#14542541 –  May 03 '14 at 03:55

0 Answers0