5

I've made MP3 Files on my computer to associate with my media player. So when i double click a MP3 file in the explorer, obviously the media player fires up and plays it.

My problem I can prevent multiple instances popping up, but how can i transfer the file path of the new instance to the instance i am using already

What i want to happen: I double click a song,... and im listening to it I double click another song and the current song should stop and new song should start playback, without popping up a new instance of the application.

I use Mutex to check whether there is already a running instance. To get file path i use Environment.GetCommandLineArgs()

All I need is to pass the file's path to the current instance. How can I do this??

Sylens
  • 1,097
  • 8
  • 31

3 Answers3

3

Essentially you will have to build this into your application.

When a user clicks a second song, you would have to have some way for the current instance to kill the previous instance of your application or send it a message.

These seems a little dirty to me.

I think a clean approach would be to write a small app_booter.exe that is associated with your media files. When a media file is clicked, it starts this _app_booter_ which :

  1. Looks for a running instance of your application.
  2. Send data to it to stop the current song and play this new one.
  3. If the app is not running, it just starts it with the song on the command line.

To send your application data from this other exe you could take a look at IPC: Interprocess communication for Windows in C# (.NET 2.0)

I think the WM_COPYDATA approach mentioned in the above post, or a tcp socket would suffice since your data is pretty simple, just a file path and maybe a command.

I think keeping two separate exes will be easier and cleaner than building your app to talk to its previous instance.

Community
  • 1
  • 1
gideon
  • 19,329
  • 11
  • 72
  • 113
2

I think you are looking for this http://wpfinstanceawareapp.codeplex.com/

Provides the functionality for a single instance application that can pass the command line arguments to the first instance.

Bob Vale
  • 18,094
  • 1
  • 42
  • 49
0

In your Main after checking your mutex, get the song from the arguments and fire a Play(FileInfo filename) event to play the song. Your form should handle this event (or better a controller class)

Odys
  • 8,951
  • 10
  • 69
  • 111