How to send message to another instance of application ?

  • Thread starter Thread starter Adam Nowotny
  • Start date Start date
A

Adam Nowotny

I want my application to be able to load most components it uses at user
logon (f.e. by running "prog.exe /startup") and create a NotifyIcon to
show up in tray.
Now when i load the prog.exe second time (without parameter) it should
detect that an instance is already running and tell it to show the main
form of the application (i need this to reduce application startup
time). Then the second instance should exit.
So far i've made a detection, if another instance is already running, by
using Process.GetProcessesByName.

The problem is how do i tell the first run prog.exe (from the second
one) to show all the forms ?
Using threading, appdomain (getdata?), writing lock file ?
 
CJ said:
See Remoting.

Thought about it, but it seems to be a bit too serious for a single
"signal", like "hej, show the forms, i'm getting outta here" :D

I just noticed that instead of using the Process class to detect other
instances (the ProcessName property takes several seconds to complete on
the first run) i can create an instance of Threading.Mutex object and
look at the last parameter passed by reference...thought maybe using the
Threading namespace would help in this case.
 
Thought about it, but it seems to be a bit too serious for a single
"signal", like "hej, show the forms, i'm getting outta here" :D

I just noticed that instead of using the Process class to detect other
instances (the ProcessName property takes several seconds to complete on
the first run) i can create an instance of Threading.Mutex object and
look at the last parameter passed by reference...thought maybe using the
Threading namespace would help in this case.

Yeah, I looked originally in the threading namespaces but found it couldn't
do the interprocess communication. I completely forgot about Mutex's, which
is silly because I used that exact idea a few months ago. =)

Does remoting seem a little excessive? Sometimes, its really not though. I
know it's buried deep in there but its really useful for something like
this. (I do miss DDE Link's from VB6 which took care of all the inter
process communication for you). You could write to a file and have a file
system watchter respond, but you limit yourself there to Win 2000+ only.
Not that there are a massive amount of machiens running NT 4/Win9x. But
there still are a few (i.e. my shop). And they can't run that.

So remoting, its safe, not that hard to do, and about 1,000 examples on here
and other resources such as DevX or Planet Source Code. Or MSDN for that
matter.

In the end, remoting is pretty lightweight from what I've seen (I've only
used it to send a few messages back and forth. Hasn't shown any problems in
my memory profiler. No delays (same machine) etc...

Anyways, hope I helped a little.

Good Luck,
CJ



 
CJ said:
Does remoting seem a little excessive? Sometimes, its really not though. I
know it's buried deep in there but its really useful for something like
this.

Won't there be any firewall problems, like ZA reporting about a
communication ? ;-)
I'll try to figure out something and report if find something...or else
just use remoting.
Anyway, thanks for help :)
 
Adam Nowotny said:
Won't there be any firewall problems, like ZA reporting about a
communication ? ;-)
I'll try to figure out something and report if find something...or else
just use remoting.
Anyway, thanks for help :)

If its inside the same machine it should use the local loopback, which ZA
and other personal firewall software should ignore. Also uses HTTP which
again most software is taught to ignore/allow.

If I'm wrong on this someone correct me.

-CJ

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top