Restrictions on Calling Connect() Immediately After ImageViewerWpfControl.Initialize()

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

For now, I am focusing on question 1.

No, there should be no restriction on how quickly you can call Connect() after Initialize(). In our samples using the image viewer, these calls are made immediately after each other, for example:

_imageViewerWpfControl1.Initialize();
_imageViewerWpfControl1.Connect();

I suspect there may be something else in your implementation contributing to the behavior.

Could you please run the VideoViewer sample unmodified and check whether you see the same issue?

That test will help determine whether you have discovered an issue with the control itself, or whether we should focus on troubleshooting your implementation.

We confirmed that the video could be viewed successfully in all 20 test runs using the VideoViewer sample.

Therefore, there may be some processing in our implementation that affects the timing of the Connect() call. However, we have confirmed that this issue does not occur when we start monitoring the ImageDisplayed event, which is triggered when video data arrives, before calling Connect().

Even if there is some processing in our implementation that affects the timing, can we consider monitoring the ImageDisplayed event before calling Connect() to be an effective measure for ensuring that Connect() works properly?

If possible, we would appreciate your response by this weekend—specifically, by Friday, November 30, Japan Standard Time.

I would still like to understand what in your code or environment causes the behavior you observe, since it is surprising and does not match the expected behavior from the documentation.

That said, taking a practical view, I do not see a problem with the workaround you propose.

Subscribing to the ImageDisplayed event is supported, and using that event should not in itself cause any problems. As with other event handlers, I would recommend keeping the processing done directly in the handler lightweight.

Given what you have investigated and observed, I can understand why you prefer this approach over using an arbitrary delay, and I have no objection to using ImageDisplayed as a workaround in this case.

I would just keep in mind that this is a workaround for the behavior you are seeing, rather than a documented requirement to wait for ImageDisplayed after Initialize().