如何在 Visual Studio 宏中设置时间延迟
How to give a time delay in Visual Studio macros
最近我更新了我的 Visual Studio 并开始使用扩展宏资源管理器。
我尝试使用示例宏之一 "removes and sorts all",但我意识到如果我有一个打开的文档,它不会 运行。所以我关闭了所有打开的文档,这次再试一次它打开所有文档并关闭它们,但它也不起作用。
问题是在文档完全加载之前执行的命令。如果有事件或计时器可以帮助等待文档完全加载,问题就会解决。
这是我的代码。我标记了我要添加等待功能的地方:
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
根据您在原始 post 中分享的代码片段,我还从 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 并开始使用扩展宏资源管理器。
我尝试使用示例宏之一 "removes and sorts all",但我意识到如果我有一个打开的文档,它不会 运行。所以我关闭了所有打开的文档,这次再试一次它打开所有文档并关闭它们,但它也不起作用。
问题是在文档完全加载之前执行的命令。如果有事件或计时器可以帮助等待文档完全加载,问题就会解决。
这是我的代码。我标记了我要添加等待功能的地方:
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
根据您在原始 post 中分享的代码片段,我还从 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