visual-studio - 如何在 Visual Studio 宏中给出时间延迟

标签 visual-studio visual-studio-extensions visual-studio-macros

最近我更新了 Visual Studio 并开始使用扩展宏资源管理器。

我尝试使用示例宏之一“删除并排序所有”,但我意识到如果我有一个打开的文档,它就不会运行。因此,我关闭了所有打开的文档,然后再次尝试,这次它打开所有文档并关闭它们,但它也不起作用。

问题是在文档完全加载之前执行的命令。如果有一个事件或计时器可以帮助等待文档完全加载,问题就会解决。

这是我的代码。我标记了要添加等待功能的位置:

function formatFile(file) {
    dte.ExecuteCommand("View.SolutionExplorer");
    if (file.Name.indexOf(".cs", file.Name.length - ".cs".length) !== -1) {
        file.Open();
        file.Document.Activate();

//here i want to wait for 1 second
        dte.ExecuteCommand("Edit.RemoveAndSort");

        file.Document.Save();
        file.Document.Close();
    }
}

我感谢任何形式的帮助。

GitHub 上的宏资源管理器:https://github.com/Microsoft/VS-Macros

最佳答案

根据您在原始帖子中共享的代码片段以及我还从 GitHub 克隆的项目,您的代码应该是 JavaScript 代码。

在 JavaScript 代码中,有 setTimeout() 或 setInterval() 函数。例如,如果您使用setTimeout(),则需要将暂停后需要运行的代码移至setTimeout()回调中。

请按如下方式修改您的代码。

function formatFile(file) {
dte.ExecuteCommand("View.SolutionExplorer");
if (file.Name.indexOf(".cs", file.Name.length - ".cs".length) !== -1) {
    file.Open();
    file.Document.Activate();


    setTimeout(function () {
        //move the code that you want to run after 1 second
        dte.ExecuteCommand("Edit.RemoveAndSort");
        file.Document.Save();
        file.Document.Close();
    }, 1000);

}

Javascript 代码中有很多关于 sleep() 的线程。请引用:

What is the JavaScript version of sleep()?

JavaScript sleep/wait before continuing

关于visual-studio - 如何在 Visual Studio 宏中给出时间延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46189416/

相关文章:

c++ - 从 sln 文件生成 Qt pro 文件

visual-studio-2010 - 将上下文菜单添加到 VS 2010 解决方案资源管理器(仅在解决方案上显示项目)

visual-studio - Visual Studio宏: Find files that aren't included in the project?

javascript - 学习哪种(批处理)语言来合并源文件?

visual-studio-2010 - 启动对话框时,Visual Studio 2010宏挂起

c++ - 获取带有 typeid(*this).name 的静态 int 变量类名用于其自己的定义 - C++

c# - 合并 2 个 VS 项目 - 如何更改起始 .exe 文件?

c# - VSTO Powerpoint 笔记页 - 同一行上的不同颜色的单词

windows - 如何在Windows上使用clang从命令行传递链接描述文件?

c# - EnvDTE.CodeNamespace 成员为空?