2

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

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

"X.Y.Delegate"

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

Method( delegatetype CallbackMethod)

So, I need to :

  1. define a delegate in my script of type "X.Y.Delegate" for example $MyDelegate

  2. 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.

MikO
  • 18,243
  • 12
  • 77
  • 109
Ahmed Atef
  • 59
  • 4
  • What is your actual question? Are you asking how to implement these or for information as to which option is considered better? – masrtis May 08 '13 at 18:50
  • Thanks for your reply :) I'm using it actually to minimize resources consumption. rather than checking 10000 of tag values every minute, check what have been changed, I can subscribe to an eventhandler, create my own scriptblock to be executed when event occurs. This way I won't scan my 10000 tags, I would be called back whenever something changes. This is my understanding to the topic, if I'm wrong I'd be grateful if you correct me. Thanks again. – Ahmed Atef May 09 '13 at 08:02

1 Answers1

1

Update:

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.

A class that generates events:

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:

# 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."
dugas
  • 12,025
  • 3
  • 45
  • 51
  • Thanks a lot, it DOES work. The idea I think is the variable "ScriptBlock" that encapsulates the block to be executed. I got Powershell stops responding when My application starts calling back the scriptblock, but I can see now in my application server log that handler starts, a handler object is sent, means that I'm on the right direction. Could you please tell me how can powershell responds to my application callbacks after the script has been executed ? I run the script on ISE, debug, when my application server calls me back powershell stops. should I see the output in my console ? Thanks – Ahmed Atef May 09 '13 at 07:56
  • @AhmedAtef - Updated based on your comments. – dugas May 09 '13 at 20:21