Im与C#共同开展一个项目。 该方案从一个连接的网络运动中获取图像,并通过Socket将其发送到Pi/iPad。 所有这些工作都很出色,我能够成功地看到我的行踪。
But when the client disconnects, the webcam has to get turned off and in this function, the program just hangs up. No error messages and no exception calls...just hangs! I believe it s a problem with multiple threads but unfortunately I m not as experienced in C# to find a solution. I hope someone here can bring me on the right track...
Code:
onImageCaptured function:
public void OnImageCaptured(Touchless.Vision.Contracts.IFrameSource frameSource, Touchless.Vision.Contracts.Frame frame, double fps)
{
_latestFrame = frame.Image;
Console.WriteLine("OnImageCaptured");
if (isConnected)
{
Console.WriteLine("OnImageCaptured - isConnected");
byteArray = new byte[0];
MemoryStream stream = new MemoryStream();
_latestFrame.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Close();
byteArray = stream.ToArray();
if (byteArray.Length > 0)
{
string eof = "<EOF>";
byte[] eofByte = Encoding.ASCII.GetBytes(eof);
Console.WriteLine("OnImageCaptured - sendStream");
this.onDataSend(byteArray);
this.onDataSend(eofByte);
stream.Flush();
}
System.Diagnostics.Debugger.Log(0, "1", "
Byte Array Length: " + byteArray.Length.ToString());
}
pictureBoxDisplay.Invalidate();
}
象照相机级一样:
public event EventHandler<CameraEventArgs> OnImageCaptured;
触发:
OnImageCaptured.Invoke(this, new CameraEventArgs(bitmap, fps));
因此,这一功能——我相信的话——面临一种单独的威胁,因为当图像出现时,不阻挡统一。
因此,在这一职能中处理客户脱节问题:
public void onDataSend(byte[] data)
{
clientReady = false;
try
{
socketWorker.Send(data);
}
catch (SocketException se)
{
isConnected = false;
Console.WriteLine("Error: Data Write - SocketException");
Console.WriteLine(se.ErrorCode.ToString());
thrashOldCamera() // THIS FUNCTION HANGS THE PROGRAM !!
onDisconnectServer();
// onDisconnectServer();
}
catch (ObjectDisposedException)
{
isConnected = false;
Console.WriteLine("Error: Data Write - ObjectDisposedException");
// onDisconnectServer();
}
}
用户脱节,thrashOldCamera(
)。 至今为止的工程罚款! 现在:
private void thrashOldCamera()
{
Console.WriteLine("TrashOldCamera");
// Trash the old camera
if (_frameSource != null)
{
try
{
_frameSource.NewFrame -= OnImageCaptured;
Console.WriteLine("TrashOldCamera - 1");
_frameSource.Camera.Dispose(); // HERE IT HANGS. IT NEVER GOES PAST HERE !!!
Console.WriteLine("TrashOldCamera - 2");
setFrameSource(null);
Console.WriteLine("TrashOldCamera - 3");
pictureBoxDisplay.Paint -= new PaintEventHandler(drawLatestImage);
}
catch (Exception ex)
{
Console.WriteLine("End Trash Camera Ex: " + ex);
}
}
Console.WriteLine("End Trash Camera");
}
The program hangs at _frameSource.Camera.Dispose();
. As stated above, there s no error or exception.
It may be a problem that onDataReceive()
gets called within the onImageCapture
function().
I also added a button to the form that triggers thrashOldCamera()
and this works perfectly.
任何帮助/帮助都得到真正赞赏。