简体   繁体   中英

Share memory between Powershell and C#

I'm developing a log search software by C#, and the underlayer of the software is achieved by powershell. There are two theards, one thread invoke powershell to search log, the log search function is achieved by powershell and it will give me the result when finishing executing the powershell. But i want to get the log realtime. I want the powershell put the logs that has been searched to a queue. and another thread of C# to get the result from the queue. I have tried write the logs that powershell has get to a file, and C# thread read the file. but it faild, because the file is occupied by another process(powershell). Now I want to share memory between powershell and C#, that means powershell can operate the C# variable so that i can put logs to the queue when powershell is searching logs. Could you please kindly tell me how can I achieve it? Thanks.

You have several options here. First, you don't have to use PowerShell's file output commands (Out-File/Set-Content). You can use the .NET FileStream/StreamWriter classes directly from PowerShell which allows you to control the FileAccess/FileShare options. Second, you could create a ConcurrentQueue (or BlockingCollection) and give that to PowerShell to write into. Third, you could use MSMQ. Fourth, although I haven't personally tried it, I don't see why the .NET memory mapped functionality wouldn't work in PowerShell. The last two options seem like overkill to me. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM