WCF - How to delay task for specified amount of time?

199 Views Asked by At

I have WCF WebService for Silverlight client.

Let's say client click "Make building".

Service will receive new task, and star counting time, until it's ready to make action (i.e add to database).

Time - how much time task will need to complete (i.e to construct building).

The point is how to delay task for the certain amount of time.

Also, is there a way to stream time from server to client ? I have setup this:

        [OperationContract]
    public void GetTime()
    {
        foreach (IDuplexClient client in _clientDic.Values)
        {
            client.ShowStatus(DateTime.Now);
        }
    }

        [OperationContract]
    public void Login()
    {
        string clientID = OperationContext.Current.Channel.SessionId;
        IDuplexClient client = OperationContext.Current.GetCallbackChannel<IDuplexClient>();

        _clientDic.Add(clientID, client);
    }

IDuplexClient:

        [OperationContract(IsOneWay = true)]
    void ShowStatus(DateTime status);

And client side:

            _client.LoginAsync();
        _client.GetTimeAsync();
        _client.ShowStatusReceived += new EventHandler<ShowStatusReceivedEventArgs>(_client_ShowStatusReceived);

        void _client_ShowStatusReceived(object sender, ShowStatusReceivedEventArgs e)
    {
        label1.Content = e.status.ToString();
    }

It's working.. For first run. But time doesn't get refreshed, which is not what I want. As well, after few forced refresh in browser, time stop to show at all.

    public partial class MainPage : UserControl
{
    Service1Client _client;
    int time = 10000;
    public MainPage()
    {
        InitializeComponent();
        _client = new Service1Client(new PollingDuplexHttpBinding { DuplexMode = PollingDuplexMode.SingleMessagePerPoll, OpenTimeout = TimeSpan.FromMinutes(10), ReceiveTimeout = TimeSpan.FromMinutes(10) },
new EndpointAddress("http://localhost:44544/Service1.svc"));

        _client.LoginAsync();
        _client.DoWorkCompleted += new EventHandler<DoWorkCompletedEventArgs>(_client_DoWorkCompleted);
        _client.DoWorkAsync();
        _client.AddNewTaskAsync("testTaskzor", time);
        _client.GetTimeAsync();
        //_client.AddNewTaskCompleted += new EventHandler<AddNewTaskCompletedEventArgs>(_client_AddNewTaskCompleted);
        _client.ShowStatusReceived += new EventHandler<ShowStatusReceivedEventArgs>(_client_ShowStatusReceived);

    }

    void _client_ShowStatusReceived(object sender, ShowStatusReceivedEventArgs e)
    {
        label1.Content = e.status.ToString();
    }

    void _client_DoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
    {
        //label1.Content = e.Result;
    }
}

That' entire client code. Although I finally fixed and time is streaming properly to client (it wa surpsingly easy it was enough to enlose foreach with while(true) statment, at least for now).

But on other side. When I close browser, and open it again, nothing show up. As well as after I refresh it, time do not show up at all.

1

There are 1 best solutions below

1
On

The easiest way would be to implement the delay on the client side. You can't really delay a RESTful service like WCF without breaking the model.