none
Parallel processing inside my asp.net console application RRS feed

  • Question

  • I have the following console application inside visual studio 2017, and i am using c# version 7.1. Inside my console application I want to call an async method (called gettingCustomerInfo) in a parallel manner using WhenAll, here is my console application main method:-

    class Program
    {
        static int concurrentrequests = int.Parse(ConfigurationManager.AppSettings["ConcurrentRequests"]);
        SemaphoreSlim throttler = new SemaphoreSlim(initialCount: concurrentrequests);
        int numberofrequests = int.Parse(ConfigurationManager.AppSettings["numberofrequests"].ToString());
        int waitduration = int.Parse(ConfigurationManager.AppSettings["waitdurationmilsc"].ToString());
    static async Task Main(string[] args) 
    {
        Marketing ipfd = new Marketing();
        try
        {
            using (WebClient wc = new WebClient()) // call the PM API to get the account id
            {
                //code goes here...
            }
        }
        catch (Exception e)
        {
        }
        var tasks = ipfd.companies.Select(c => gettingCustomerInfo(c.properties.website.value)).ToList();
        var results = await Task.WhenAll(tasks);}

    And here is the method i am calling :-

    private async Task<ScanInfo> gettingCustomerInfo(string website) { await throttler.WaitAsync(); ScanInfo si = new ScanInfo(); var tasks = ipfd.companies.Select(c => gettingCustomerInfo(c.properties.website.value)).ToList(); var results = await Task.WhenAll(tasks);

    //code goes here...


    But i am getting these exceptions:-

    1) Program does not contain a static 'Main' method suitable for an entry point

    2) An object reference is required for the non-static field, method, or property 'Program.gettingCustomerInfo(string)'

    so can anyone advice on this please?





    • Edited by johnjohn11 Friday, November 30, 2018 3:26 AM
    Friday, November 30, 2018 3:23 AM

All replies

  • 1) Change your Main method back to this:

    static void Main(string[] args)

    2) Add "static" keyword to your method:

    private static async Task<ScanInfo> gettingCustomerInfo(string website)

    Friday, November 30, 2018 3:44 AM
    Answerer
  • Thanks for the reply.. here are my comments

    1) Change your Main method back to this:

    static void Main(string[] args)

    if i change the method as you mentioned then i will get this error:-

    The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.	

    on this line of code:-

    var results = await Task.WhenAll(tasks);



    2) Add "static" keyword to your method:

    private static async Task<ScanInfo> gettingCustomerInfo(string website)

    ok this fixed the error i use to get ""An object reference is required for the non-static field, method, or property 'Program.gettingCustomerInfo(string)'

    Saturday, December 1, 2018 1:04 AM
  • You don't use "await" keyword on your Main() method.

    Just make it something like this if you need to access the result later in code:

                var results = Task.WhenAll(tasks);
                Console.WriteLine(((Task<ScanInfo>)tasks[0]).Result.PropertyName);

    Monday, December 3, 2018 1:30 AM
    Answerer
  • Hi johnjohn11,

    Here is a complete sample about how to Task.WhenAll for your reference.

    class Program
        {
    
            static void Main(string[] args)
            {
                Task<string[]> t = AsynchronousProcessing();
    
                t.Wait();
    
                foreach (var item in t.Result)
                {
                    Console.WriteLine(item);
                }
    
                Console.ReadKey();
            }
    
    
            async static Task<string[]> AsynchronousProcessing()
            {
                Task<string> t1 = GetInfoAsync("Task 1", 3);
                Task<string> t2 = GetInfoAsync("Task 2", 5);
                string[] results = await Task.WhenAll(t1, t2);
                return results;
            }
    
            async static Task<string> GetInfoAsync(string name, int seconds)
            {
                await Task.Delay(TimeSpan.FromSeconds(seconds));
                return string.Format("Task {0} is running on a thread id {1}. Is thread pool thread: {2}",
                name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
            }
        }

    Best regards,

    Zhanglong 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 3, 2018 3:06 AM
    Moderator