fix!: get rid of async void

This commit is contained in:
ZavaruKitsu 2022-10-08 19:37:00 +03:00
parent 5ab15621a0
commit f41fdf90ed
5 changed files with 28 additions and 28 deletions

View File

@ -20,5 +20,5 @@ var bot = BotBaseBuilder.Create()
.DefaultLanguage()
.Build();
bot.Start();
await Task.Delay(-1);
await bot.Start();
await Task.Delay(-1);

View File

@ -80,7 +80,7 @@ public class BotBase
/// <summary>
/// Start your Bot
/// </summary>
public void Start()
public async Task Start()
{
if (Client == null)
{
@ -89,16 +89,15 @@ public class BotBase
Client.MessageLoop += Client_MessageLoop;
if (StateMachine != null)
{
Sessions.LoadSessionStates(StateMachine);
await Sessions.LoadSessionStates(StateMachine);
}
//Enable auto session saving
if (GetSetting(ESettings.SaveSessionsOnConsoleExit, false))
{
Console.SetHandler(() => { Sessions.SaveSessionStates(); });
Console.SetHandler(() => { Sessions.SaveSessionStates().GetAwaiter().GetResult(); });
}
DeviceSession.MaxNumberOfRetries = GetSetting(ESettings.MaxNumberOfRetries, 5);
@ -141,7 +140,7 @@ public class BotBase
/// <summary>
/// Stop your Bot
/// </summary>
public void Stop()
public async Task Stop()
{
if (Client == null)
{
@ -149,11 +148,9 @@ public class BotBase
}
Client.MessageLoop -= Client_MessageLoop;
Client.StopReceiving();
Sessions.SaveSessionStates();
await Sessions.SaveSessionStates();
}
/// <summary>
@ -407,4 +404,4 @@ public class BotBase
}
#endregion
}
}

View File

@ -69,7 +69,7 @@ public class NavigationController : FormBase, IStateForm
}
public void LoadState(LoadStateEventArgs e)
public async Task LoadState(LoadStateEventArgs e)
{
if (e.Get("$controller.history.count") == null)
{
@ -144,13 +144,13 @@ public class NavigationController : FormBase, IStateForm
form.Client = Client;
form.NavigationController = this;
form.OnInit(new InitEventArgs());
await form.OnInit(new InitEventArgs());
History.Add(form);
}
}
public void SaveState(SaveStateEventArgs e)
public Task SaveState(SaveStateEventArgs e)
{
e.Set("$controller.history.count", History.Count.ToString());
@ -175,6 +175,8 @@ public class NavigationController : FormBase, IStateForm
i++;
}
return Task.CompletedTask;
}
private async Task NavigationController_Init(object sender, InitEventArgs e)

View File

@ -1,4 +1,5 @@
using TelegramBotBase.Args;
using System.Threading.Tasks;
using TelegramBotBase.Args;
namespace TelegramBotBase.Interfaces;
@ -7,7 +8,7 @@ namespace TelegramBotBase.Interfaces;
/// </summary>
public interface IStateForm
{
void LoadState(LoadStateEventArgs e);
Task LoadState(LoadStateEventArgs e);
void SaveState(SaveStateEventArgs e);
}
Task SaveState(SaveStateEventArgs e);
}

View File

@ -118,25 +118,25 @@ public class SessionBase
/// <summary>
/// Loads the previously saved states from the machine.
/// </summary>
public async void LoadSessionStates()
public async Task LoadSessionStates()
{
if (BotBase.StateMachine == null)
{
return;
}
LoadSessionStates(BotBase.StateMachine);
await LoadSessionStates(BotBase.StateMachine);
}
/// <summary>
/// Loads the previously saved states from the machine.
/// </summary>
public async void LoadSessionStates(IStateMachine statemachine)
public async Task LoadSessionStates(IStateMachine statemachine)
{
if (statemachine == null)
{
throw new ArgumentNullException("StateMachine",
throw new ArgumentNullException(nameof(statemachine),
"No StateMachine defined. Please set one to property BotBase.StateMachine");
}
@ -230,7 +230,7 @@ public class SessionBase
{
Values = s.Values
};
iform.LoadState(ls);
await iform.LoadState(ls);
}
try
@ -251,11 +251,11 @@ public class SessionBase
/// <summary>
/// Saves all open states into the machine.
/// </summary>
public void SaveSessionStates(IStateMachine statemachine)
public async Task SaveSessionStates(IStateMachine statemachine)
{
if (statemachine == null)
{
throw new ArgumentNullException("StateMachine",
throw new ArgumentNullException(nameof(statemachine),
"No StateMachine defined. Please set one to property BotBase.StateMachine");
}
@ -299,7 +299,7 @@ public class SessionBase
{
//Loading Session states
var ssea = new SaveStateEventArgs();
iform.SaveState(ssea);
await iform.SaveState(ssea);
se.Values = ssea.Values;
}
@ -335,7 +335,7 @@ public class SessionBase
/// <summary>
/// Saves all open states into the machine.
/// </summary>
public void SaveSessionStates()
public async Task SaveSessionStates()
{
if (BotBase.StateMachine == null)
{
@ -343,6 +343,6 @@ public class SessionBase
}
SaveSessionStates(BotBase.StateMachine);
await SaveSessionStates(BotBase.StateMachine);
}
}