c# async method without a real await supported method called within it.

Have been searching for this since a long time. Finally found it:

http://stackoverflow.com/questions/15522900/how-to-safely-call-an-async-method-in-c-sharp-without-await

public async static Task<int> GetTwitterResultsCountAsync(string itemLink)
        {
            int returnCount = await Task.Run<int>(() =>
            {
                int count = 0;
 
                try
                {
                    count = (from search in TwitterLog.TwitterContext.Search
                             where search.Type == SearchType.Search &&
                                   search.Query == itemLink
                             select search).Count();
 
                    return count;
                }
                catch
                {
                    return count;
                }
            });
 
            return returnCount;
        }

Just a note - these methods work fine when called from a asp.net page. However, if you call it from a web service method even if you use .Result to call it synchronously, it does not work for some reason.

Comments

Popular posts from this blog

Tutorial: Using Google Cloud Storage from C# and .NET

Late 2008 Macbook only giving 1.5 gb/s speed with 6 gb/s Intel SSD?

Enable Visual Studio to use more than 2GB of memory