Powershell呼叫组装代表 [英] Powershell Call Assembly Delegate

查看:77
本文介绍了Powershell呼叫组装代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我们的过程控制应用程序的dll程序集,我使用我的 powershell 脚本中的加载。

I have a dll assembly for our process control application, which I use load inside my powershell script.

该DLL包含一个Delegate键入我需要使用,委托名称是:

The DLL contains a Delegate type that I need to use, delegate name is :

"X.Y.Delegate"

我在这个DLL中有另一种方法,应该这样称呼:

I have an another method in that DLL that should be called this way :

Method( delegatetype CallbackMethod)

所以,我需要:


  1. 在我的脚本中定义一个类型为XYDelegate的代理,例如 $ MyDelegate

定义一个回调方法,以便在触发进程事件时调用它 p>

define a callback method so that it gets invoked when a process event is triggered"

注意:如果我的问题看起来很愚蠢,我很抱歉,我是一个绝对的初学者。 >

Note: I'm sorry if my question seems silly, I'm an ABSOLUTE Beginner.

推荐答案

更新:

阅读您的评论并阅读更密切的问题,我想你可能是我ooking以利用异步事件处理。下面是一个监听事件直到达到超时然后退出的示例。此示例假设您可以更改程序集以添加事件。

After reading your comment and reading your question more closely, I think you might be looking to utilize asynchronous event handling. Below is an example that listens for events until a timeout is reached and then exits. This example assumes you can change your assembly to add an event.

生成事件的类:

namespace ClassLibrary1
{
 public class Class1
 {
  public event EventHandler SomeEvent;

  protected void OnSomeEvent(EventArgs e)
  {
   var someEvent = SomeEvent;

   if (someEvent != null)
   {
    SomeEvent(this, e);
   }
 }

 public void SomeMethod()
 {
  Task.Run(() =>
  {
   for (int i = 0; i < 3; i++)
   {
    Thread.Sleep(3000);
    OnSomeEvent(EventArgs.Empty);
    }
   });
  }
 }
}

Powershell:

Powershell:

# Load up your .net assembly
add-type -path .\Class1.cs 

$x = new-object ClassLibrary1.Class1

$sourceIdentifier = "SomeEvent"

# Register event
$eh = Register-ObjectEvent -SourceIdentifier $sourceIdentifier -InputObject $x -EventName SomeEvent

$x.SomeMethod()

while ($true)
{
 Write-Host "Waiting for event..."
 $event = Wait-Event -SourceIdentifier $sourceIdentifier -Timeout 10

 if ($event -eq $null) 
 {
  Write-Host "No event received for 10 seconds." 
  break 
 }

 # Do processing here
 Write-Host "Processing event..."
 $event

 # Remove event from queue
 Remove-Event -SourceIdentifier $sourceIdentifier
}

Unregister-Event -SourceIdentifier $sourceIdentifier
Write-Host "Done processing events."

这篇关于Powershell呼叫组装代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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