Reputation: 3074
I am creating a music player in C#.
It's a single instance application made in C# [WinForms
]. Currently for making it single instance I am using the following method.
public static Process PriorProcess()
{
Process curr = Process.GetCurrentProcess();
Process[] procs = Process.GetProcessesByName(curr.ProcessName);
foreach (Process p in procs)
{
if ((p.Id != curr.Id) && (p.MainModule.FileName == curr.MainModule.FileName))
{
return p;
}
}
return null;
}
Now whenever we pass some song to the application, if the application is already running then the song path is passed to already running instance using the SendMessage()
function and WM_COPYDATA
structs. It works fine.
But when I select some songs together for example 10 songs at a time and press enter, for a second 10 instance is created but they don't send WM_COPYDATA
message to the already running instance.
So, what's the proper way to make single instance and handle data passing to already running instance. Or if, any instance is not running, then?
What I wanted to achieve is when someone selects few songs and just press enter then how can I load those songs?
Upvotes: 1
Views: 263
Reputation: 3684
The easiest way is to use the VB class Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
, which is a normal .NET class
.
You need to add a reference to Microsoft.VisualBasic
assembly:
This handles the single instance problem and solves passing the data to the other application:
What is the correct way to create a single-instance WPF application?
Upvotes: 0