Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PowerShell:无法生成新线程_C#_Powershell - Fatal编程技术网

C# PowerShell:无法生成新线程

C# PowerShell:无法生成新线程,c#,powershell,C#,Powershell,我正在尝试使用以下命令在PowerShell的命令行中生成新线程: $t = New-Object System.Threading.Thread ([System.Threading.ThreadStart]{ Write-Host "Hello World" }); $t.Start(); 发生的情况是出现一个对话框,上面显示“Powershell已停止工作” 我想使用我自己的Job类,用C#编写,带有start、pause、continue和stop方法。它使用两个Wait

我正在尝试使用以下命令在PowerShell的命令行中生成新线程:

$t = New-Object System.Threading.Thread ([System.Threading.ThreadStart]{ 
    Write-Host "Hello World" 
});

$t.Start();
发生的情况是出现一个对话框,上面显示“Powershell已停止工作”

我想使用我自己的
Job
类,用C#编写,带有start、pause、continue和stop方法。它使用两个
WaitHandles
和一个新的
Thead
实例来实现这一点

我知道
启动作业
等,但希望使用真正的线程

有办法吗


编辑:似乎有一种方法

更新我已将以下内容打包到名为
PSRunspacedDelegate
的模块中,您可以使用
安装包PSRunspacedDelegate
进行安装。你可以找到


说明运行PowerShell代码的线程必须具有运行空间

换句话说,
[Runspace]::DefaultRunspace
不能为空

最后我编写了一个
RunspacedDelegateModule.psm1
模块,其中有一个函数
newrunspaceddelegate
,可以完成这项工作

Add-Type -Path "$PSScriptRoot\RunspacedDelegateFactory.cs"

Function New-RunspacedDelegate(
    [Parameter(Mandatory=$true)][System.Delegate]$Delegate, 
    [Runspace]$Runspace=[Runspace]::DefaultRunspace) {

    [PowerShell.RunspacedDelegateFactory]::NewRunspacedDelegate($Delegate, $Runspace)
}
RunspacedDelegateFactory.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Management.Automation.Runspaces;

namespace PowerShell
{
    public class RunspacedDelegateFactory
    {
        public static Delegate NewRunspacedDelegate(Delegate _delegate, Runspace runspace)
        {
            Action setRunspace = () => Runspace.DefaultRunspace = runspace;

            return ConcatActionToDelegate(setRunspace, _delegate);
        }

        private static Expression ExpressionInvoke(Delegate _delegate, params Expression[] arguments)
        {
            var invokeMethod = _delegate.GetType().GetMethod("Invoke");

            return Expression.Call(Expression.Constant(_delegate), invokeMethod, arguments);
        }

        public static Delegate ConcatActionToDelegate(Action a, Delegate d)
        {
            var parameters =
                d.GetType().GetMethod("Invoke").GetParameters()
                .Select(p => Expression.Parameter(p.ParameterType, p.Name))
                .ToArray();

            Expression body = Expression.Block(ExpressionInvoke(a), ExpressionInvoke(d, parameters));

            var lambda = Expression.Lambda(d.GetType(), body, parameters);

            var compiled = lambda.Compile();

            return compiled;
        }
    }
}
我注意到,如果我使用
写主机
,它仍然会崩溃,但是
输出文件
似乎还可以

以下是如何使用它:

Import-Module RunspacedDelegateModule;
$writeHello = New-RunspacedDelegate ([System.Threading.ThreadStart]{ 
    "$([DateTime]::Now) hello world" | Out-File "C:\Temp\log.txt" -Append -Encoding utf8 
});
$t = New-Object System.Threading.Thread $writeHello;
$t.Start();

最近在玩弄乔布斯时发生了几起车祸。。。不确定我当时是否打开了强制的DEP/ASLR,并且从未查看日志您正在测试的PowerShell版本是什么?以下是我得到的异常
CLR版本:v4.0.30319[08:14:34]异常:E0434F4D.System.Management.Automation.PSInvalidOperationException(此线程中没有可用于运行脚本的运行空间。您可以在System.Management.Automation.Runspaces.Runspace类型的DefaultRunspace属性中提供一个运行空间。您尝试调用的脚本块是:Write Host“Hello World”)
但不知道如何解决。可能是interest@TahirHassan请记住,PowerShell不是本机.NET/CLR语言,您不能期望运行时的行为与编译C#或VB.NET时的行为相同。不要从多线程的角度来考虑,而是