Hello. I use the AForge framework to capture images from the camera. Capture occurs during the execution of an event. But by the time the AcceptTcpClientAsync method is executed, the video variable (bitmap picture) is still null. How to make the event run before the AcceptTcpClientAsync method? Next, the bitmap variable is needed in the CodingImages method.

private void Form1_Load(object sender, EventArgs e) { StartCapture(); Form1 async = new Form1(51510); async.Start(); Console.ReadLine(); } public void StartCapture() { VidoeCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); FinalVideo = new VideoCaptureDevice(VidoeCaptureDevices[0].MonikerString); FinalVideo.VideoResolution = FinalVideo.VideoCapabilities[2]; FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame); FinalVideo.Start(); Thread.Sleep(2000); } void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs) { video = (Bitmap)eventArgs.Frame.Clone(); } public async void Start() { //Thread.Sleep(10000); IPAddress ipAddre = IPAddress.Loopback; TcpListener listener = new TcpListener(ipAddre, _listeningPort); listener.Start(); LogMessage("Server is running"); LogMessage("Listening on port " + _listeningPort); while (true) { //CaptureImageFromCamera captureFromCamera = new CaptureImageFromCamera(); //Bitmap image = captureFromCamera.StartCapture(); CodingImage img = new CodingImage(); buffer = img.CodingImages(video); count = buffer.Length; lenght = buffer.Length.ToString(); LogMessage("Waiting for connections..."); try { var tcpClient = await listener.AcceptTcpClientAsync(); HandleConnectionAsync(tcpClient); } catch (Exception exp) { LogMessage(exp.ToString()); } i++; } } 

    1 answer 1

    It is enough to create a TaskCompletionSource<> , put it into a completed state when a frame is received, and wait for the associated tsc.Task task before establishing connections.

     TaskCompletionSource<bool> tcs; // ... void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs) { video = (Bitmap)eventArgs.Frame.Clone(); tcs.TrySetResult(true); } // ... await tcs.Task; CodingImage img = new CodingImage(); buffer = img.CodingImages(video); count = buffer.Length; lenght = buffer.Length.ToString(); LogMessage("Waiting for connections..."); try { var tcpClient = await listener.AcceptTcpClientAsync(); HandleConnectionAsync(tcpClient); } catch (Exception exp) { LogMessage(exp.ToString()); } 

    In addition, you should add synchronization of access to the video field, because a reordering of a record into it and a call to tsc.TrySetResult() from the point of view of the stream that receives connections may occur.

    • Hmmm, if you do as you suggested, then the System.NullReferenceException crashes. But about the "synchronization of access to the field," I did not quite understand. Could you explain? - Igor Vasilyev
    • This is not a telepathic, give a full setrays. Also, do you remember to create an instance of TaskCompletitionSource ? - AlexeyM
    • it seems like I forget. But how to do it? - Igor Vasilyev
    • Create an instance of the type. tcs = new TaskCompletitionSource<bool>() - AlexeyM