So i have a method that calls methods from another class and is waiting for them to complete before calling another method. The problem i am having is that the method thats being called uses an eventhandler to do actions. Is there a way to delay the return of that method until the eventhandler has done its thing? To clarify, i made an example:
Main class:
class MainClass
{
SomeObject someObject;
private async void MainMethod()
{
someObject = new SomeObject();
await someObject.SomeMethod();
await someObject.SomeOtherMethod();
}
}
SomeObject Class:
class SomeObject
{
public event EventHandler<object> SomethingChanged;
public async Task SomeMethod()
{
Console.WriteLine("fired SomeMethod");
SomethingChanged += SomethingChangedAsync;
Subscribe("<someURL>", SomethingChanged); //this is being done by an api im using...
await api.someApiCall;
Console.WriteLine("here things are happening that would make the event trigger. the method however, is now done with its logic and now returns and instantly goes to SomeOtherMethod but SomethingChangedAsync is not done processing what needs to be done");
}
public async Task SomeOtherMethod()
{
await api.someApiCall;
Console.WriteLine("fired SomeOtherMethod");
}
private async void SomethingChangedAsync(object sender, object e)
{
await api.someApiCall;
Console.WriteLine("doing stuff here that takes some time. i would like SomeMethod to wait with returning until the logic here is finished");
}
}
is there a way i can fix this issue. maybe my approach is completely wrong. i hope someone can help me with this