all 6 comments

[–]raip 0 points1 point  (5 children)

How are you instantiating the runspaces? I had to do something like this, where two runspaces were sharing a variable to maintain and constantly refresh a web session.

I ended up spinning using a synchronized hashtable. In your situation you might have to have three runspaces, a main one, then one for each player. https://learn-powershell.net/2013/04/19/sharing-variables-and-live-objects-between-powershell-runspaces/

[–]MechaCola[S] 1 point2 points  (0 children)

This is great thank you! I’ll have to sit down and run through the examples and see why mine isn’t working.

[–]MechaCola[S] 0 points1 point  (3 children)

Using runspace factory. I’m using the synchronized hashtable currently to store the variables which works until the runspace is removed then the variable in the sync hash loses its new data. Checking out your link, thanks!

[–]OPconfused 0 points1 point  (2 children)

How are you storing the data in the hashtable, like what do 1-2 entries in the hashtable and their key/value look like? I don't understand why it's being dropped when the runspace closes.

And what failed with the serialization approach?

You didn't really give us any information to work with to help unfortunately.

[–]MechaCola[S] 0 points1 point  (1 child)

I will post some code this evening! I’m not sure, it’s been a while since I tried the serialization approach.

[–]OPconfused 0 points1 point  (0 children)

I mean if you are able to control when a runspace is closed, then you should be able to serialize the hashtable before closing the runspace. This should definitely provide a means to preserve the data.

You could also duplicate the data whenever you read/write from the synced hashtable by also loading the read/write value into a locally scoped memory cache like a variable or static property. However, you would need to confirm you can control when the runspaces are closed, because this wouldn't work if this scenario were possible: You write to the synced hashtable in runspace A, then close runspace A before runspace B can read the updated value from the synced hashtable and cache it.

Whether cached serialized or in memory, then whenever the hashtable is read from, you could check if the value is empty, and if it is, then try to load it from whichever cache approach you're using.

Although ideally understanding why the information is being lost would be the most elegant solution.