Unable to close and end Windows Forms app

Forum - I have a small WinForms app which inputs video from a webcam and displays the video in a pictureBox control. I'm using Visual C++ 2008 Windows Forms basically, with video handling done with modified OpenCV code (https://opencv.org/). Most of the code in the app works fine (input the picture and display it) but so far I have not been able to gracefully stop the app and shut it down.

Below is the code for the app:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once
#include "opencv2/opencv.hpp"
#include "iostream"
#include "Windows.h"
#include "stdlib.h"
#include "stdio.h"

namespace WebCamAN {
	using namespace cv;
	using namespace std;
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//TODO: Add the constructor code here
			//
		}
	protected:
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  Run;
	private: System::Windows::Forms::Button^  Stop;
	private: System::Windows::Forms::PictureBox^  pictureBox1;
	private:
		System::ComponentModel::Container ^components;
		//char a;
#pragma region Windows Form Designer generated code
		void InitializeComponent(void)
		{
	// Code generated by VisStud2008 IDE - constructors for buttons, pictureBox, etc.
		}
#pragma endregion
//------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------
	private: System::Void Run_Click(System::Object^  sender, System::EventArgs^  e) {
				 char a;
				 //AllocConsole();
				 //freopen("CONOUT$", "w", stdout);
				 VideoCapture capture(1);
				 Mat frame;
				 while(1)
				 {
					 //int a;
					 //char a;
					 capture.read(frame);
					 System::Drawing::Graphics^ graphics2 = pictureBox1->CreateGraphics();
					 System::IntPtr ptr2(frame.ptr());
					 System::Drawing::Bitmap^ b2 = gcnew System::Drawing::Bitmap(frame.cols, frame.rows, frame.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr2);
					 System::Drawing::RectangleF rect2(0,0,pictureBox1->Width, pictureBox1->Height);
					 graphics2->DrawImage(b2, rect2);

					 //delete graphics2;
					 //a = getchar();
	// This code is an attempt		 //printf(a);
	// at writing value of variable		 //if(a=='q'||a=='Q')
	// a to Output Window and at	 	//{
	// graceful shutdown.		 	//delete b2;
	// Doesn't work.			//delete pictureBox1->Image;
						//pictureBox1->Image = nullptr;
						//delete graphics2;
						//break;
					 //}
				 }
			 } // End of Run_Click

	private: System::Void Stop_Click(System::Object^  sender, System::EventArgs^  e) {
				 exit(0);			 
			 } // End of Stop_Click
	};
}


The above code for acquiring the video and displaying it in a pictureBox works fine. The commented-out snippet to stop the video and break the while() loop does not work; it compiles and runs but doesn't do anything. Several basically similar approaches I have tried do the same: compile and run but do nothing. I could really use some help from the Forum on how to stop the video, terminate the running while() loop, and close the app. Any help offered will be greatly appreciated. Thanks much.

Mick H.
4 Jan 22
1:51 PM PST USA
[Note this is C++/cli code]
Within an event handler like Run_Click(...) you shouldn't run an infinite loop like this. It blocks further input processing thus Stop_Click(...) is never called.

Instead consider a timer to show the current frame.
coder777 - thanks for the reply, I didn't even think about blocking. I'll have to try to put the video capture code in a function that doesn't block the thread; not sure how to do that as I have very limited experience with multi-thread coding. Anyway, now I know where the problem is. Thanks again.

Mick H
5 Jan 22
3:10 PM PST USA
Topic archived. No new replies allowed.