How to identify pending asynchronous wcf calls?

I am calling the WCF service from a silverlight application. I do this asynchronously and I do not block execution after creating async. call (this means that I do not use the wait-join frm mechanism on this page ). I do not want the thread to block.

However, I would like to find that the WCF call has gone into a waiting state, so that I can show the busy icon in the user interface - a visual message indicating that something is happening behind the user interface.

I can change my code so that I can start animating the Busy icon and stop this animation when the asynchronous call ends.

However, this is a lot of code with a higher gain, and with a large number of calls made throughout the client code, this will only worsen the work.

So, is there any method or property displayed by the wcf service client reference code that can be used to trigger events when any calls to the async wcf service go into standby mode, and also trigger events when all async wcf completes service calls?

+3
source share
1 answer

, , Silverlight WCF. , , , , .

, Silverlight ProgressBar, / - Silverlight:

Page.xaml:

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="100">

    <StackPanel x:Name="LayoutRoot" Background="White">
        <Button x:Name="ButtonDoWork" Content="Do Work"
                Click="ButtonDoWork_Click"
                Height="32" Width="100" Margin="0,20,0,0" />
        <ProgressBar x:Name="ProgressBarWorking"
                     Height="10" Width="200" Margin="0,20,0,0" />
    </StackPanel>
</UserControl>

Page.xaml.cs:

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using SilverlightApplication1.ServiceReference1;

namespace SilverlightApplication1
{
    public partial class Page : UserControl
    {
        public bool IsWorking
        {
            get { return ProgressBarWorking.IsIndeterminate; }
            set { ProgressBarWorking.IsIndeterminate = value; }
        }

        public Page()
        {
            InitializeComponent();
        }

        private void ButtonDoWork_Click(object sender, RoutedEventArgs e)
        {
            Service1Client client = new Service1Client();
            client.DoWorkCompleted += OnClientDoWorkCompleted;
            client.DoWorkAsync();

            this.IsWorking = true;
        }

        private void OnClientDoWorkCompleted(object sender, AsyncCompletedEventArgs e)
        {
            this.IsWorking = false;
        }
    }
}

IsIndeterminate true DoWork :

alt text http://www.freeimagehosting.net/uploads/89620987f0.png

OnClientDoWorkCompleted , , IsIndeterminate false ; ProgressBar, / .

- DoWork, , , 5 :

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Threading;

namespace SilverlightApplication1.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1
    {
        [OperationContract]
        public void DoWork()
        {
            Thread.Sleep(TimeSpan.FromSeconds(5.0));
            return;
        }
    }
}
+4

Source: https://habr.com/ru/post/1703548/


All Articles