none
Asynchronous method 'anonymous' should not return void RRS feed

  • Question

  • Hello,
    I call an asynchronous function from a synchronous function. It works, but a warning is still issued.
    How do I solve this correctly, not that I'll get in trouble later.

    This is not clear

    Thanks for help.

    Greetings Markus

    protected override void Data(InputA input)
    {
       ScanOrderNo(value);
    
    
    protected async void ScanOrderNo(string value)
    {
      COrder = new Order();
    
      ResponseScada response = null;
      response = await LScada.GetResponseInfosAsync();
    
    public async ResponseScada GetResponseInfosAsync()
    {
    	while (!IsAFileExist(out useFile, out fileName))
    	{
         	await Task.Delay(1000);
    
    	//..
    	} 
    	ResponseScada ret = new ResponseScada ();

    Tuesday, February 4, 2020 5:37 PM

Answers

  • Hello Karen,
    now works well.
    The main problem was Wait().
    case "ID_SCAN_ORDER_NUMBER":
     SettingsScanOrderNo(value).Wait();
     break;

    I have not used Wait(). Why I need this is not really clear to me.
    Is there a simple explanation?

    Greetings Markus

    is ok now

    Wait is a synchronization method that causes the calling thread to wait until the current task has completed. If the current task has not started execution, the Wait method attempts to remove the task from the scheduler and execute it inline on the current thread. If it is unable to do that, or if the current task has already started execution, it blocks the calling thread until the task completes.

    Source


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Markus Freitag Tuesday, February 11, 2020 5:04 PM
    Thursday, February 6, 2020 8:51 PM
    Moderator

All replies

  • Hello,

    Its advisable to not have async void for a asynchronous method but instead return a Task and do nothing. Otherwise exceptions may be swallowed especially without a try-catch.

    public async Task SomeMethod()
    {
        await Task.Delay(1000);
    }
    
    private async void button1_Click(object sender, EventArgs e)
    {
        try
        {
            await SomeMethod();
            MessageBox.Show("Done");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Done with error " + ex.Message);
        }
        
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, February 4, 2020 6:27 PM
    Moderator
  • Hi Markus Freitag,
    As Kareninstructor said, you can use task. And you can access the Result property of the task that will cause your thread to block until the result is available.
    More solutions you can refer to this document.
    [How to call asynchronous method from synchronous method in C#?]
    Best Regards,
    Daniel Zhang


    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.

    Wednesday, February 5, 2020 6:38 AM
  • Hello,
    works not.
    // Here is synchron, I can't change it.
    protected override void Data(InputA input)
    {
       ScanOrderNo(value);
    
    // I call the function.
    protected async void ScanOrderNo(string value)
    {
      COrder = new Order();
    
      ResponseScada response = null;
      // It must be asynchronous or in a thread so that 
      // the user input is still present.
      // I take await because the process can only be continued after an answer or a timeout.
      response = await LScada.GetResponseInfosAsync();
       
      
    // the asynchronous verification function.
    public async ResponseScada GetResponseInfosAsync()
    {
    	while (!IsAFileExist(out useFile, out fileName))
    	{
         	await Task.Delay(1000);
    
    	//..
    	} 
    	ResponseScada ret = new ResponseScada ();
    AB
    Can someone please show the correct syntax in this example?
    Thanks in advance.


    Thanks in advance.

    Greetings Markus



    Wednesday, February 5, 2020 6:46 AM
  • What Karen said, applied to your code.

    protected override void Data(InputA input)
    {
       //Wait for it to complete
       ScanOrderNo(value).Wait();
    }
    
    //Return a Task so calling code can wait
    protected async Task ScanOrderNo(string value)
    {
      COrder = new Order();
    
      ResponseScada response = null;
      response = await LScada.GetResponseInfosAsync();
      ...
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, February 5, 2020 2:50 PM
    Moderator

  • Hello Michael,
    Works well. Thanks for the sample!
    I need Wait (). I couldn't read anywhere.
    Last question. Would it be better to use a task or thread?
    Best regards Markus

    Thursday, February 6, 2020 7:08 AM
  • Hello,
    works not.
    // Here is synchron, I can't change it.
    protected override void Data(InputA input)
    {
       ScanOrderNo(value);
    
    // I call the function.
    protected async void ScanOrderNo(string value)
    {
      COrder = new Order();
    
      ResponseScada response = null;
      // It must be asynchronous or in a thread so that 
      // the user input is still present.
      // I take await because the process can only be continued after an answer or a timeout.
      response = await LScada.GetResponseInfosAsync();
       
      
    // the asynchronous verification function.
    public async ResponseScada GetResponseInfosAsync()
    {
    	while (!IsAFileExist(out useFile, out fileName))
    	{
         	await Task.Delay(1000);
    
    	//..
    	} 
    	ResponseScada ret = new ResponseScada ();
    AB
    Can someone please show the correct syntax in this example?
    Thanks in advance.


    Thanks in advance.

    Greetings Markus



    I don't see the offending code that the image references

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, February 6, 2020 1:10 PM
    Moderator
  • Use tasks unless you need the advanced features of threads.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, February 6, 2020 2:34 PM
    Moderator
  • Hello Karen,
    now works well.
    The main problem was Wait().
    case "ID_SCAN_ORDER_NUMBER":
     SettingsScanOrderNo(value).Wait();
     break;

    I have not used Wait(). Why I need this is not really clear to me.
    Is there a simple explanation?

    Greetings Markus

    is ok now

    Thursday, February 6, 2020 5:29 PM
  • ok, what would the extended functions be? Do you have a simple sample?

    Reasons for

    • await/async
    • Task
    • Thread
    Thursday, February 6, 2020 5:31 PM
  • Hello Karen,
    now works well.
    The main problem was Wait().
    case "ID_SCAN_ORDER_NUMBER":
     SettingsScanOrderNo(value).Wait();
     break;

    I have not used Wait(). Why I need this is not really clear to me.
    Is there a simple explanation?

    Greetings Markus

    is ok now

    Wait is a synchronization method that causes the calling thread to wait until the current task has completed. If the current task has not started execution, the Wait method attempts to remove the task from the scheduler and execute it inline on the current thread. If it is unable to do that, or if the current task has already started execution, it blocks the calling thread until the task completes.

    Source


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Markus Freitag Tuesday, February 11, 2020 5:04 PM
    Thursday, February 6, 2020 8:51 PM
    Moderator
  • Hi Markus Freitag,
    I am glad you have got your solution,we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    Best Regards,
    Daniel Zhang


    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.

    Tuesday, February 11, 2020 7:01 AM