Reputation: 634
The scenario: We wrote a small program, let's call it MyLittleProggy.exe, that can be launched and it will continue to run, basically just idling.
MyLittleProggy.exe contains a public class Class1
Now we run AnotherProggy.exe
This is what we're trying to do:
Both applications will be written for .NET Framework 4.8
Is this possible and if so, how do I go about it? Do I work from the Process object I find, using its Modules, perhaps? Or do I need to use System.Reflection? Please note I am not looking for a fully worked out example, just a few pointers in the right direction as I am currently a bit lost in the weeds, and as a result I am also not sure how to ask the right questions so please be kind.... I'd be happy with short snippets of code in either C# or VB
Thank you
Upvotes: 3
Views: 134
Reputation: 6237
I'll give you two options, one that simply notifies MyLittleProggy
each time AnotherProggy
launches, and a second that sends data from AnotherProggy
to MyLittleProggy
.
For the "notification only" version, we'll use a named event. Here's MyLittleProggy
:
Sub Main(args As String())
Console.WriteLine("I just started up")
Dim namedEvent As New EventWaitHandle(False, EventResetMode.AutoReset, "MyExampleEvent")
Console.WriteLine("I just finished one-time initialization")
For i = 1 To 5
Console.WriteLine("Waiting to be notified that other app started")
namedEvent.WaitOne()
Console.WriteLine("I just got notified that the other app started")
Next
namedEvent.Dispose()
Console.WriteLine("I just quit")
End Sub
And here's AnotherProggy
:
Sub Main(args As String())
Console.WriteLine("AnotherProggy just started")
Dim namedEvent As New EventWaitHandle(False, EventResetMode.AutoReset, "MyExampleEvent")
Console.WriteLine("Initialization done")
namedEvent.Set()
Console.WriteLine("Just triggered event")
namedEvent.Dispose()
Console.WriteLine("I quit")
End Sub
This method uses an event that can be referenced by name by any program. The "heavy" MyLittleProggy
does its heavy initialization once and then waits until something else triggers the event. The "light" application just triggers the event.
Let's move on to the version that passes data. It uses a named pipe. Here's MyLittleProggy
:
Sub Main(args As String())
Console.WriteLine("I just started up")
Dim namedPipe As New Pipes.NamedPipeServerStream("MyExamplePipe")
Dim pipeReader As New StreamReader(namedPipe)
Console.WriteLine("I just finished one-time initialization")
For i = 1 To 5
Console.WriteLine("Waiting for other app to send data")
namedPipe.WaitForConnection()
Console.WriteLine("Reading data from other app")
Dim pipeData = pipeReader.ReadToEnd()
Console.WriteLine($"Data from other app: {pipeData}")
namedPipe.Disconnect()
Next
pipeReader.Dispose()
namedPipe.Dispose()
Console.WriteLine("I just quit")
End Sub
And here's AnotherProggy
:
Sub Main(args As String())
Console.WriteLine("I just started")
Dim namedPipe As New Pipes.NamedPipeClientStream("MyExamplePipe")
Dim writer As New StreamWriter(namedPipe)
Console.WriteLine("Initialization done")
namedPipe.Connect()
writer.Write("Hello")
writer.Flush()
Console.WriteLine("Just sent data")
Console.WriteLine("Cleaning up")
writer.Dispose()
namedPipe.Dispose()
Console.WriteLine("Quit")
End Sub
Conceptually this is very similar to the first example - you wait on an object to tell you when the lightweight application was launched. The difference here is that you are waiting on a pipe connection instead of a named event, and then you read from the pipe.
Hopefully this is enough to get you started.
Upvotes: 2