简体   繁体   中英

Only instances per user in the same session windows

Only instances per user in the same session windows.

I am making a program that several users can enter, but I can only open one instance per user. The problem is how to check if it is already running some instance that user and bring to the foreground application, but open for that user tries to enter.

I'm trying to assign or find out the active system processes an ID or something that can record and read so that when the application starts check if that user is already running some instance.

I'm now using Diagnostics.Process.GetProcessesByName (Diagnostics.Process.GetCurrentProcess.ProcessName) but only worth to have a running instance.

I've run into a similar problem before. where I was creating an application and I only wanted one instance to run. Then I created a

Here is how I would tackled it. I created a WCF Pipe Service:

[ServiceContract]
interface DNRIService 
{
    [OperationContract]
    void OpenDnt(string path);

    [OperationContract]
    void OpenPak(string path);

    [OperationContract]
    bool IsOnline();

    [OperationContract]
    void Activate();


}

Then I did this :

public Main(String [] args) : this()
{
    try
    {
        using (ChannelFactory<DNRIService> serviceFactory = new ChannelFactory<DNRIService>(new NetNamedPipeBinding(), new EndpointAddress(PipeService)))
        {
            var channel = serviceFactory.CreateChannel();
            if(channel.IsOnline())
            {
                foreach (var argument in args)
                {
                    var argTrim = argument.Trim() ;
                    if (argTrim.EndsWith(".dnt"))
                        channel.OpenDnt(argument);
                    else if (argTrim.EndsWith(".pak"))
                        channel.OpenPak(argument);
                }
                channel.Activate();
                Close();
            }
        }
    }
    catch
    {
        @this = new ServiceHost(this, new Uri(PipeName));
        @this.AddServiceEndpoint(typeof(DNRIService), new NetNamedPipeBinding(), PipeService);
        @this.BeginOpen((IAsyncResult ar) => @this.EndOpen(ar), null);

        foreach (var argument in args)
        {
            var argTrim = argument.Trim();
            if (argTrim.EndsWith(".dnt"))
                OpenDnt(argument);
            else if (argTrim.EndsWith(".pak"))
                OpenPak(argument);
        }
    }
}

Notice that it tries to open with the same name. If it can't then it closes and assumes that another one is already open. It then tries to send its arguments to the original.

You only need to do a slight modification to get the current user. This question should help you with that: How do I get the current username in .NET using C#?

The rest is for you to implement. If you want to see the full code from which I posted its located here :

https://github.com/Aelphaeis/Dragon-Nest-Resource-Inspector

(Running it will give you an idea of how it works in only allowing a single instance).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM