Examples

Code examples and sample projects for the LogiKal C# API V3

Code examples #

Create LoginScope #

string installationPath = @"C:\Logikal";
string programMode = "Enter your program mode here";
using (IServiceProxyUiResult serviceProxyResult = ServiceProxyUiFactory.CreateServiceProxy(installationPath, programMode))
{
    IServiceProxyUi serviceProxy = serviceProxyResult.ServiceProxyUi;
    serviceProxy.Start();
    var parameters = new Dictionary<string, object>();
    parameters.Add(WellKnownParameterKey.Login.ProgramMode, programMode);
    parameters.Add(WellKnownParameterKey.Login.ApplicationHandle, GetWindowHandle());
    using (ICoreObjectResult<ILoginScopeUi> loginScopeResult = ServiceProxy.Login(parameters))
    {
        ILoginScopeUi loginScope = loginScopeResult.CoreObject;
        ...
    }
}

Get Project Information #

ILoginScopeUi loginScope = ...;
IProjectCenterInfo projectCenterInfo = loginScope.ProjectCenterInfos[0];
using (ICoreObjectResult<IProjectCenterUi> projectCenterResult = loginScope.GetProjectCenter(projectCenterInfo))
{
    IProjectCenterUi projectCenter = projectCenterResult.CoreObject;
    IList<IBaseProjectInfo> projectInfos = projectCenter.ChildrenInfos;
    foreach (var projectInfo in projectInfos)
    {
        Guid projectGuid = projectInfo.Guid;
        string projectName = projectInfo.Name;
    }
}

Lifetime and Disposing of Result Objects #

API methods return objects which inherit from IResult and IDisposable. In order to prevent memory leaks IResult objects need to be disposed if they aren’t needed anymore. Disposing of an IResult object causes all objects contained within the results to be disposed. If used without asynchronous methods the disposing can be managed through the using statement.

using (ICoreObjectResult<IProjectCenterUi> projectCenterResult = loginScope.GetProjectCenter(projectCenterInfo))
{
    ...
}

Are asynchronous methods used the disposing of an object might not be possible because a background operation prevents the disposing of the object or one of its children. In this case it needs to be checked with CanDispose if the result object is in a state to be disposed.

ICoreObjectResult<IProjectCenterUi> projectCenterResult = loginScope.GetProjectCenter(projectCenterInfo);
try
{
    ...
}
finally
{
    if (!projectCenterResult.CanDispose())
        WaitForUserToExit(projectCenterResult);
    else
        projectCenterResult.Dispose();
}

Usage of Can Methods #

Most methods in the API offer a Can method to check if a method can be executed with the given parameter. The Can methods have the same input parameters as the method they are guarding.

public interface IElevationUi
{
    IOperationInfo CanEditComment(IElevationInfo historicalElevationInfo);
    ICoreInfoResult<IElevationInfo> EditComment(IElevationInfo historicalElevationInfo);
}

A Can method returns an IOperationInfo which contains a bool field (CanExecute) which determines if the method can be executed with the given input parameters. In case it cannot be executed the object will contain a list of restrictions which explain why the method cannot be executed.

private void EditElevationComment(IElevationInfo elevationInfo)
{
    using (IOperationInfo operationInfo = _elevationUi.CanEditComment(elevationInfo))
    {
        if (!operationInfo.CanExecute)
            throw new Exception(operationInfo.ToString());
    }
    using (ICoreInfoResult<IElevationInfo> elevationInfoResult = _elevationUi.EditComment(elevationInfo))
    {
        if (elevationInfoResult.OperationCode == OperationCode.Accepted)
        {
            RefreshHistory();
        }
    }
}

Calling an Asynchronous Method (Begin / End) #

The API offers asynchronous methods by providing a Begin and an End method. The Begin method is used to start the asynchronous method in the background. The End method is then used to wait for the background operation to finish.

public interface IElevationUi
{
    IOperationInfo CanShow(IElevationEditMode elevationEditMode);
    IBeginShowSynchronizedOperationResult BeginShow(IElevationEditMode elevationEditMode);
    IOperationInfo CanEndShow(ISynchronizedOperation synchronizedOperation);
    ICoreInfoResult<IElevationInfo> EndShow(ISynchronizedOperation synchronizedOperation);
}
private async Task<ICoreInfoResult<IElevationInfo>> ShowAsync(IElevationEditMode editMode)
{
    using (IOperationInfo operationInfo = ElevationUi.CanShow(editMode))
        if (!operationInfo.CanExecute) return null;

    using (ISynchronizedOperationResult synchronizedOperationResult = ElevationUi.BeginShow(editMode))
    {
        using (IOperationInfo operationInfo = ElevationUi.CanEndShow(synchronizedOperationResult.SynchronizedOperation))
            if (!operationInfo.CanExecute) return null;

        ICoreInfoResult<IElevationInfo> coreInfoResult = await Task.Run(() =>
            ElevationUi.EndShow(synchronizedOperationResult.SynchronizedOperation)).ConfigureAwait(true);

        return coreInfoResult;
    }
}

API Demo UI Client (C# / WPF)

Demo application built with Visual Studio 2019 showcasing API V3 usage

Sample Applications

Sample applications showcasing specific API V3 use cases