Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Inside my SchedulerService WCF service, I have a method as follows:
public void Process(bool isForced)
{
    var prevStatus = Status;
    Status = SchedulerStatus.Processing;
    ProcessListings();
    Status = prevStatus;
}

Now I have a view model that polls the Status property, and all is good except with the above method. ProcessListings is long-running, and it blocks the thread so that my status poll calls are blocked until ProcessListings returns. This is problematic, as I need to know if the status is "Processing" after calling Process, and I need to continue polling the status while ProcessListings is running.

How can I use the new asynchrony features, or older ThreadPool features, to execute ProcessListings without blocking calls to other methods in SchedulerService? The polling is an essential workaround as I do not have any more time to try and get WCF callbacks working into a WPF project.
Posted
Updated 3-Mar-15 22:57pm
v3

If you can change the ProcessListings method to do its work asynchronously, and return a Task, then this would be simple:
C#
public async Task ProcessAsync(bool isForced)
{
    var prevStatus = Status;
    Status = SchedulerStatus.Processing;
    await ProcessListingsAsync();
    Status = prevStatus;
}


If you can't change the ProcessListings method, then you'll need to run the method on a background thread - the simplest option would be to use Task.Run[^]:
C#
public async Task ProcessAsync(bool isForced)
{
    var prevStatus = Status;
    Status = SchedulerStatus.Processing;
    await Task.Run(ProcessListings);
    Status = prevStatus;
}
 
Share this answer
 
Comments
Brady Kelly 4-Mar-15 8:17am    
Thanks Richard. I already figured it out, and feel stupid it's so simple, but I'm up to my ears getting other stuff to work by yesterday, so I never tried anything myself yet. What your answer is short on, and sort of what I was looking for, is how to make ProcessListings async, returning a Task. I'll post my solution just to illustrate my code, but your's is accepted.
Richard Deeming 4-Mar-15 8:18am    
Which is impossible to answer without seeing what ProcessListings is doing. :)
Brady Kelly 4-Mar-15 8:26am    
It actually calls one service to pull data from a WCF web service into the DB, then calls another service to pull data from the DB and push it to a good ol' SOAPy web service. Can you believe people are still using that old tech for brand new systems?
Richard Deeming 4-Mar-15 8:39am    
When you import the service, there's an option to "Generate Asynchronous Operations" hidden under the "Advanced" button.

That will generate traditional APM signatures (Begin... / End... using IAsyncResult). You can then use the Task.Factory.FromAsync method[^] to turn those methods into a Task<T>, which you can use from your async method. (You should chose an overload that takes a beginMethod rather than an IAsyncResult, as Stephen Toub explains[^].)
Brady Kelly 4-Mar-15 8:45am    
Cool, thanks again. The service methods I'm calling in ProcessListings are plain old POCO methods, not MVC, so I can't generate async operations for them. Next version maybe, but everything here is so long running, I feel much safer only having one async method doing all the work synchronously.
I have accepted Richard's solution, because it answered the question well, but I'm just posting mine here to show exactly how I achieved what was needed.

Making ProcessListings was as simple as:
C#
private static Task ProcessListings()
{
    return Task.Run(() => Thread.Sleep(5000));            
}

And then calling it as simple as:
C#
public async void Process(bool isForced)
{
    var prevStatus = Status;
    Status = SchedulerStatus.Processing;
    await ProcessListings();
    Status = prevStatus;
}
 
Share this answer
 
Comments
Richard Deeming 4-Mar-15 8:27am    
I assume that's a simplified version of the ProcessListings method? You'd do better to use:

private static Task ProcessListings()
{
return Task.Delay(5000);
}
Brady Kelly 4-Mar-15 8:30am    
Oooh, thanks. Yes, my version just kludges in a delay so I can read the status when it changes in the UI.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900