开发者

WPF threading help needed

Hopefully you can help.

Scenario WPF Application calling a wcf Service to buy products etc... A customer can select 20 products in the UI and press buy,now for each product that is processed I need to report a status EG("OK","Out of stock","Busy"开发者_如何学C etc...) I have a txtReportProgress that is bind to a BuyCommand.

Problem Even though everything seems to work ok and I receive a notification for each product processed it gets all out sync and not actually reporting the steps in the correct order. I think this is todo with the fact that I have NOT implemented threading but I dont know how to do it. I have read i can use "Dispatcher" i/o background worker but cannot make it work. Noddy example here to give you an idea.

Can you help me and give me some pointers?

    ---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;            
    }


What is the WCF communication channel? Are you making multiple requests to a service in parallel? I can't really see from your code where there is any multi-threading going on at all but I'm assuming that NotificationReceivedFromService is some kind of callback?

Anyhow, if you were using something like Dispatcher.BeginInvoke, your invokes should be executed in the order they were made as long as they all have the same dispatcher priority.

If however you are making multiple parallel requests to the service, you can't really control the order in which they will complete. You'll need to make one request, then make the next when the first completes, etc. Or you can change your design so that it doesn't depend on the order of operations.


As you get callbacks out of sync, obviously some multithreading is going on, perhaps without your realizing it. You are aware that that calls to WCF services can be handled asynchronously by the service itself? Read here for more information. Please elaborate on your question or show us some more code if you need more help.

As to Dispatchers and BackgroundWorkers, these are very different beasts:
Dispatcher.Invoke is used to update the UI from a different thread than the UI thread.
Backgroundworkers encapsulate tasks that run on the background, but conveniently have events that are thrown on the UI thread, thereby making it unnecessary to use the Dispatcher.Invoke call.

You can read more about BackgroundWorkers here


As mentioned already, the CallBack is asynchronous so you will have an ordering problem. can you arrange the call to the service in some way that you can bring back some token or other identifier that will help you report the progress in the correct order? Something you can sort on perhaps?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜