cooldoger

i want to open several files by using the same programe, just like ultraEdit, when open a new file, it check whether ultraEdit is in processing, if it is, then open the file use the ultraEdit process with a new "tab".

so i wonder how to achieve it in c#, and i have use follow code to check whether a program is in processing and get the process. And then, how can i open the new file use the exist process

private static Process RunningIntance()
{
Process currentProcess = Process.GetCurrentProcess();
Process[] processCollection = Process.GetProcessesByName (currentProcess.ProcessName);
foreach (Process p in processCollection)
{
if (p.Id != currentProcess.Id) {
if (System.Reflection.Assembly.GetExecutingAssembly ().Location.Replace("/", "\\") == currentProcess.MainModule.FileName)
return currentProcess;
}
}
return null;
}



Re: Visual C# General how to use the same process to open several files

TaylorMichaelL

You could do it that way (and indeed you'll need to use a similar method in order to find the handle) however most apps that want to detect multiple instances of themselves use a named synchronization object. A semaphore or mutex is often used. The algorithm is as follows:

Attempt to create an instance of a named semaphore or mutex.
If the create succeeds then this is the first instance.
If the create failed then another instance of the app is running.

In .NET the Semaphore class has a constructor that creates the semaphore if it doesn't exist or opens the existing one if it does. It has an out parameter that specifies whether it was created or not. This is the cleanest approach.

Now that you know whether the app is already running or not you need to communicate with it. The easiest way is to send a custom message to the main window. You'll have to find the window by enumerating the running processes looking for your application. You already gave the code earlier. This code is expensive so that is why you use a sync object to detect the common case.

Once you have the process object you can use the MainWindowHandle property to get access to the main window. Using SendMessage (unmanaged API, there might be a managed way) you can send a custom message that contains the file to open. In your main window you'll have to override WndProc to look for this message, retrieve the parameter and open the file.

The only final challenge is the message itself. You shouldn't just pick a number and use that as a message as it could already be reserved for someone else. Within an app you can do this but across processes you shouldn't. Instead you'll have to register a custom message with Windows and then unregister it when you're done (at app close). This is unmanaged code. Use RegisterWindowMessage to get a message ID that you can use. The string you specify for the message should be globally unique. Any two applications that use the same message string will get back the same ID. This method allows your two different processes (even though they are the same executable) to communicate properly.

Michael Taylor - 10/31/06





Re: Visual C# General how to use the same process to open several files

cooldoger

it's very helpful. thank u Michael Taylor



Re: Visual C# General how to use the same process to open several files

Mohe

Hi you can find a easier and more clear way here:

http://www.codeproject.com/csharp/ncksingleinstance.asp