///<summary> /// Find the previous occurence of the byte pattern. Works asynchronously. ///</summary> public IAsyncResult FindPrevious(AsyncCallback ac) { if (dataBook.NPages == 0 || inUse) { FindPreviousOperation op = new FindPreviousOperation(strategy, null, FindAsyncCallback); return(HandleProblematicOp(op, ac)); } inUse = true; userFindAsyncCallback = ac; findFinishedEvent.Reset(); // if this is the first time we are checking // for a valid pattern, emit the event if (firstTime == true && strategy.Pattern.Length > 0) { firstTime = false; if (FirstFind != null) { FirstFind(); } } // is pattern acceptable? else if (strategy.Pattern.Length == 0) { return(null); } DataView dv = ((DataViewDisplay)dataBook.CurrentPageWidget).View; strategy.Buffer = dv.Buffer; // decide where to start searching from if (!dv.Selection.IsEmpty()) { strategy.Position = dv.Selection.End; } else { strategy.Position = dv.CursorOffset; } SetUpFindProgressReport(); FindPreviousOperation fpo = new FindPreviousOperation(strategy, progressCallback, FindAsyncCallback); // lock the buffer, so user can't modify it while // searching strategy.Buffer.ModifyAllowed = false; strategy.Buffer.FileOperationsAllowed = false; // start find thread Thread findThread = new Thread(fpo.OperationThread); findThread.IsBackground = true; findThread.Start(); return(new ThreadedAsyncResult(fpo, findFinishedEvent, false)); }
void FindPreviousAsyncCallback(IAsyncResult ar) { ThreadedAsyncResult tar = (ThreadedAsyncResult)ar; FindPreviousOperation state = (FindPreviousOperation)tar.AsyncState; if (state.Result == FindPreviousOperation.OperationResult.Finished && state.Match == null) { InformationAlert ia = new InformationAlert(Catalog.GetString("The pattern you requested was not found."), Catalog.GetString("Beginning of file reached."), mainWindow); ia.Run(); ia.Destroy(); } }