0

我想为 WPF 应用程序中的连续进度条分配一个新线程,以便在单击 UI 按钮后它将连续运行,直到我收到服务响应。

我做了如下代码,但进度条(MclarenServerCecksProgressBar)似乎根本不起作用

MclarenServerCecksProgressBar.Visibility = Visibility.Visible;
var uiThread = new Thread(() =>
{
progressBarDisptacher = MclarenServerCecksProgressBar.Dispatcher;


// allowing the main UI thread to proceed 
System.Windows.Threading.Dispatcher.Run();    
});
uiThread.SetApartmentState(ApartmentState.STA);
uiThread.IsBackground = true;
uiThread.Start();      
string[] servicesss = getServices(servername,server);
DataTable dtd = esd.getListOfServices(servername, usrid.Text.ToString(),   
userpass.Password.ToString(), servicesss);
MclarenServerCecksProgressBar.Visibility = Visibility.Hidden;

请安排建议我实现这一目标,任何其他指针都会有很大帮助。

4

3 回答 3

0

WPF UI 只有一个可用于通信的 UI 线程 - 调度程序线程。只要与 UI 对象交互的调用是通过 UI 调度程序完成的,您的所有后台任务都可以在您喜欢的任何线程上完成。通过此调度程序发送调用是一项常见任务,因此每个 WPF 对象都提供了 Dispatcher 属性。

我会这样处理问题:

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        // Currently running on the WPF thread so the WPF property can be set directly
        this.progressBar1.Visibility = System.Windows.Visibility.Visible;

        // Create a new thread to do the long running task
        Thread worker = new Thread(() =>
        {
            WebService.DoSomethingThatTakesAges();

            // When the above call has completed, update the UI on its dispatcher thread
            this.progressBar1.Dispatcher.BeginInvoke(new Action(() =>
                {
                    this.progressBar1.Visibility = System.Windows.Visibility.Hidden;
                }));
        });

        worker.Start();
    }

使用 System.Threading.Tasks 命名空间使用 TaskScheduler 创建 Task 可能比直接创建线程更好,但原理是一样的。

于 2013-03-30T05:08:05.430 回答
0

最后,我通过一些技巧实现了这一点。我已经使用委托与 DependencyProperty 和 Dispatacher 来更新进度条,经过很长时间的各种尝试后,这对我有用的任何方式,请参阅下面我所做的代码

private delegate void UpdateProgressBarDelegate(
    System.Windows.DependencyProperty dp, Object value);

private void startProgessBar(string controlname)
{
    MclarenServerCecksProgressBar.Visibility = Visibility.Visible;


    //Stores the value of the ProgressBar

    //Create a new instance of our ProgressBar Delegate that points
    // to the ProgressBar's SetValue method.
    UpdateProgressBarDelegate updatePbDelegate =
        new UpdateProgressBarDelegate(MclarenServerCecksProgressBar.SetValue);
    bool _status = true;
    int flag = 0;
    double value = 0;

    do
    {

        /*Update the Value of the ProgressBar: */

        Dispatcher.Invoke(updatePbDelegate,
            System.Windows.Threading.DispatcherPriority.Background,
            new object[] { System.Windows.Controls.ProgressBar.ValueProperty, value });


                if (flag == 0)
                {
                    flag == 1
                    _status = processesServices(controlname);
                }
                if (_status == false)
                {
                    MclarenServerCecksProgressBar.Visibility = Visibility.Hidden;
                }
    }
    while (_status);
}

无论如何,修复它可能很脏,我一直在寻找运行 progessbar,直到我收到一个函数的响应,任何其他改进这种方式的建议都会非常受欢迎。欢迎提供任何建议和帮助来改进这一点。

于 2013-03-29T23:19:32.887 回答
0

我不明白你的代码进度条值集如何。另外,我不太明白您为什么要对线程使用这种黑客攻击。如果我理解你的问题,我举了一个例子,我是如何用进度条做到这一点的。

<Window x:Class="WpfApplication5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    Height="350"
    Width="525">
<Grid>
    <ProgressBar Height="10"
             HorizontalAlignment="Left"
             Margin="12,12,0,0"
             Name="progressBar1"
             VerticalAlignment="Top"
             Width="100"
             Visibility="Hidden" />
    <Button Content="Button"
        Height="23"
        HorizontalAlignment="Left"
        Margin="30,54,0,0"
        Name="button1"
        VerticalAlignment="Top"
        Width="75"
        Click="button1_Click" />
</Grid>
</Window>

using System;
using System.Threading;
using System.Windows;

namespace WpfApplication5 {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }

        void button1_Click(object sender, RoutedEventArgs e) {
            progressBar1.Visibility = Visibility.Visible;
            Action method = () => {
                Action<int> method2 = (val) => {
                    progressBar1.Value = val;
                };
                for (int i = 0 ; i < 10 ; i++) {
                    Thread.Sleep(500);
                    Dispatcher.BeginInvoke(method2, i * 10);
                }
                Action method3 = () => {
                    progressBar1.Visibility = Visibility.Hidden;
                };
                Dispatcher.BeginInvoke(method3);
            };
            method.BeginInvoke(null, null);
        }
    }
}
于 2013-03-29T07:37:13.093 回答