private static void DeleteFilesAsync(string[] paths, Action callback) { if (backgroundWorker == null) { backgroundWorker = InitializeBackgroundWorker(); backgroundWorker.DoWork += (sender, args) => { DeleteFiles(paths); }; backgroundWorker.RunWorkerCompleted += (sender, args) => { if (callback != null) { AsyncActionHelper.RunInGameThread(() => { callback(); }); } backgroundWorker = null; }; backgroundWorker.RunWorkerAsync(); } else if (backgroundWorker != null && backgroundWorker.IsBusy) { EnqueueAction(() => { DeleteFilesAsync(paths, callback); }); } }
private static void ReadFileAsync <T>(string path, Action <T> callback) { if (backgroundWorker == null) { backgroundWorker = InitializeBackgroundWorker(); backgroundWorker.DoWork += (sender, args) => { args.Result = ReadAndDeserialize <T>(path); }; backgroundWorker.RunWorkerCompleted += (sender, args) => { var result = (T)args.Result; AsyncActionHelper.RunInGameThread(() => { callback(result); }); backgroundWorker = null; }; backgroundWorker.RunWorkerAsync(); } else if (backgroundWorker != null && backgroundWorker.IsBusy) { EnqueueAction(() => { ReadFileAsync <T>(path, callback); }); } }
private static void ReadFilesAsync <T>(string rootPath, string searchPattern, Action <List <T> > callback) { if (backgroundWorker == null) { backgroundWorker = InitializeBackgroundWorker(); backgroundWorker.DoWork += (sender, args) => { args.Result = ReadAndDeserializeMultiple <T>(rootPath, searchPattern); }; backgroundWorker.RunWorkerCompleted += (sender, args) => { var result = (List <T>)args.Result; AsyncActionHelper.RunInGameThread(() => { callback(result); }); backgroundWorker = null; }; backgroundWorker.RunWorkerAsync(); } else if (backgroundWorker != null && backgroundWorker.IsBusy) { EnqueueAction(() => { ReadFilesAsync(rootPath, searchPattern, callback); }); } }
private static void SaveDataAsync(object data, string path, Action callback) { if (backgroundWorker == null) { backgroundWorker = InitializeBackgroundWorker(); backgroundWorker.DoWork += (sender, args) => { SerializeAndWrite(data, path); }; backgroundWorker.RunWorkerCompleted += (sender, args) => { if (callback != null) { AsyncActionHelper.RunInGameThread(() => { callback(); }); } backgroundWorker = null; }; backgroundWorker.RunWorkerAsync(); } else if (backgroundWorker != null && backgroundWorker.IsBusy) { EnqueueAction(() => { SaveDataAsync(data, path, callback); }); } }
private static BackgroundWorker InitializeBackgroundWorker() { var newBackgroundWorker = new BackgroundWorker(); newBackgroundWorker.RunWorkerCompleted += (sender, args) => { if (args.Error != null) { AsyncActionHelper.RunInGameThread(() => { throw args.Error; }); } if (backgroundWorkQueue != null && backgroundWorkQueue.Count > 0) { backgroundWorkQueue.Dequeue().Invoke(); } }; return(newBackgroundWorker); }
private static void SaveGameAsync(SavedGame savedGame, string gameDataPath, string metadataPath, Action callback) { if (backgroundWorker == null) { backgroundWorker = InitializeBackgroundWorker(); backgroundWorker.DoWork += (sender, args) => { SerializeAndWrite(savedGame.gameSpecificData, gameDataPath); SerializeAndWrite(savedGame.metadata, metadataPath); }; backgroundWorker.RunWorkerCompleted += (sender, args) => { if (callback != null) { AsyncActionHelper.RunInGameThread(() => { callback(); }); } backgroundWorker = null; }; backgroundWorker.RunWorkerAsync(); } else if (backgroundWorker != null && backgroundWorker.IsBusy) { EnqueueAction(() => { SaveGameAsync(savedGame, gameDataPath, metadataPath, callback); }); } }