How to block on a OpenFile in a Visual Studio addin
I have a Visual Studio 2008 addin that when I press a certain hotkey, it opens a specific file (different based on the context of the hotkey) and then searches that file for a specific string (again, context dependent.) Most of the time this works flawlessly, but sometimes if the file it's opening is too big, the search will fail.
Here is a code snippet:
Window xmlWindow = Commands.Application.ItemOperations.OpenFile(objectFilename, EnvDTE.Constants.vsViewKindPrimary);
Find find = xmlWindow.Document.DTE.Find;
find.Action = vsFindAction.vsFindActionFind;
find.FindWhat = String.Format("Name=\"{0}\"", objectLocalName);
if (find.Execute() == vsFindResult.vsFindResultFound) {
MessageBox.Show("Found!");
}
1. Is there a way to get it to always work (for example by blocking on the OpenFile)?
2. On a less important note, is there a way to search like this without having the results end up in the Find Results pane (this causes my old results to be cleared out by this search that is only used to get the cursor down to that part of the file)?
开发者_如何转开发
If OpenFile behaves asynchronously, I'd suggest you consider changing the logic to rely on a different event, one that relies on the document being activated.
For example, have you tried triggering the OpenFile with your shortcut key, then queuing the search so that it's later handled by a VS event? (The code below was taken from a Visual Studio 2010 addin, but I believe the events are the same.)
// make sure these are class variables, otherwise they may get GC'd incorrectly and break the COM interaction private WindowEvents _winEvents = null; private DTE2 _applicationObject;
in the connect:
_events = _applicationObject.Events;
_winEvents = _events.get_WindowEvents();
_winEvents.WindowActivated += new _dispWindowEvents_WindowActivatedEventHandler(WindowActivated);
Then, you'd put some code in the WindowActivated:
void WindowActivated(Window GotFocus, Window LostFocus)
{
Document gotFocusDoc = GotFocus.Document;
if (gotFocusDoc != null)
{
string fileExt = Path.GetExtension(gotFocusDoc.Name);
There, you'd watch for the file that you wanted to scan (you'd maybe need to keep a list, etc.).
For the second issue, you could just scan through the document yourself once you've got access in the way I suggested above.
I think, that DTE.ItemOperations.OpenFile() method is synchronous. Try to use a following code snippet for VS2008.
using EnvDTE;
Window win = _applicationObject.ItemOperations.OpenFile(@"path-to-xml-file", Constants.vsViewKindPrimary);
TextDocument doc = win.Document.Object("TextDocument") as TextDocument;
if (doc != null)
{
EditPoint searchStart = doc.StartPoint.CreateEditPoint();
EditPoint endOfFoundText = null;
TextRanges ranges = null;
bool result = searchStart.FindPattern("Text-to-search", (int)vsFindOptions.vsFindOptionsNone, ref endOfFoundText, ref ranges);
if (result)
{
// Result is bounded by searchStart and endOfFoundText points.
System.Windows.Forms.MessageBox.Show("BINGO! Found at " + searchStart.Line.ToString());
}
}
If you will have a trouble with catching an opened window, I suggest to check a source code of my extension WordLight: there is a WindowWatcher class, which tracks creation of text views.
精彩评论