Controlling a running process

Please let me know if this is possible, and if so how (using c#) :

User starts my application with optional command line arguments. The application starts in the background, maintains a list of the arguments passed
If the user starts the app again with some more arguments, the first instance of the app should be detected, and the arguments should be appended to the existing list (instead of creating a new process and new list)
I googled and found many ways to detect the running instances (and getting the Process object representing the first instance). What I need to know is, how do I use this object and manipulate its members (like adding to the argument list etc.) ?

Will making my app a windows service help? If so, I still need to know how to achieve the above.

LVL 4
v_karthikAsked:
Who is Participating?
 
sumixConnect With a Mentor Commented:
I think there are several ways to do that, I'll just try to give some hints.

 You may communicate with another process (send data to it) using SendMessage API function and WM_COPYDATA message. The receiving form will have to override its WndProc method, in order to process the received data. An example that does this is:
 http://www.thecodeproject.com/csharp/wm_copydata_use.asp

 Another approach is to store an object in memory and let it be accessed by other processes:
http://www.vbaccelerator.com/home/NET/Code/Libraries/Windows_Messages/Simple_Interprocess_Communication/article.asp

 Speaking of what 2005 has special, there is a nice tool from Microsoft called ManagedSpy, that allows you to access the controls in another process and change their properties. It comes with source code.
 http://msdn.microsoft.com/msdnmag/issues/06/04/ManagedSpy/
0
 
Bob LearnedCommented:
What version of .NET do you have?  2005 can do this seamlessly, but 2003 is a kludge.

Bob
0
 
v_karthikAuthor Commented:
2003, unfortunately.  Whats special in 2005?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Bob LearnedCommented:
Sorry, this is the C# area (got mixed up) :(

Here is a screen shot from VB.NET 2005 project properties:
    https://filedb.experts-exchange.com/incoming/ee-stuff/375-VB.NET-2005---Single-Instance-Ap.png

Take a look at this CodeProject entry, and see if it helps:

Single Instance Application in C#
http://www.codeproject.com/csharp/singleinstance.asp

Bob
0
 
v_karthikAuthor Commented:
I saw that codeproject stuff already. As I said, I think theres enough resource to detect and block multiple instances. The trouble is, how do I grab the members of that single instance and manipulate its attributes.
0
 
v_karthikAuthor Commented:
Thanks for the reply. I thought of another simpler way that can serve my purpose - the sender process dumping some info to a text file, and receiver monitoring the file every few seconds or so. I'm not used to the sendmessage api or ipc, but the links you've given seem to be interesting. I'll wait for a few more days, after that, no matter I use this method or not I'll accept your answer.

0
 
Bob LearnedCommented:
1) Are you thinking of using a FileSystemWatcher?

2) I like your approach on the simple scale with the file monitoring, but most people shoot me down when I suggest it, so I leave it out most of the time now.

Bob
0
 
v_karthikAuthor Commented:
1) No. I just need some way to communicate with a running background process. Think of it like this... the sender process is a context menu extension that lives only for a short period. Within that period, it needs to pass user's arguments to a background application / windows service.

2) I know its not the best way of doing things, which is why I would like to know the recommended way. I've not done windows programming much.. this "file touch" approach works for all languages and all platforms :D ..  One question I have is, the WM_COPYDATA tutorial link says it can be used only for "limited" amount of data. I'm not sure what this means. The amount of data I'm passing is highly dynamic. For example, if a user points to a folder, I may need to enumerate all the files in it and pass it to the background process.
0
 
sumixCommented:

When you send data between processes using SendMessage method, the data resides in memory and this is the reason why it is not recommended to manipulate big amount of data this way. In your case you could pass just a path from where the receiving process should copy its data.

It is true that it may take a little effort to understand the platform invoke (it is just about calling unmanaged methods from managed code) and how to process windows messages from c# (overriding WndProc), but once you get used to it, it may offer a solution as simple as the one that monitors a file.

I suggest you to download the sample code from the second link I provided; there is a ready to use class 'CopyData', and a sample Form that uses this class.
0
 
v_karthikAuthor Commented:
Yes, Im trying out from the 2nd link. I guess I can skip the gory details for now and use the CopyData class as a wrapper.

One question here ... Is it mandatory to have a window handle to send and receive?  The receiver process, in my case, is going to be a windows service (no forms). Do I need to create a dummy hidden form and assign it to CopyData?  Similarly, for the sender, I thought I'll put this send data functionality in a static helper class. Is it a must to have a window handle to send the data?
0
 
sumixCommented:

From what I saw, CopyData is derived from NativeWindow class, and a handle is assigned to it using AssignHandle method. Try using a simple IntPtr variable as parameter for this method after you create CopyData instances.
0
 
v_karthikAuthor Commented:
It seems to work fine. But one trouble is, it doesnt communicate properly if one application passes message to a different app (it works between 2 instances of the same app).  

I've decided to communicate using datagrams. I'll open a port in 127.0.0.1 and have the receiver listening to a port.
0
 
v_karthikAuthor Commented:
I'll accept your first answer, but I'll be grateful if you could explain why it fails between 2 different apps. I've narrowed it down to a certain call where the receiver method tried to unmarshall the received data and copy it to COPYDATASTRUCT. This call returns a 0 length data if the data arrives from a different app.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.