Creating a Custom Progress Reporter
Interacting with Connector UI
The Connector UI offers several methods to manage its state and interact with the user interface. These methods allow you to control the visibility of the UI, update displayed information, and send notifications to users.
Setting Progress Messages
To display progress messages during long-running operations, use the SetProgressMessage method:
bridge.SetProgressMessage("Loading exchange...");
The SetProgressMessage method displays messages on the Connector UI’s Progress screen. This method is effective only when a progress screen is already visible, typically during operations like exchange creation or loading. Progress messages are automatically cleared when long-running operations complete. The host application does not need to manage the visibility of the progress screen; showing and hiding of this screen are handled internally by the Connector UI. There are no IInteropBridge APIs to directly control the progress screen’s visibility. The host application’s role is limited to providing informative messages when appropriate.
Creating New Base Model
CustomReadWriteModel
The CustomReadWriteModel class serves as a foundational base for implementing data exchange operations. It provides core functionality for reading from and writing to a data exchange, ensuring essential capabilities common to models that perform such operations. Additionally, it offers features like data caching for efficient management of exchange data and logging to facilitate tracking and debugging of exchange-related activities. Designed for extensibility, this class enables derived classes to build upon its features and customize functionality to meet specific requirements.
Here is a minimal implementation:
public class CustomReadWriteModel : BaseReadWriteExchangeModel
{
public CustomReadWriteModel(Client client) : base(client)
{
}
public override List<DataExchange> GetCachedExchanges()
{
return new List<DataExchange>();
}
public override async Task<List<DataExchange>> GetExchangesAsync(ExchangeSearchFilter exchangeSearchFilter)
{
return new List<DataExchange>();
}
public override async Task<bool> SelectElementsAsync(List<string> exchangeIds)
{
return false;
}
public override async Task<IEnumerable<string>> UnloadExchangesAsync(List<ExchangeItem> exchanges)
{
return Enumerable.Empty<string>();
}
public override async Task UpdateExchangeAsync(ExchangeItem exchangeItem, CancellationToken cancellationToken = default)
{
}
}
When your model caches exchanges (via GetCachedExchanges), use the ForceRefresh flag on the ExchangeSearchFilter passed to GetExchangesAsync to decide whether to serve cached data or re-read from the host. ForceRefresh is true only when the user clicks the Refresh button in the Connector UI; programmatic refreshes leave it false. For the full pattern, see Refreshing the exchange list.
Ensuring Thread Safety for UI Updates
Main Thread Invoker
The MainThreadInvoker ensures that UI actions and other main-thread-specific tasks are executed safely on the main UI thread. It provides a mechanism for InteropBridge to schedule and invoke asynchronous operations on the main thread, ensuring thread safety for operations such as UI updates.
Below are sample implementations for known host applications, provided as a reference for implementing IMainThreadInvoker with the specific thread management API in your host application.
Sample WPF Main Thread Invoker
public class WpfMainThreadInvoker : IMainThreadInvoker
{
private readonly Dispatcher dispatcher;
public WpfMainThreadInvoker(Dispatcher dispatcher)
{
this.dispatcher = dispatcher;
}
public async Task<T> InvokeAsync<T>(Func<Task<T>> func)
{
if (this.dispatcher.CheckAccess())
{
return await func();
}
var operation = await this.dispatcher.InvokeAsync(func);
return await operation;
}
}
Sample WinForm Main Thread Invoker
public class WinFormsMainThreadInvoker : IMainThreadInvoker
{
private readonly ISynchronizeInvoke synchronizeInvoke;
public WinFormsMainThreadInvoker(ISynchronizeInvoke synchronizeInvoke)
{
this.synchronizeInvoke = synchronizeInvoke ?? throw new ArgumentNullException(nameof(synchronizeInvoke));
}
public async Task<T> InvokeAsync<T>(Func<Task<T>> func)
{
if (synchronizeInvoke.InvokeRequired)
{
return await (Task<T>)synchronizeInvoke.Invoke(new Func<Task<T>>(() => func()), null);
}
return await func();
}
}
These implementations ensure that UI actions are executed on the main thread, maintaining thread safety and preventing potential issues related to asynchronous operations.