MSDN > 論壇首頁 > Windows Presentation Foundation (WPF) > System.InvalidOperationException
發問發問
 

已答覆System.InvalidOperationException

  • Thursday, 8 November, 2007 17:34Aart-Jan 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    I keep getting this error System.InvalidOperationException on the first line of the showInterface function:

    progressbar_canvas.Visibility = Visibility.Collapsed;

     This is my code:


    Code Block

    using System;

    using System.Diagnostics;

    using System.IO;

    using System.Net;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Data;

    using System.Windows.Media;

    using System.Windows.Media.Animation;

    using System.Windows.Navigation;

    using Microsoft.Win32;

     

    namespace DialogTest1

    {

        public partial class Window1

        {

            public Window1()

            {

                this.InitializeComponent();

     

                // Insert code required on object creation below this point.

                newDate.Text = DateTime.Now.ToString("yyyy:MM:dd hh:mm:ss");

            }

     

            private void WindowLauncher(object sender, RoutedEventArgs e)

            {

                OpenFileDialog ofd = new OpenFileDialog();

                //ofd.Multiselect = true;

                ofd.Filter = "Afbeeldingen (*.jpg, *.jpeg)|*.jpg*;*.jpeg|Alle bestanden|*.*";

                if (ofd.ShowDialog() == true)

                {

     

                    string filePath = ofd.FileName;

                    string safeFilePath = ofd.SafeFileName;

                    string filePath2 = filePath.Substring(0, filePath.LastIndexOf("\\"));

                    photoDir.Text = filePath2;

                }

            }

     

            private void showInterface()

            {

                progressbar_canvas.Visibility = Visibility.Collapsed;

                heart_design.Visibility = Visibility.Visible;

            }

     

            private void setPhotoDate(object sender, RoutedEventArgs e)

            {

                progressbar_canvas.Visibility = Visibility.Visible;

                heart_design.Visibility = Visibility.Collapsed;

                Process exiftool = new Process();

                exiftool.StartInfo.FileName = "C:\\exiftool.exe";

                exiftool.StartInfo.Arguments = "-overwrite_original -AllDates=\"" + newDate.Text + "\" \"" + photoDir.Text + "\"";

                exiftool.StartInfo.CreateNoWindow = true;

                exiftool.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                exiftool.EnableRaisingEvents = true;

                exiftool.Exited += new System.EventHandler(this.exiftool_Exited);

                exiftool.Start();

     

     

            }

     

            private void exiftool_Exited(object sender, System.EventArgs e)

            {

                MessageBox.Show("Exited");

                try

                {

                    this.showInterface();

                }

                catch (Exception ex)

                {

                    MessageBox.Show("oException" + ex.Message + "\n helplink:" + ex.HelpLink + "\n" + ex.StackTrace + "\n type:" + ex.GetType().ToString());

                }

     

     

     

            }

     

            private void closeWindow(object sender, RoutedEventArgs e)

            {

                this.Close();

            }

        }

    }




     I'm new to c# and I've Googled this problem, but I can't seem to figure out what's wrong.

    Any help?
     

     

     

     

解答

  • Thursday, 8 November, 2007 17:40Drew Marsh版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    It would be useful to include the full stacktrace of the exception in the future. My guess however is that

    the event is being fired from a thread other than the UI/Dispatcher thread and therefore you will need to marshal the call back to the Dispatcher thread using this.Dispatcher.Invoke.

     

    You can check this out really quick by calling this.CheckAccess() in the Exited event handler. If it's false then my guess is right, if it's true then we will need the details of the exception to figure out exactly what is wrong.

     

    HTH,
    Drew

所有回覆

  • Thursday, 8 November, 2007 17:40Drew Marsh版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    It would be useful to include the full stacktrace of the exception in the future. My guess however is that

    the event is being fired from a thread other than the UI/Dispatcher thread and therefore you will need to marshal the call back to the Dispatcher thread using this.Dispatcher.Invoke.

     

    You can check this out really quick by calling this.CheckAccess() in the Exited event handler. If it's false then my guess is right, if it's true then we will need the details of the exception to figure out exactly what is wrong.

     

    HTH,
    Drew

  • Thursday, 8 November, 2007 17:55Aart-Jan 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    You're were right indeed! Thanks for looking into it.
    It works now. Smile