///<summary> /// Called when an asynchronous in-place save operation finishes ///</summary> void SaveInPlaceAsyncCallback(IAsyncResult ar) { lock (LockObj) { SaveInPlaceOperation sipo = (SaveInPlaceOperation)ar.AsyncState; if (sipo.Result == ThreadedAsyncOperation.OperationResult.Finished) // save went ok { LoadWithFile(sipo.SavePath); if (undoDeque.Count > 0) { SaveCheckpoint = undoDeque.PeekFront(); } else { SaveCheckpoint = null; } changedBeyondUndo = false; } else if (sipo.Result == ThreadedAsyncOperation.OperationResult.Cancelled) // save cancelled { } else if (sipo.Result == ThreadedAsyncOperation.OperationResult.CaughtException) { } // re-allow buffer usage this.ReadAllowed = true; this.ModifyAllowed = true; this.FileOperationsAllowed = true; this.EmitEvents = true; fsw.EnableRaisingEvents = true; // notify the world about the changes EmitPermissionsChanged(); EmitChanged(); // if user provided a callback, call it now if (userSaveAsyncCallback != null) { userSaveAsyncCallback(ar); } // notify that Save has finished saveFinishedEvent.Set(); } }
///<summary> /// Save the buffer under the same filename, using an asynchronous model ///</summary> public IAsyncResult BeginSave(ProgressCallback progressCallback, AsyncCallback ac) { lock (LockObj) { if (!fileOperationsAllowed) { return(null); } saveFinishedEvent.Reset(); userSaveAsyncCallback = ac; Thread saveThread = null; ThreadedAsyncResult tar = null; // decide whether to save in place or normally if (!fileBuf.IsResizable || this.Size == fileBuf.Size) { SaveInPlaceOperation sipo = new SaveInPlaceOperation(this, progressCallback, SaveInPlaceAsyncCallback, useGLibIdle); saveThread = new Thread(sipo.OperationThread); tar = new ThreadedAsyncResult(sipo, saveFinishedEvent, false); } else { SaveOperation so = new SaveOperation(this, TempFile.CreateName(tempDir), progressCallback, SaveAsyncCallback, useGLibIdle); saveThread = new Thread(so.OperationThread); tar = new ThreadedAsyncResult(so, saveFinishedEvent, false); } // don't allow messing up with the buffer // while we are saving // ...ReadAllowed is set in SaveOperation //this.ReadAllowed=false; this.ModifyAllowed = false; this.FileOperationsAllowed = false; this.EmitEvents = false; fsw.EnableRaisingEvents = false; // start save thread saveThread.IsBackground = true; saveThread.Start(); return(tar); } }