长时间运行WCF服务的WPF应用程序中的进度条
问题描述:
我有一个长时间运行的WCF服务和一个通过WPF使用它的客户端。使用一个进度条通知完成百分比的客户特定进程AM(WCF中的方法:我需要能够显示基于循环计数器在服务的百分比)长时间运行WCF服务的WPF应用程序中的进度条
我有使用Background Worker显示进度百分比,但它不能正确显示进度。 (只显示0和100而不显示值)在DEBUG模式下一切正常,但不在RELEASE模式下! (进度条在DEBUG模式下按顺序更新)
我试过使用callbacks/wsDualHttpBinding,但在为所有客户端获取这些信息时遇到了一些困难。所以,不得不放弃这个选项。
在异步工作/待机
我用Google搜索了不少的联系,但没有与我的问题有所帮助。
请指导我如何从WCF服务尚未完成的方法中获取当前/运行值,以便我可以根据此值填充进度栏百分比。 (在值之间)
P.S:WCF服务使用的wsHttpBinding下面示例代码:
public Progress()
{
// Start the BackgroundWorker.
myBGWorker.WorkerReportsProgress = true;
myBGWorker.WorkerSupportsCancellation = false;
myBGWorker.DoWork += myBGWorker_DoWork;
myBGWorker.ProgressChanged += myBGWorker_ProgressChanged;
}
public void ShowProgress()
{
myBGWorker.RunWorkerAsync();
}
private void myBGWorker_DoWork(object sender, DoWorkEventArgs e)
{
// fetches a static value from the service
string value = _client.Progress();
int p=0;
for (int i = 1; i <= 100; i++)
{
// Report progress.
p = Convert.ToInt32(_client.Progress());
_logger.Debug("Progress5:" + p.ToString());
myBGWorker.ReportProgress(p, i);
}
}
private void myBGWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.Dispatcher.BeginInvoke(new Action(delegate
{
progressBar1.Value = e.ProgressPercentage;
}), DispatcherPriority.ContextIdle);
}
答
只是为了了解它如何实现。这是我工作代码的一部分。 的.xaml文件:
<ProgressBar x:Name="ProgressBarCompare" HorizontalAlignment="Left" Height="20" Margin="10,157,0,0" VerticalAlignment="Top" Width="321"/>
功能的过程:
private async void btnCompare_Click(object sender, RoutedEventArgs e)
{
ProgressBarCompare.Value = 0;
lblCompare.Content = "";
List<string> list1= (List<string>)Application.Current.Properties["list1"];
List<string> list2= (List<string>)Application.Current.Properties["list2"];
List<Result> output = new List<Result>();
List<Result> passed = new List<Result>();
int topCount = emailList.Count;
int currentItem = 0;
int topBound = topCount - 1;
while (currentItem < topCount)
{
var hash = await CheckOperation(list1[currentItem]); // this line perform progress bar to be filled
var result = list2.Contains(hash);
//some operations
if (Convert.ToInt32(Math.Ceiling(100d * currentItem/topBound)) < 51)
{
Style style = this.FindResource("LabelTemplateNotFilled") as Style;
lblCompare.Style = style;
}
else
{
Style style = this.FindResource("LabelTemplateFilled") as Style;
lblCompare.Style = style;
}
ProgressBarCompare.Value = Convert.ToInt32(Math.Ceiling(100d * currentItem/topBound));
lblCompare.Content = Convert.ToInt32(Math.Ceiling(100d * currentItem/topBound)) + "%";
currentItem++;
}
lblCompare.Content = "COMPLETE";
}
和核心功能是:
private async Task<string> CheckOperation(string input)
{
var result = "";
await Task.Run(() =>
{
//perform some code
});
return result;
}
谢谢SouXin。我的问题在于将服务的价值提取到用户界面。它不绑定UI中的进度数据。我想了解如何在不使用回调/双工绑定的情况下向客户端发送进度数据的方式。使用wsHttpBinding,我如何从客户端代理中获取函数服务调用的值之间的值?尽管现在使用后台工作,但它在发布模式下不返回值,虽然它在DEBUG模式下工作正常。我不太确定这是怎么发生的。 – kani