Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,903 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
when running the application, it starts doing the code in the method but then after few seconds the application crash shutdown without showing any errors or exceptions and it's never reaching any of the catch places.
maybe there is a deadlock? not sure.
How to solve the problem?
this is the code:
private async Task ProcessFrame(CancellationToken cancellationToken)
{
try
{
using (Mat frame = new Mat())
{
if (!_videoCapture.Read(frame))
{
_timer.Stop();
labelStatus.Invoke((Action)(() => labelStatus.Text = "Error reading video file."));
return;
}
if (cancellationToken.IsCancellationRequested)
return;
if (_tracking && _firstFrame != null)
_roiPictureboxFrames = UpdateROI(_firstFrame, frame, _roiPictureboxFrames);
_roiPictureboxFrames = EnsureROIWithinBounds(frame, _roiPictureboxFrames);
var frameBitmap = BitmapConverter.ToBitmap(frame);
pictureBoxFrames.Invoke((Action)(() => pictureBoxFrames.Image = frameBitmap));
(Mat analyzedFrame, bool segmentDetected, bool blinkDetected) = await Task.Run(() => AnalyzeFrame(frame, _currentFrame), cancellationToken);
try
{
var analyzedBitmap = analyzedFrame != null ? BitmapConverter.ToBitmap(analyzedFrame) : null;
var statusText = analyzedFrame != null
? $"Segment detected in frame {_currentFrame}."
: $"Processing frame {_currentFrame}/{_totalFrames}.";
pictureBoxPoints.Invoke((Action)(() =>
{
pictureBoxPoints.Image = analyzedBitmap;
labelStatus.Text = statusText;
}));
trackBarFrames.Invoke((Action)(() =>
{
trackBarFrames.Value = _currentFrame;
labelFrames.Text = $"Displaying frame {_currentFrame + 1}/{_totalFrames}";
}));
_videoCapture.Set(VideoCaptureProperties.PosFrames, _currentFrame);
progressBarAnalysis.Invoke((Action)(() =>
{
progressBarAnalysis.Value = (int)((double)_currentFrame / _totalFrames * 100);
}));
_currentFrame++;
if (_currentFrame >= _totalFrames)
{
progressBarAnalysis.Invoke((Action)(() => progressBarAnalysis.Value = 100));
_timer.Stop();
labelStatus.Invoke((Action)(() => labelStatus.Text = "Analysis complete."));
}
_firstFrame = frame.Clone();
}
catch (Exception ex)
{
string yy = "";
}
}
}
catch (Exception ex)
{
// Log the exception (e.g., using a logging library)
// Re-throw the exception to terminate the application
_timer.Stop();
labelStatus.Invoke((Action)(() => labelStatus.Text = $"Error: {ex.Message}"));
}
}