C#WFP不确定动画的进度条

时间:2018-06-21 20:13:45

标签: c# wpf multithreading asynchronous progress-bar

我对C#WPF还是很陌生,需要帮助,以便在我的主要代码继续运行的同时使不确定的进度条动画化。我已经研究并尝试了许多不同的线程方法和后台工作人员等,但是似乎没有办法解决问题。下面的代码是最接近的代码,实际上我可以看到进度条开始动画,并且我的主代码继续运行,但是随后出现异常“调用线程无法访问该对象,因为另一个线程拥有它。 ”我非常感激我如何实现这一目标!

 public partial class MainWindow : Window
{

    public MainWindow()
    {
       InitializeComponent();
    }

    [STAThread]
    private async void cmsServerConnect(object sender, RoutedEventArgs e)
    {
        Login form = new Login();
        form.ShowDialog();

        if (form.cancelClicked == true && form.enter_Pressed == false)
        {
            return;
        }

        //Create new thread to have indeterminate progress bar run on
        //Set new thread appartmentstate to STA
        //Start thread
        Connect_Progress progress = new Connect_Progress();
        Thread t = new Thread(() => progress.Show());
        t.SetApartmentState(ApartmentState.STA);
        t.Start();

        //Using  async at method declaration
        //await is allowing the UI to continue updating while running the task connection.cmsConnect()
        Ribbon_Buttons connection = new Ribbon_Buttons();
        int connected = await Task.Run(() => connection.cmsConnect(form.id, form.Pword));

        if (connected == 2)
        {
            BitmapImage image = new BitmapImage();
            image.BeginInit();
                image.UriSource = new Uri("pack://application:,,,/Images/Circle_Green.png");
            image.EndInit();

            this.Ribbon_Button_ACM12_Connection.SmallImageSource = image;
            this.Ribbon_Button_ACM12_Connection.Label = "Connected";
        }

        t.Abort();

        this.Activate();
        this.Topmost = true;
        this.Topmost = false;
        this.Focus();
    }

1 个答案:

答案 0 :(得分:0)

您应该使用后台工作程序

xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <ProgressBar x:Name="myProgressBar" Width="400" Grid.Row="0" Height="30" />
    <Button x:Name="myButton" Width="200" Height="30" Grid.Row="1" Click="myButton_Click" Content="Click Me" />
</Grid>

c#

 private readonly BackgroundWorker worker = new BackgroundWorker();

    public MainWindow()
    {
        InitializeComponent();
        worker.DoWork += worker_DoWork;
        worker.RunWorkerCompleted += worker_RunWorkerCompleted;
    }
    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {
            Dispatcher.BeginInvoke((Action)(() =>
            {
                myProgressBar.Value = i;
            }));
            Thread.Sleep(100);
        }
    }

    private void worker_RunWorkerCompleted(object sender,
                                           RunWorkerCompletedEventArgs e)
    {
        myProgressBar.Value = 100;
    }
    private void myButton_Click(object sender, RoutedEventArgs e)
    {
        worker.RunWorkerAsync();
    }
}