Microsoft.VisualStudio.Project.SuspendFileChanges.Suspend C# (CSharp) Méthode

Suspend() public méthode

public Suspend ( ) : void
Résultat void
        public virtual void Suspend()
        {
            if(this.isSuspending)
                return;

            IntPtr docData = IntPtr.Zero;
            try
            {
                IVsRunningDocumentTable rdt = this.site.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable;

                IVsHierarchy hierarchy;
                uint itemId;
                uint docCookie;
                IVsFileChangeEx fileChange;

                if(rdt == null) return;

                ErrorHandler.ThrowOnFailure(rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, this.documentFileName, out hierarchy, out itemId, out docData, out docCookie));

                if((docCookie == (uint)ShellConstants.VSDOCCOOKIE_NIL) || docData == IntPtr.Zero)
                    return;

                fileChange = this.site.GetService(typeof(SVsFileChangeEx)) as IVsFileChangeEx;

                if(fileChange != null)
                {
                    this.isSuspending = true;
                    ErrorHandler.ThrowOnFailure(fileChange.IgnoreFile(0, this.documentFileName, 1));
                    if(docData != IntPtr.Zero)
                    {
                        // if interface is not supported, return null
                        object unknown = Marshal.GetObjectForIUnknown(docData);
                        this.fileChangeControl = unknown as IVsDocDataFileChangeControl;
                        if(this.fileChangeControl != null)
                        {
                            ErrorHandler.ThrowOnFailure(this.fileChangeControl.IgnoreFileChanges(1));
                        }
                    }
                }
            }
            catch(InvalidCastException e)
            {
                Trace.WriteLine("Exception" + e.Message);
            }
            finally
            {
                if(docData != IntPtr.Zero)
                {
                    Marshal.Release(docData);
                }
            }
            return;
        }

Usage Example

        /// <summary>
        /// Get's called to rename the eventually running document this hierarchyitem points to
        /// </summary>
        /// returns FALSE if the doc can not be renamed
        internal bool RenameDocument(string oldName, string newName)
        {
            IVsRunningDocumentTable pRDT = this.GetService(typeof(IVsRunningDocumentTable)) as IVsRunningDocumentTable;
            if(pRDT == null) return false;
            IntPtr docData = IntPtr.Zero;
            IVsHierarchy pIVsHierarchy;
            uint itemId;
            uint uiVsDocCookie;

            SuspendFileChanges sfc = new SuspendFileChanges(this.ProjectMgr.Site, oldName);
            sfc.Suspend();

            try
            {
                // Suspend ms build since during a rename operation no msbuild re-evaluation should be performed until we have finished.
                // Scenario that could fail if we do not suspend.
                // We have a project system relying on MPF that triggers a Compile target build (re-evaluates itself) whenever the project changes. (example: a file is added, property changed.)
                // 1. User renames a file in  the above project sytem relying on MPF
                // 2. Our rename funstionality implemented in this method removes and readds the file and as a post step copies all msbuild entries from the removed file to the added file.
                // 3. The project system mentioned will trigger an msbuild re-evaluate with the new item, because it was listening to OnItemAdded.
                //    The problem is that the item at the "add" time is only partly added to the project, since the msbuild part has not yet been copied over as mentioned in part 2 of the last step of the rename process.
                //    The result is that the project re-evaluates itself wrongly.
                VSRENAMEFILEFLAGS renameflag = VSRENAMEFILEFLAGS.VSRENAMEFILEFLAGS_NoFlags;
                try
                {
                    this.ProjectMgr.SuspendMSBuild();
                    ErrorHandler.ThrowOnFailure(pRDT.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, oldName, out pIVsHierarchy, out itemId, out docData, out uiVsDocCookie));

                    if(pIVsHierarchy != null && !Utilities.IsSameComObject(pIVsHierarchy, this.ProjectMgr))
                    {
                        // Don't rename it if it wasn't opened by us.
                        return false;
                    }

                    // ask other potentially running packages
                    if(!this.ProjectMgr.Tracker.CanRenameItem(oldName, newName, renameflag))
                    {
                        return false;
                    }
                    // Allow the user to "fix" the project by renaming the item in the hierarchy
                    // to the real name of the file on disk.
                    if(IsFileOnDisk(oldName) || !IsFileOnDisk(newName))
                    {
                        RenameInStorage(oldName, newName);
                    }

                    string newFileName = Path.GetFileName(newName);
                    DocumentManager.UpdateCaption(this.ProjectMgr.Site, newFileName, docData);
                    bool caseOnlyChange = NativeMethods.IsSamePath(oldName, newName);
                    if(!caseOnlyChange)
                    {
                        // Check out the project file if necessary.
                        if(!this.ProjectMgr.QueryEditProjectFile(false))
                        {
                            throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED);
                        }

                        this.RenameFileNode(oldName, newName);
                    }
                    else
                    {
                        this.RenameCaseOnlyChange(newFileName);
                    }
                }
                finally
                {
                    this.ProjectMgr.ResumeMSBuild(this.ProjectMgr.ReEvaluateProjectFileTargetName);
                }

                this.ProjectMgr.Tracker.OnItemRenamed(oldName, newName, renameflag);
            }
            finally
            {
                sfc.Resume();
                if(docData != IntPtr.Zero)
                {
                    Marshal.Release(docData);
                }
            }

            return true;
        }
All Usage Examples Of Microsoft.VisualStudio.Project.SuspendFileChanges::Suspend