dll - Powershell Call Assembly Delegate -
i have dll assembly our process control application, use load inside powershell script.
the dll contains delegate type need use, delegate name :
"x.y.delegate"
i have method in dll should called way :
method( delegatetype callbackmethod)
so, need :
define delegate in script of type
"x.y.delegate"
example$mydelegate
define callback method gets invoked when process event triggered"
note: i'm sorry if question seems silly, i'm absolute beginner.
update:
after reading comment , reading question more closely, think might looking utilize asynchronous event handling. below example listens events until timeout reached , exits. example assumes can change assembly add event.
a class 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(() => { (int = 0; < 3; i++) { thread.sleep(3000); onsomeevent(eventargs.empty); } }); } } }
powershell:
# load .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 event..." $event = wait-event -sourceidentifier $sourceidentifier -timeout 10 if ($event -eq $null) { write-host "no event received 10 seconds." break } # processing here write-host "processing event..." $event # remove event queue remove-event -sourceidentifier $sourceidentifier } unregister-event -sourceidentifier $sourceidentifier write-host "done processing events."
Comments
Post a Comment