Solved

Controlling a running process

Posted on 2006-07-12
13
291 Views
Last Modified: 2010-04-16
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.

0
Comment
Question by:v_karthik
  • 7
  • 3
  • 3
13 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17093884
What version of .NET do you have?  2005 can do this seamlessly, but 2003 is a kludge.

Bob
0
 
LVL 4

Author Comment

by:v_karthik
ID: 17093953
2003, unfortunately.  Whats special in 2005?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17094071
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
 
LVL 4

Author Comment

by:v_karthik
ID: 17094437
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
 
LVL 12

Accepted Solution

by:
sumix earned 400 total points
ID: 17100021
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
 
LVL 4

Author Comment

by:v_karthik
ID: 17100689
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 96

Expert Comment

by:Bob Learned
ID: 17100850
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
 
LVL 4

Author Comment

by:v_karthik
ID: 17100995
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
 
LVL 12

Expert Comment

by:sumix
ID: 17106234

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
 
LVL 4

Author Comment

by:v_karthik
ID: 17108563
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
 
LVL 12

Expert Comment

by:sumix
ID: 17113750

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
 
LVL 4

Author Comment

by:v_karthik
ID: 17113784
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
 
LVL 4

Author Comment

by:v_karthik
ID: 17113789
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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now