VoW
VoW

Reputation: 315

How to send parameter to already running application when I open it second time?

I am trying to create a music player.

When I click a mp3 file it is opening in my application, but when I click on another mp3 file it is opening the application again. I need code to check if application is already running and if it is, don't open new application and send name of new mp3 file into already opened application.

I'm using the following code to check if application is running:

string processName = Path.GetFileNameWithoutExtension(
    Assembly.GetEntryAssembly().Location);

if(Process.GetProcessesByName(processName).Count() > 1) 
    return;

Now I need to send the new mp3 file path into the already running application. For example Windows Media Player and Gom Player have this function.

Upvotes: 2

Views: 2832

Answers (4)

João Angelo
João Angelo

Reputation: 57658

I'll assume Windows Forms application, so what you're trying to achieve is a single instance application that can be notified of subsequent starts of the application and instead of spawning up a new application instance handle the opening of the new file itself.

This scenario is supported by infra-structure classes provided by the .NET Framework in the Microsoft.VisualBasic.dll assembly, more specifically by the WindowsFormsApplicationBase class. Don't be fooled by the name, you can use it also in C# with no problems. I wrote about this scenario in the following blog post:

Single Instance Windows Forms Application

You can read it entirely to see the complete example, but the important part is the following snippet of code where you handle the arguments passed to the second instance directly in the first application instance:

class SingleInstanceApp : WindowsFormsApplicationBase
{
    public SingleInstanceApp()
        : base()
    {
        this.IsSingleInstance = true;
    }

    protected override void OnStartupNextInstance(
        StartupNextInstanceEventArgs e)
    {
        base.OnStartupNextInstance(e);

        string[] secondInstanceArgumens = e.CommandLine.ToArray();

        // Handle command line arguments of second instance

        if (e.BringToForeground)
        {
            this.MainForm.BringToFront();
        }
    }
}

Upvotes: 3

Daniel Fisher  lennybacon
Daniel Fisher lennybacon

Reputation: 4174

There are several ways to communicate between the two processes.

.NET Remoting: http://msdn.microsoft.com/en-us/library/kwdt6w2k(v=vs.71).aspx

WCF: http://msdn.microsoft.com/en-us/library/dd456779.aspx

...

After passing your 'check-if-instance-is-already-running' you start a listener that is waiting for external calls. If the check fails you start the client calling the listener passing the arguments. This way you can also have e.g. a tray icon sending for instance play/pause commands to your application.

Upvotes: 0

Mark Sowul
Mark Sowul

Reputation: 10600

I suggest a named mutex instead of GetProcessesByName. Anyway if you get the mutex, start a Remoting server. If you don't get the mutex, start a remoting client, connect to that server (the first running application), and use that to call a method on the server.

There's an MSDN sample here: http://msdn.microsoft.com/en-us/library/ms771662(v=VS.90).aspx

Upvotes: 4

Maximc
Maximc

Reputation: 1762

You can comminicate between apps on the same machine on multiple ways. Threading, WCF (with the right procotol) etc. Just google on of these things, you can also make something ugly like it writes the path to a file, and then the main app reads the file same location every 1 sec. If there is a path (to the new mp3 file). The main app will play it and then delete the path and keep checking.

Upvotes: 0

Related Questions