c# - Why does Runspace.OpenAsync() ignore InitialSessionState? -
i writing code interface powershell. had following:
using (var runspace = runspacefactory.createrunspace(initialsessionstate)) { runspace.open();
this code works fine. however, since code in async method, wrote following experiment:
public static task opentaskasync(this runspace runspace) { if (runspace.runspacestateinfo.state == runspacestate.opened) return task.fromresult<object>(null); var tcs = new taskcompletionsource<object>(); eventhandler<runspacestateeventargs> statehandler = null; statehandler = (o, e) => { if (e.runspacestateinfo.reason != null) { runspace.statechanged -= statehandler; tcs.trysetexception(e.runspacestateinfo.reason); } else if (e.runspacestateinfo.state == runspacestate.opened) { runspace.statechanged -= statehandler; tcs.trysetresult(null); } }; runspace.statechanged += statehandler; runspace.openasync(); return tcs.task; }
however, after changing code this:
using (var runspace = runspacefactory.createrunspace(initialsessionstate)) { await runspace.opentaskasync();
my tests fail. reason powershell can no longer find commands module imported initialsessionstate (prior runspace creation). cannot determine why is. have debugged , runspace open , available after task completes, , not proceed invoke commands until does. ideas?
i looked @ runspace open code in reflector , see 1 thing might explain it. in localrunspace.doopenhelper()
statechanged
event (when state changes "opened") occurs before modules imported. looks poll on default drive being set (in initialsessionstate.setsessionstatedrive()
) workaround.
Comments
Post a Comment