0

I have modified code, but now I have another problem. The InvalidOperation exception occurs inside if statement on validating user info. It says that the calling thread cannot access this object because a different thread owns it.Any sugestions?

 private void finishConfigButton_Click(object sender, RoutedEventArgs e)
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.WorkerSupportsCancellation = true;
        bool validated = false;

        errorLabel.Visibility = System.Windows.Visibility.Collapsed;
        validationProfile.IsBusy = true;
        finishConfigButton.IsEnabled = false;
        backToLoginHyperlink.IsEnabled = false;

        worker.DoWork += (o, ea) =>
        {
            if (newUser.ValidateNewUserInformation(newNameTextBox.Text, newEmailTextBox.Text, newUsernameTextBox.Text, newPasswordPasswordBox.Password, ref errorLabel))
            {
                validated = true;

                string activeDir = Environment.SystemDirectory.Substring(0, 1) + @":\Users\" + Environment.UserName + @"\My Documents\SSK\Users";
                string newPath = System.IO.Path.Combine(activeDir, newUser.Username);
                Directory.CreateDirectory(newPath);

                newUser.SaveUserData(newUser);

                newPath = System.IO.Path.Combine(activeDir, newUser.Username + @"\Settings");
                Directory.CreateDirectory(newPath);

                newUserSettings.SetDefaultValues();
                newUserSettings.SaveSettings(newUser, newUserSettings);
            }
            else
                validated = false;

            if (worker.CancellationPending)
            {
                ea.Cancel = true;
                return;
            }
        };

        worker.RunWorkerCompleted += (o, ea) =>
        {
            validationProfile.IsBusy = false;
            finishConfigButton.IsEnabled = true;
            backToLoginHyperlink.IsEnabled = true;
        };

        worker.RunWorkerAsync(this);

        if (validated)
        {
            IntelliMonitorWindow intelliMonitor = new IntelliMonitorWindow(newUser, newUserSettings);
            intelliMonitor.Show();
            this.Close();
        }
        else
            errorLabel.Visibility = System.Windows.Visibility.Visible;
    }
4

4 回答 4

1

XAML 代码中的忙碌指示符没有任何内容。将一些控件放入其中:

<wpfet:BusyIndicator Name="validationProfile" IsBusy="False" BusyContent="Working...Please wait"  DisplayAfter="0" Background="DimGray">
    <Grid>
        ...
    </Grid>
</wpfet:BusyIndicator>

如果您更改为忙,这些控件将被禁用,并且 BusyIndi​​cator 将出现在它们上方。

我想你想<Grid Background="LightGray">用 BusyIndi​​cator 包裹整个。

于 2012-03-12T10:11:58.150 回答
1

您在这里所做的是在 UI 线程上运行所有内容。这意味着在运行繁重的代码时,您会阻止 UI 重新绘制,因此 validationProfile 直到方法结束时才会更新,其中 IsBusy 设置为 false。

您需要做的就是将繁重的代码处理成一个新线程,该线程可以同时更新 UI。

看看扩展工具包的创建者 Brian Lagunas 写的这篇博文:http: //elegantcode.com/2011/10/07/extended-wpf-toolkitusing-the-busyindicator/

他解释了如何将 BusyIndi​​cator 与 BackgroundWorker 一起使用。

于 2012-03-12T09:35:15.937 回答
0

使用后台工作者或新线程来运行繁重的进程并释放 UI 线程。即使后台进程正在运行,这也可以帮助您更新 UI

例如:

public void finishConfigButton_Click()
{
    worker = new BackgroundWorker();

    worker.DoWork += delegate(object s, DoWorkEventArgs args)
    {
        //Do the heavy work here
    };

    worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
    {
        //Things to do after the execution of heavy work
        validationProfile.IsBusy = false;
    };

    validationProfile.IsBusy= true;
    worker.RunWorkerAsync();
    }
}
于 2012-03-12T09:51:51.667 回答
0

我终于弄明白了。您不能在 worker.DoWork 块中使用 UI 对象。我稍微修改了代码,它现在可以工作了。

 private void finishConfigButton_Click(object sender, RoutedEventArgs e)
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.WorkerSupportsCancellation = true;

        errorLabel.Visibility = System.Windows.Visibility.Collapsed;
        validationProfile.IsBusy = true;
        finishConfigButton.IsEnabled = false;
        backToLoginHyperlink.IsEnabled = false;

        bool validated = false;
        string newName = newNameTextBox.Text;
        string newEmail = newEmailTextBox.Text;
        string newUsername = newUsernameTextBox.Text;
        string newPassword = newPasswordPasswordBox.Password;
        string errorMessage = "Unknown error.";

        worker.DoWork += (o, ea) =>
        {
            if (newUser.ValidateNewUserInformation(newName, newEmail, newUsername, newPassword, ref errorMessage))
            {
                string activeDir = Environment.SystemDirectory.Substring(0, 1) + @":\Users\" + Environment.UserName + @"\My Documents\SSK\Users";
                string newPath = System.IO.Path.Combine(activeDir, newUser.Username);
                Directory.CreateDirectory(newPath);

                newUser.SaveUserData(newUser);

                newPath = System.IO.Path.Combine(activeDir, newUser.Username + @"\Settings");
                Directory.CreateDirectory(newPath);

                newUserSettings.SetDefaultValues();
                newUserSettings.SaveSettings(newUser, newUserSettings);

                validated = true;
            }
            else
                ea.Cancel = true;
        };

        worker.RunWorkerCompleted += (o, ea) =>
        {
            if (validated)
            {
                IntelliMonitorWindow intelliMonitor = new IntelliMonitorWindow(newUser, newUserSettings);
                intelliMonitor.Show();
                this.Close();
            }

            validationProfile.IsBusy = false;
            finishConfigButton.IsEnabled = true;
            backToLoginHyperlink.IsEnabled = true;
            errorLabel.Visibility = System.Windows.Visibility.Visible;
            errorLabel.Content = errorMessage;
        };

        worker.RunWorkerAsync();
    }
于 2012-03-13T10:51:07.253 回答