我有一个C#Web服务,它记录了来自IP摄像机的数据。当我在指定的时间段(例如10秒)内记录数据时,此服务运行良好。但是我的目标是在未指定的时间内完成数据记录,并让用户按一下以停止记录。因此,我修改了代码,创建了一个新的Web服务(stopRecording),以更改充当互斥体的全局变量的值。显然这是错误的,因为我已经对其进行了测试,但是我不知道如何进行。有谁能够帮助我?我真的很感激。
下面,我留下相关代码。
[WebMethod]
public string startRecording(string ipAddress)
{
// Connection preset for H.264 (HTTP API 3.0)
string Url = "axrtsp:://" + ipAddress + "/axis-media/media.amp?videocodec=h264";
string UserName = "username";
string Password = "pass";
string Path = "C:/directory/subdirectory/";
string Filename = "myRecordedFile.bin";
string FilePath = Path + Filename;
// Open binary output file to write parsed video frames into
using (FileStream outFileStream = new FileStream(FilePath, FileMode.Create))
using (outFile = new BinaryWriter(outFileStream))
{
try
{
// Register for events like OnVideoSample, OnAudioSample, OnTriggerData and OnError
...
// Set connection and media properties
...
// Get absolute time from Axis device
...
// Connect to the device
int cookieID;
int numberOfStreams;
object buffer;
parser.Connect(out cookieID, out numberOfStreams, out buffer);
// Write media type information to out file (buffer is an array of bytes)
byte[] mediaTypeBuffer = (byte[])buffer;
outFile.Write(mediaTypeBuffer.Length);
outFile.Write(mediaTypeBuffer, 0, mediaTypeBuffer.Length);
// Start the media stream and registered event handlers will be called
parser.Start();
Debug.WriteLine("Will start recording...");
do {
Debug.WriteLine("recording..."); //want to record during an unspecified time
} while (record); //my mutex variable that doesn´t make the thing even when I call the stopRecording Web Service. The program remains overlooping
System.Diagnostics.Debug.Write("Finish recording... never reached!!!!! ");
// Stop the stream
parser.Stop();
// Unregister event handlers
...
}
catch (COMException e)
{
Console.WriteLine("Exception for {0}, {1}", parser.MediaURL, e.Message);
}
// Inform the GC that COM object no longer will be used
Marshal.FinalReleaseComObject(parser);
parser = null;
Console.WriteLine("Stream stopped");
}
return "Recording from camera " + Url;
}
[WebMethod]
public string stopRecording()
{
System.Diagnostics.Debug.Write("I want to stop recording...");
record = false;
return "Stop";
}
您的记录变量不是Mutex对象,而是一个简单的标志...除此之外。
麻烦的是,您在startRecording()中编写的代码永远不会把手交回给父类,并且可能永远持有处理线程。
[如果我建议的话,为什么不创建线程进行录音?这里有很多可能性(new Thread(),Action.BeginInvoke(),..)
这样,您就有机会接收stopRecording并将此记录标志设置为false并离开记录线程。