Hi,
We are implementing a video display function using ImageViewerWpfControl and would like to confirm the timing of the Initialize() and Connect() calls.
[Observed Behavior]
When Connect() is called immediately after ImageViewerWpfControl.Initialize(), the video is not displayed.
[Workaround and Result]
When Connect() is called after waiting for a certain interval (approximately one second) following Initialize(), the video is displayed.
[Question ①: Specifications / Restrictions]
Are there any restrictions that prevent Connect() from being called immediately after Initialize(), such as a requirement to wait until an asynchronous internal initialization process has completed?
If such a restriction exists, we would also like to confirm the following:
Recommended method for confirming completion of Initialize() (whether a specific event/callback is available)
Recommended wait time, or an officially recommended implementation method instead of using a fixed Sleep
[Question ②: Confirmation of Implementation Approach]
Since using a fixed-duration Sleep may introduce environment-dependent risks, we would like to avoid it as a permanent solution. Instead, we are considering the following approach:
・After Initialize(), call Connect() to request the video stream
・Monitor the ImageDisplayed event, which is triggered when video data arrives
・After ImageDisplayed is triggered, rebuild the display component (subsequent processing)
Would there be any issue with using the ImageDisplayed event as the trigger for subsequent processing instead of using a fixed wait time?
Our proposed implementation is provided at the end for your review.
Also, if there are other internal states that cannot be detected through ImageDisplayed, such as incomplete initialization or connection failure, are there any other events that should be monitored?
[Environment]
XProtect version: XProtect Corporate 2025R2
MIP SDK version: MIP-SDK2025R2
OS: Windows 11 LTSC
Language / Framework: C# / .NET Framework 4.8
[Reproducibility]
Occurs every time
===(Proposed Implementation Related to ImageViewerWpfControl)===
■Excerpt 1: ImageViewerControlWrapper.cs (Event Forwarding Implementation)
This section forwards the ImageViewerWpfControl.ImageDisplayed event through our wrapper class. We would like you to confirm whether the event source and forwarding implementation are correct.
① Adding the Event Declaration
public delegate void ImageDisplayedEventHandler(object sender, EventArgs e);
/// Event triggered whenever an image is actually displayed on the screen.
/// Used to detect the arrival of video data (start of playback).
public event ImageDisplayedEventHandler OnImageDisplayed;
② Registering / Unregistering the SDK Event Handler
// Register
imageViewerControl.ImageDisplayedEvent += ImageViewerControl_ImageDisplayed;
// Unregister
imageViewerControl.ImageDisplayedEvent -= ImageViewerControl_ImageDisplayed;
③ Event Handler Implementation (Forwarding)
// In accordance with the SDK restriction that the handler should be kept very lightweight,
// Log().EnterDebug/ExitDebug are not called
private void ImageViewerControl_ImageDisplayed(object sender, EventArgs e)
{
try
{
OnImageDisplayed?.Invoke(sender, e);
}
catch (Exception ex)
{
Log().Error(ex);
}
}
④ Direct Hook to ImageViewerWpfControl.ImageDisplayed
_wpfViewer.ImageDisplayed += (s, e) => ImageDisplayedEvent?.Invoke(this, e);
/// Forwards the SDK’s ImageViewerWpfControl.ImageDisplayed event.
/// Triggered whenever an image is displayed on the screen, whether in Live or Playback mode.
public event ImageDisplayedEventHandler ImageDisplayedEvent;
■Excerpt 2: StreamingViewerBase.cs (Event Wait Implementation)
This is the common implementation for waiting for the ImageDisplayed event only once with a timeout. We would like you to confirm whether the event waiting and timeout handling are implemented correctly.
/// Waits until an image is actually displayed on the screen (the ImageDisplayed event is triggered) or until the timeout is reached.
/// Used to synchronize with the arrival of video data immediately after the view is recreated.
/// Timeout in milliseconds
/// true if display is detected; false if the operation times out
public abstract Task WaitForImageDisplayedAsync(int timeoutMs);
/// Common implementation of WaitForImageDisplayedAsync.
/// Monitors ImageViewerControlWrapper.OnImageDisplayed only once and
/// completes when either the event is triggered or the timeout occurs, whichever comes first.
protected static async Task WaitForImageDisplayedCoreAsync(ImageViewerControlWrapper control, int timeoutMs)
{
if (control == null)
{
return false;
}
var tcs = new TaskCompletionSource<bool>();
void Handler(object sender, EventArgs e) => tcs.TrySetResult(true);
control.OnImageDisplayed += Handler;
try
{
Task completedTask = await Task.WhenAny(tcs.Task, Task.Delay(timeoutMs));
return completedTask == tcs.Task;
}
finally
{
control.OnImageDisplayed -= Handler;
}
}
Implementation in each derived class (StreamingViewerMulti / StreamingViewerSingle):
public override Task WaitForImageDisplayedAsync(int timeoutMs)
{
return WaitForImageDisplayedCoreAsync(imageViewerControl, timeoutMs);
}
Excerpt 3: StreamingViewerTabItemControl.cs (Switching to Event-Based Waiting)
This section replaces the fixed-duration Task.Delay with an approach that waits for the ImageDisplayed event to be triggered before executing the subsequent processing.
We would like you to confirm whether this approach and implementation are appropriate.
[Event-Based Waiting]
_ = RunAfterImageDisplayedAsync(ctrl.Controls, v => v.Play(), BeginOutputViewOperation());
Common method that combines waiting for the event and executing the subsequent processing:
/// For a single view, waits with a timeout until video display starts (the ImageDisplayed event is triggered)
/// and then executes the specified processing.
/// At the point when the wait completes, cancellationT
Thanks,
Best Regards,
NVS Technical Support