How to Allow the second instance if it have different arguments?

Jun 12, 2014 at 10:05 AM
Hi Blade,

I have a requirement where I will need to call my main exe many times but will only allow to have 1 window with 1 args so the same arguments cant be used until the 1st window is closed.

Is this possible using your library?

I am using WPF with caliburn. thanks alot
Jun 12, 2014 at 11:15 AM
The InstanceAwareApplication class uses an identifier (Guid) to create both the synchronization and communication objects.
This means that as long as the Guid is different, the same executable with a different Guid defines a different 'instance context'.
By default, the Guid is retrieved from the assembly Guid property, but you can leave it blank and override GenerateApplicationGuid, to provide a custom value at runtime (actually, I could change the implementation, so that overriding GenerateApplicationGuid overrides the assembly guid, without being forced to leave it blank...).

So, as long as different (but consistent) Guids are generated given the current application args, yes, this implementation can handle your scenario.

An example:
protected virtual Guid GenerateApplicationGuid()
{
    var args = Environment.GetCommandLineArgs();
    switch (args.FirstOrDefault())
    {
        case "1":
            return new Guid("00A46AB8-048F-44DE-A854-74BAD1CB709B");
        case "2":
            return new Guid("00A46AB8-048F-44DE-A854-74BAD1CB70CC");
    }
    return new Guid("00A46AB8-048F-44DE-A854-74BAD1CB70DD");
}
The above code would generate 3 different ids, so 3 different instance contextes. Note that in this case, the 3 contextes would not be able to communicate among themselves (eg. the instance started with arg "1" would not receive a next instance notification form an instance called with arg "2").

If you need communication among all processes, you need to modify the code a bit, to provide next instances with the current list of active instances and startup args. Just let me know if this is a requirement for you, or separated instance contextes are suitable for you.