tags:

views:

51

answers:

2

My VB.NET WinForms program (parent) calls another VB.NET Console program (child) with Process.Start(). The child application runs quickly and I would like a status message returned to the parent. How can I send a string from the child to parent?

Is there some built-in way because of the parent-child relationship or must I use some other interface like sockets?

+1  A: 

You can have your calling application send command line arguments via Proccess.Start(), which your called app then uses.

Depending on your requirements / architecutre, this is either a good, simple design; or a terrible idea.

I'll assume the former; but if it is the latter, I highly recommend using the WCF for this. Direct socket calls have long been the way of the dodo, save for high volume/performance apps like video games. And with the WCF you can have a client sending it's server app data in very few lines of code.

What are your requirements here? Will the messages be sent frequently or just on the start? How often does that happen?

dferraro
I need the child program to simply accept a command line argument, load a file, send a summary message back, and exit. The only part I can't figure out is "send a summary message back". The child program most-likely wouldn't even take a half-second to complete.
Steven
In that case, have your parent call Process.Start and have the child write to StandardOutput/StandardError. You can read those back in the parent by redirecting those calls. http://stackoverflow.com/questions/206323/how-to-execute-command-line-in-c-get-std-out-results
Chris Haas
This sounds like my assumption was right. You don't want to be getting into the business of using command line arguments for IPC for this. I would strongly recommend using the WCF; it's *very* high level, try typing 'WCF Tutorial' into google. Here's one link that I briefly skimmed through and it looked straight-forward: http://www.switchonthecode.com/tutorials/wcf-tutorial-basic-interprocess-communication
dferraro
Does the child simply call Console.Write() or should I use a different method?
Steven
@Chris, what about when you have to deal with concurrency, file permissions, file shares, etc?? Unless this is some hobby app you are doing at home for personal reasons, I don't think that's a good idea. If this is any kind of LOB / mission critical application then you're just asking for trouble... We're talking about maybe a few extra days of work for a mid-level software engineer to refactor this to using WCF. Hell, for 150$, I will do it, it should only take a few hours ;)
dferraro
@dferraro not sure what you're talking about. Concurrency isn't an issue because this a separate app. Permissions/shares is a problem with Process.Start() in general but if he can call that then he can access StandardOut/Error.
Chris Haas
@Steven, yep, just write to Console. The nice part of this is that you've also got a command-line app that doesn't even need the parent app necessarily.
Chris Haas
won't there be concurrency issues when you have 100 winforms clients calling this console 'server'? What happens in this scenario? What about even just 10 clients?
dferraro
He's calling a command-line program. If 100 winform clients all call this then each client/machine will create a separate isolated process for the EXE, run it and retrieve the information. Is this the best way to work with 100 winform clients? Probably not, but that's not his question.
Chris Haas
I guess I was mostly concerned about security, among other things... (he did mention exploring sockets, etc..).. better impersonate some account who only has rights to that share containing the exe in your winforms code. Otherwise it will only take one malicious user to simply browse to the folder and delete your 'server'. This just screams bad design, but to each their own...
dferraro
+1  A: 

Just to add some code to my other comment, imagine the following simple child program:

Module Module1
    Sub Main()
        Console.WriteLine("This is a test")
    End Sub
End Module

And here's the parent:

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim P As New Process()
        P.StartInfo.FileName = "Child.exe"
        P.StartInfo.RedirectStandardOutput = True
        P.StartInfo.UseShellExecute = False
        P.Start()
        Dim T = P.StandardOutput.ReadToEnd()
        'Do something with T
    End Sub
End Class
Chris Haas