/// <summary> /// Does the asynchronous highlighting step. /// </summary> private void StepHilite() { if (_stateHilite == null) { return; // Has been shut down } uint dwStart = Win32Declarations.GetTickCount(); uint dwLimit = 222; // Allow running for this much milliseconds continuously // Freeze the control Win32Declarations.SendMessage(Handle, Win32Declarations.WM_SETREDRAW, IntPtr.Zero, IntPtr.Zero); try { int nIterations; for (nIterations = 0; Win32Declarations.GetTickCount() - dwStart < dwLimit; nIterations++) // Work for some limited time { if (!_stateHilite.StepHiliteDelegate(_stateHilite)) // Invoke the individual highlighting step { // Highlighting Completed! // Reset the status bar dials _statuswriter.ClearStatus(); _statuswriter = null; // Retrieve the values _wordsSearchHits = _stateHilite.ActualSearchHits; _nCurrentSearchHit = -1; // Deinitialize the hilite search _stateHilite = null; // Jump to the next search hit GotoNextSearchHit(true, false); // Invalidate Win32Declarations.SendMessage(Handle, Win32Declarations.WM_SETREDRAW, (IntPtr)1, IntPtr.Zero); Invalidate(); // Done! Trace.WriteLine(String.Format("The JetRichTextBox has completed the async highlighting with {0} hits total.", (_wordsSearchHits != null ? _wordsSearchHits.Length.ToString() : "#ERROR#")), "[JRTB]"); return; } } Trace.WriteLine(String.Format("The JetRichTextBox async highlighting has done {0} highlightings on this step.", nIterations), "[JRTB]"); } finally { // Unfreeze the events and repaint Win32Declarations.SendMessage(Handle, Win32Declarations.WM_SETREDRAW, (IntPtr)1, IntPtr.Zero); if ((_stateHilite != null) && (Win32Declarations.GetTickCount() - _stateHilite.LastRepaintTime > 2000)) // Repaint rarely { Invalidate(); _stateHilite.LastRepaintTime = Win32Declarations.GetTickCount(); } } // Requeue the rest of execution Application.DoEvents(); // Without this, the painting events won't occur Core.UserInterfaceAP.QueueJob("Highlight the search hits.", new MethodInvoker(StepHilite)); }