我想为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;
请安排建议我实现这一点,任何其他指示都会有所帮助。
答案 0 :(得分: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);
}
}
}
答案 1 :(得分:0)
最后,我通过做一些技巧实现了这一目标。我已经使用了DependencyProperty和Dispatacher的委托更新了Progressbar,经过多次尝试,这对我有用,看看我在下面做的代码
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,直到我收到一个函数的响应,任何其他改进这种方式的建议都会很受欢迎。 欢迎任何建议和帮助,以改善这一点。
答案 2 :(得分: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命名空间用TaskSchedulers创建Tasks而不是直接创建线程,但原理是相同的。