希望你能帮上忙。
场景 WPF 应用程序调用 wcf 服务来购买产品等... 客户可以在 UI 中选择 20 种产品并按下购买,现在对于每个处理过的产品,我需要报告状态 EG(“确定”、“缺货”、“忙碌”等...) 我有一个绑定(bind)到 BuyCommand 的 txtReportProgress。
问题 尽管一切似乎都正常,而且我收到了每个处理过的产品的通知,但它完全不同步,实际上并没有按正确的顺序报告步骤。 我认为这与我没有实现线程化但我不知道如何实现这一事实有关。 我读过我可以使用“Dispatcher”i/o background worker 但不能让它工作。 Noddy 示例在这里给你一个想法。
你能帮我指点一下吗?
---CustomerViewModel
public ICommand BuyCommand
{
get
{
if (BuyCommand == null)
{
BuyCommand = new RelayCommand(param => Buy(), param => CanBuy);
}
return BuyCommand;
}
}
private void Buy()
{
ReportProgress("Buying Progress is starting");
ProductStatus status=myService.IsProductAvailable();
myService.ProcessProducts(); //For each product that is processed service does a callback and NotificationReceivedfromService is fired.THIS WORKS!!
ReportProgress(status);
var result =DoSomethingElse();
ReportProgress(result);
}
void NotificationReceivedFromService(object sender, StatusEventArg e)
{
//update the UI
ReportProgress(e.Message);
}
static bool CanBuy
{
get
{
return true;
}
}
public string CustomerLog
{
get { return _customerModel.CustomerLog; }
set
{
_customerModel.CustomerLog = value;
base.OnPropertyChanged("CustomerLog");
}
}
internal void ReportProgress(string text)
{
CustomerLog += text + Environment.NewLine;
}
最佳答案
什么是 WCF 通信 channel ?您是否并行向服务发出多个请求?我真的无法从您的代码中看出哪里有任何多线程正在进行,但我假设 NotificationReceivedFromService 是某种回调?
无论如何,如果您使用诸如 Dispatcher.BeginInvoke 之类的东西,只要它们都具有相同的调度程序优先级,您的调用就应该按照它们的顺序执行。
但是,如果您向服务发出多个并行请求,则无法真正控制它们完成的顺序。您需要提出一个请求,然后在第一个请求完成时提出下一个请求,等等。或者您可以更改您的设计,使其不依赖于操作顺序。
关于c# - 需要 WPF 线程帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2346666/