Skip to content

Commit

Permalink
Refactor MainWindow closing logic and add async save
Browse files Browse the repository at this point in the history
Refactored the MainWindow closing logic to support asynchronous
operations. Updated `SentClearMessage` in `OSCSender.cs` to accept
a delay parameter. Replaced `SaveDataToDisk` with an asynchronous
version `MainWindow_ClosingAsync` in `MainWindow.xaml.cs`, which
temporarily cancels the window closing event to await async tasks.
Added a new method `SaveDataToDiskAsync` for async save operations
and included exception handling in both async methods.
  • Loading branch information
BoiHanny committed Sep 28, 2024
1 parent 73ff4d4 commit ed3584f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions vrcosc-magicchatbox/Classes/DataAndSecurity/OSCSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public static async Task SendOSCMessage(bool FX, int delay = 0)
await SendMessageAsync(PrepareMessage(FX), delay);
}

public static async void SentClearMessage()
public static async Task SentClearMessage(int delay)
{
var clearMessage = new OscMessage(CHATBOX_INPUT, "", true, false);
await SendMessageAsync(clearMessage, 0);
await SendMessageAsync(clearMessage, delay);
}

public static async Task ToggleVoice(bool force = false)
Expand Down
36 changes: 29 additions & 7 deletions vrcosc-magicchatbox/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private void OnEndResize()
public MainWindow()
{
InitializeComponent();
Closing += SaveDataToDisk;
Closing += MainWindow_ClosingAsync;

backgroundCheck.Tick += Timer;
backgroundCheck.Interval = TimeSpan.FromMilliseconds(ViewModel.Instance.ScanningInterval * 1000);
Expand Down Expand Up @@ -594,7 +594,7 @@ private void MasterSwitch_Click(object sender, RoutedEventArgs e)
else
{
backgroundCheck.Stop();
OSCSender.SentClearMessage();
OSCSender.SentClearMessage(1000);

}
}
Expand Down Expand Up @@ -784,19 +784,41 @@ private void ResetWindowActivity_Click(object sender, RoutedEventArgs e)
}
}


public void SaveDataToDisk(object sender, System.ComponentModel.CancelEventArgs e)
private async void MainWindow_ClosingAsync(object sender, System.ComponentModel.CancelEventArgs e)
{
// Cancel the window closing event temporarily to await the async task
e.Cancel = true;

try
{
OSCSender.SentClearMessage();
// Optionally hide the window while saving data
Hide();
FireExitSave();

System.Environment.Exit(1);
// Await your asynchronous save logic
await SaveDataToDiskAsync();

// After the async task completes, close the window
Application.Current.Shutdown(); // This is equivalent to System.Environment.Exit in WPF
}
catch (Exception ex)
{
// Handle the exception and optionally log it
Logging.WriteException(ex, MSGBox: true, exitapp: true);
}
}


public async Task SaveDataToDiskAsync()
{
try
{
// Perform your async operations here
await OSCSender.SentClearMessage(1500);
FireExitSave();
}
catch (Exception ex)
{
// Log any exceptions encountered during the save process
Logging.WriteException(ex, MSGBox: true, exitapp: true);
}
}
Expand Down

0 comments on commit ed3584f

Please sign in to comment.