如何让C#PowerShell的调用成员线程安全 [英] How to make C# Powershell Invoke member thread safe

查看:179
本文介绍了如何让C#PowerShell的调用成员线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种服务,当接收请求时,运行PowerShell命令,并返回结果。下面是调用类代码:

I have this service that, when request is received, runs a powershell command and returns result. Here is the invoker class code:

public class PowerShellScript {

    public PowerShellScript() {
    }

    public Object[] Invoke( String strScriptName, NameValueCollection nvcParams ) {
        Boolean bResult = true;
        int n = 0;
        Object[] objResult = null;
        PowerShell ps = PowerShell.Create();
        String strScript = strScriptName;

        for (n = 0; n < nvcParams.Count; n++) {
            strScript += String.Format( " -{0} {1}", nvcParams.GetKey( n ), nvcParams[n] );
        }

        //ps.AddScript( @"E:\snapins\Init-profile.ps1" );
        ps.AddScript( strScript );
        Collection<PSObject> colpsOutput = ps.Invoke();
        if (colpsOutput.Count > 0)
            objResult = new Object[colpsOutput.Count];

        n = 0;
        foreach (PSObject psOutput in colpsOutput) {
            if (psOutput != null) {
                try {
                    objResult[n] = psOutput.BaseObject;
                }
                catch (Exception ex) { 
                    //exception should be handeled properly in powershell script
                }
            }
            n++;
        }
        colpsOutput.Clear();
        ps.Dispose();

        return objResult;
    }
}



方法调用返回由PowerShell脚本返回的所有结果。

Method Invoke returns all results returned by powershell script.

所有罚款和好。只要这个运行在一个单独的线程。当我们调用一些PowerShell脚本可能需要一个小时才能完成,我们不想为服务在那个时候什么都不做,我们决定去多线程。
不幸的是PowerShell的类不是线程安全的,造成SEVER内存泄漏和CPU的燃烧率。但是,如果我在Invoke方法使用锁,这将意味着,为什么我们去多线程的整个想法会付诸东流。

All fine and well. As long as this runs in a single thread. As some powershell scripts we invoke can take up to an hour to complete and we don't want for service to do nothing in that time, we decided to go multi-threaded. Unfortunately Powershell class is not thread safe, resulting in sever memory leaks and cpu burn rate. However, if I use lock on Invoke method, this would mean that the entire idea why we went multithreaded will go down the drain.

任何想法如何解决这个问题?

Any ideas how to solve this?

推荐答案

反正...我在执行PowerShell命令时,放弃了多线程。我创建了一个小程序,它能够执行PowerShell脚本。然后,每个线程创建该程序的新进程。我知道这是一个有点开销,但它的作品。

Anyway... I gave up on multithreading when executing powershell commands. I created a small program that is able to execute powershell scripts. Then, each thread creates new process for that program. I know it is a bit of an overhead, but it works.

基本上,PowerShell的类不是线程安全的。除了静态变量(的http:/ /msdn.microsoft.com/en-us/library/system.management.automation.powershell%28VS.85%29.aspx )。

Basically, Powershell classes are not thread safe. Except static variables (http://msdn.microsoft.com/en-us/library/system.management.automation.powershell%28VS.85%29.aspx).

因此,试图通过在内存泄漏和一些无法解释的CPU使用单独的线程结果调用多个脚本。我的胡乱猜测是,它不关闭正常。从多线程多进程环境变化应理清头绪。然而,这意味着一个重大政治变化

Hence, an attempt to call multiple scripts via separate threads results in memory leakage and some unexplained CPU usage. My wild guess is that it doesn't close properly. Changing from multi-threaded to multi-process environment should sort things out. However, that means a major politics change.

这篇关于如何让C#PowerShell的调用成员线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆