Search code examples
c#.netasync-awaitregistry

Data Entry in registry is Not working when Use Async/Await specially in API


public async Task AddValueInRegistry() {
       await Task.Run(() => {
           RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\Shashi3\Stage1\Stage3", true);
           key.SetValue("Test Key", 2);
           key.Close();
       });
    }

Consider the above method when I run it it creates directory in registry but not data entry. enter image description here


Solution

  • call AddValueInRegistry by using one of the following methods:

    AddValueInRegistry().Wait(); 
    

    or

    public void async UpperMethod()  {
        await AddValueInRegistry();
    }