Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Controlling a running process

Posted on 2006-07-12
13
Medium Priority
?
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1600 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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

597 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