async Task method with await Task.Run make application crash shutdown without any errors/exceptions. how to solve?

Chocolade 516 Reputation points
2024-06-14T00:32:37.6766667+00:00

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}"));
    }
}

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,868 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,575 questions
{count} votes