c# - 调用其他命令的 PSCmdlet 由于处理时间长而卡住?

标签 c# .net powershell powershell-cmdlet

我有一个用 C# 和 .NET 3.5 编写的 PSCmdlet,它调用在同一 Cmdlet 中定义的一些其他命令。

其中一个命令会启动对我的 SharePoint 网站的全面爬网并等待它完成。乐观地说,这些需要 5-10 分钟才能完成。

由于此命令需要很长时间,并且 PowerShell 类会同步运行它们,因此控制台会同时卡住。

没关系,但几分钟后,Windows 会假定它已崩溃并炸毁 PowerShell 控制台。我怎样才能避免这种情况发生?

这是我运行命令的方式:

PowerShell ps = PowerShell.Create();
ps.Commands.AddCommand("Start-CrawlAndRunSearchCatalog").AddParameter("configFilePath", this.configFilePath);

foreach (string str in ps.AddCommand("Out-String").Invoke<string>())
            this.WriteObject(str);

最佳答案

也许是这个?

var thread = new Thread(() =>
{
    PowerShell ps = PowerShell.Create();
    ps.Commands.AddCommand("Start-CrawlAndRunSearchCatalog").AddParameter("configFilePath", this.configFilePath);

    foreach (string str in ps.AddCommand("Out-String").Invoke<string>())
        this.WriteObject(str);
});

thread.Start();
thread.Join();

关于c# - 调用其他命令的 PSCmdlet 由于处理时间长而卡住?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17192610/

相关文章:

c# - 使用 WPF 窗体 DataGrid、DataTable 和 Combobox

c# - 无法从 appsettings.json 读取数据

.net - VS 2010 .NET 应用程序发布在 XP 或 Server 2003 上编译崩溃

c# - 在不关注 TabControl 选项卡的情况下循环

powershell - 使用 Get-ADGroup 时隐藏错误

c# - asp.net mvc int 属性绑定(bind)异常

c# - 如何使用 Entity Framework Core 2.0 检索 JSON 存储过程的结果?

.net - 在 Azure 应用程序设置中配置我的连接字符串,并将其在我的 web.config 中配置为环境变量

powershell - 如何从 TFS 构建过程将参数传递给 PowerShell 脚本?

powershell - VSTS 任务组 Powershell 参数