tags:

views:

85

answers:

3

Hi all, 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" 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;            
    }
A: 

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.

Josh Einstein
NotificationReceivedFromService is a callback.There isnt any threading going on because I dont know how to implement it and I wanted some pointers or quick and dirty code snippet how to hook the dispatecher.The order is vital!.I must notify the UI eg(Step1 ok,Step2 ok etc...)In the real world there will be many request at the same time but each request must have its own Order(Step1 Started,Step1Completed etc..)Again,need to implement threading but need a starting point.How do you hook dispather to method "Buy" which triggers the all process?Thanks a lot
I would suggest not using the Dispatcher directly in this case. I would recommend BackgroundWorker. http://msdn.microsoft.com/en-us/library/8xs8549b.aspx
Josh Einstein
Any specific reason? Just so that I understand your reasoning
Wow sorry for the delayed response. I missed your comment. The reason I recommended BackgroundWorker is because it hides the complexities of doing background work and posting periodic notifications to the UI thread pretty nicely. Using Dispatcher only gets you onto the foreground thread and still leaves it up to you to get onto the background thread.
Josh Einstein
A: 
Dabblernl
A: 

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?

Phil Bennett