为什么我收到“操作已超时”消息什么时候不是超时?
我正在开发一个向 IP 摄像机发出 HTTP 请求的应用程序。每次发出 HTTP 请求时,我都会收到在屏幕上绘制的图片。所有这些过程都是这样完成的:
- 我有一个每 500 毫秒调用一次的计时器。
- 计时器中的代码调用执行 http 请求的线程。
所以很有可能当定时器被调用的时候,http请求还没有完全完成,这样就ok了。
问题是,有时,由于未知原因,我收到异常“操作已超时”。所以我做了一个操作日志。我记录http请求之前的时间和之后的时间。它总是在 300-400 毫秒左右。我还记录了异常情况,令我惊讶的是记录的时间约为 24 或 76 毫秒。我的超时设置为 5000 毫秒,所以它永远不会超时!
在我所有的测试中,我从未发现记录的时间超过 800 毫秒,而且这低于设置的超时时间。
还有其他原因可以解释“操作已超时”错误吗?我也尝试 ServicePointManager.DefaultConnectionLimit = 200;
但它没有改变任何东西。
多谢!
这是线程化的代码。 ListTest 是记录器,然后将每一行打印到文件中。
StructTakePicture structTP = (StructTakePicture)structTakePicture;
ServicePointManager.DefaultConnectionLimit = 200;
string strFileName = structTP.FolderGUID + "input" + GetNumeroPhoto(structTP.Cam.ID, structTP.NumPhoto) + ".jpg";
DateTime dateDebut = DateTime.Now;
try
{
ListTest.Add(strFileName + " --- BEGIN : " + dateDebut.ToString());
WebRequest WebRequestObject = HttpWebRequest.Create(String.Format("http://{0}/mjpg/snapshot.cgi?camera={1}", structTP.Cam.TotalIP, structTP.Cam.View));
WebRequestObject.Timeout = 5000;
WebRequestObject.Credentials = new NetworkCredential("admin", "admin");
HttpWebResponse ResponseObject = (HttpWebResponse)WebRequestObject.GetResponse();
string strTypeRetour = ResponseObject.ContentType;
if (strTypeRetour == "image/jpeg")
{
MemoryStream memoryStream = new MemoryStream(0x10000);
using (Stream responseStream = WebRequestObject.GetResponse().GetResponseStream())
{
byte[] buffer = new byte[0x1000];
int bytes;
while ((bytes = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
memoryStream.Write(buffer, 0, bytes);
}
ResponseObject.Close();
}
byte[] response = memoryStream.ToArray();
Image img = byteArrayToImage(response);
img.Save(strFileName);
structTP.StopEverything = false;
DateTime dateFin = DateTime.Now;
TimeSpan span = dateFin.Subtract(dateDebut);
ListTest.Add(strFileName + " --- TOTALTIME:" + span.Milliseconds.ToString());
ListTest.Add(strFileName + " --- END : " + dateFin.ToString());
}
}
catch (System.Net.WebException err)
{
structTP.StopEverything = true;
DateTime dateFin = DateTime.Now;
TimeSpan span = dateFin.Subtract(dateDebut);
ListTest.Add(strFileName + " === ERROR :" + span.Milliseconds + " | " + err.Message);
}
* 编辑*
为了回答评论,我得到的错误是在 System.Net.WebException 中,而 err.Message 是“操作已超时”。
* 编辑 2 *
这是我用代码编写的日志的一部分。正如您所看到的,超时是在非常短的响应时间内收到的。
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00013.jpg --- BEGIN : 2011-10-27 08:16:46
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00010.jpg --- TOTALTIME:353
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00010.jpg --- END : 2011-10-27 08:16:47
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00006.jpg --- TOTALTIME:610
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00006.jpg --- END : 2011-10-27 08:16:47
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00008.jpg --- BEGIN : 2011-10-27 08:16:47
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00014.jpg --- BEGIN : 2011-10-27 08:16:47
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00009.jpg --- BEGIN : 2011-10-27 08:16:47
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00005.jpg --- TOTALTIME:996
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00005.jpg --- END : 2011-10-27 08:16:48
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00004.jpg --- TOTALTIME:800
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00004.jpg --- END : 2011-10-27 08:16:48
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00007.jpg === ERROR :22 | The operation has timed out
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00015.jpg --- BEGIN : 2011-10-27 08:16:48
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00014.jpg --- TOTALTIME:391
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00014.jpg --- END : 2011-10-27 08:16:49
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00009.jpg === ERROR :23 | The operation has timed out
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00008.jpg --- TOTALTIME:526
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00008.jpg --- END : 2011-10-27 08:16:50
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00012.jpg --- TOTALTIME:461
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00012.jpg --- END : 2011-10-27 08:16:50
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00015.jpg --- TOTALTIME:780
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00015.jpg --- END : 2011-10-27 08:16:50
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00011.jpg --- TOTALTIME:49
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00011.jpg --- END : 2011-10-27 08:16:50
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00009.jpg --- TOTALTIME:133
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00009.jpg --- END : 2011-10-27 08:16:50
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00007.jpg --- TOTALTIME:140
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00007.jpg --- END : 2011-10-27 08:16:51
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00013.jpg === ERROR :28 | The operation has timed out
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00010.jpg --- BEGIN : 2011-10-27 08:16:56
I'm doing an application that do HTTP request to an IP camera. Each time I do an HTTP request, I receive a picture that draw on the screen. All this process is done like this:
- I have a timer that is called each 500 ms.
- The code in the timer call a thread that do an http request.
So there is a big possibility that when the timer is called, the http request is not completely done and it's ok like that.
The problem is that sometime, for an unknown reason, I receive the exception "The operation has timed out". So I did a log of the operation. I log the time before the http request and the time after. It's always arround 300-400 ms. I also did a log in the exception and my big surprise was that the logged time is like 24 or 76 ms. My timeout is set to 5000 ms so it should never timeout!
In all my test, I've never found a logged time bigger than 800 ms and that's way under the set timeout.
Is there any other reason that could explain the error "Operation has timed out"? I also try ServicePointManager.DefaultConnectionLimit = 200;
but it doesn't change anything.
Thanks a lot!
Here is the code that is threaded. The ListTest is the logger, each line is then printed to a file.
StructTakePicture structTP = (StructTakePicture)structTakePicture;
ServicePointManager.DefaultConnectionLimit = 200;
string strFileName = structTP.FolderGUID + "input" + GetNumeroPhoto(structTP.Cam.ID, structTP.NumPhoto) + ".jpg";
DateTime dateDebut = DateTime.Now;
try
{
ListTest.Add(strFileName + " --- BEGIN : " + dateDebut.ToString());
WebRequest WebRequestObject = HttpWebRequest.Create(String.Format("http://{0}/mjpg/snapshot.cgi?camera={1}", structTP.Cam.TotalIP, structTP.Cam.View));
WebRequestObject.Timeout = 5000;
WebRequestObject.Credentials = new NetworkCredential("admin", "admin");
HttpWebResponse ResponseObject = (HttpWebResponse)WebRequestObject.GetResponse();
string strTypeRetour = ResponseObject.ContentType;
if (strTypeRetour == "image/jpeg")
{
MemoryStream memoryStream = new MemoryStream(0x10000);
using (Stream responseStream = WebRequestObject.GetResponse().GetResponseStream())
{
byte[] buffer = new byte[0x1000];
int bytes;
while ((bytes = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
memoryStream.Write(buffer, 0, bytes);
}
ResponseObject.Close();
}
byte[] response = memoryStream.ToArray();
Image img = byteArrayToImage(response);
img.Save(strFileName);
structTP.StopEverything = false;
DateTime dateFin = DateTime.Now;
TimeSpan span = dateFin.Subtract(dateDebut);
ListTest.Add(strFileName + " --- TOTALTIME:" + span.Milliseconds.ToString());
ListTest.Add(strFileName + " --- END : " + dateFin.ToString());
}
}
catch (System.Net.WebException err)
{
structTP.StopEverything = true;
DateTime dateFin = DateTime.Now;
TimeSpan span = dateFin.Subtract(dateDebut);
ListTest.Add(strFileName + " === ERROR :" + span.Milliseconds + " | " + err.Message);
}
* EDIT *
To answer the comments, the error I get is in the System.Net.WebException and the err.Message is "Operation has timed out".
* EDIT 2 *
Here is a part of a log I did with the code. As you can see, the timeout is received with very tiny response time.
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00013.jpg --- BEGIN : 2011-10-27 08:16:46
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00010.jpg --- TOTALTIME:353
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00010.jpg --- END : 2011-10-27 08:16:47
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00006.jpg --- TOTALTIME:610
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00006.jpg --- END : 2011-10-27 08:16:47
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00008.jpg --- BEGIN : 2011-10-27 08:16:47
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00014.jpg --- BEGIN : 2011-10-27 08:16:47
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00009.jpg --- BEGIN : 2011-10-27 08:16:47
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00005.jpg --- TOTALTIME:996
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00005.jpg --- END : 2011-10-27 08:16:48
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00004.jpg --- TOTALTIME:800
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00004.jpg --- END : 2011-10-27 08:16:48
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00007.jpg === ERROR :22 | The operation has timed out
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00015.jpg --- BEGIN : 2011-10-27 08:16:48
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00014.jpg --- TOTALTIME:391
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00014.jpg --- END : 2011-10-27 08:16:49
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00009.jpg === ERROR :23 | The operation has timed out
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00008.jpg --- TOTALTIME:526
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00008.jpg --- END : 2011-10-27 08:16:50
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00012.jpg --- TOTALTIME:461
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00012.jpg --- END : 2011-10-27 08:16:50
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00015.jpg --- TOTALTIME:780
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00015.jpg --- END : 2011-10-27 08:16:50
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00011.jpg --- TOTALTIME:49
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00011.jpg --- END : 2011-10-27 08:16:50
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00009.jpg --- TOTALTIME:133
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00009.jpg --- END : 2011-10-27 08:16:50
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00007.jpg --- TOTALTIME:140
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00007.jpg --- END : 2011-10-27 08:16:51
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input1_00013.jpg === ERROR :28 | The operation has timed out
C:\Users\jfcote\AppData\Local\Temp\d1785720-afc6-4822-b02d-fdf6d2f2c0d1\input2_00010.jpg --- BEGIN : 2011-10-27 08:16:56
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您设置的
Timeout
值是GetResponse
响应的时间。HttpWebRequest
还有一个 ReadWriteTimeout 读取或写入时使用的值。您没有设置ReadWriteTimeout
,因此GetResponse
可能会在超时内返回,但读取超时。我建议您尝试以下修改:
其他观察结果:
您的日志不完整,例如,文件
input1_0007
有错误,但没有 BEGIN 行。您的ListTest
是线程安全的集合吗?如果不是,两个线程同时更新它很可能会损坏列表。另外,您说您的代码每 500 毫秒发出一次请求。但您的日志在一秒内显示了三个请求。
当然,这并不能解释超时,除非由于某种原因 ServicePointManager 或其他东西由于太多未完成的请求而决定终止它。您可以查看异常堆栈跟踪以查看抛出超时异常的位置。
此外,您可能会考虑更改代码,以便在第一个请求完成之前不会向相机发出另一个请求。因此,您可以启动一个延迟 500 毫秒的一次性计时器,而不是每 500 毫秒触发一次的计时器。计时器回调获取图片,然后重新初始化计时器 500 毫秒。这样,就不会出现超过一个未完成的图片请求,并且可以避免奇怪的并发问题。就目前情况而言,图片可能会乱序显示。
我认为您遇到了并发问题。如果多个线程可以同时执行此代码(您指出这是可能的),那么您的
ListTest
可能会被损坏,除非它是某种线程安全的列表。The
Timeout
value you're setting is the amount of time for theGetResponse
to respond. TheHttpWebRequest
also has a ReadWriteTimeout value that is used when reading or writing. You're not settingReadWriteTimeout
, so it's possible thatGetResponse
is returning within the timeout, but the read is timing out.I would suggest you try the following modification:
Additional observations:
Your log is incomplete, for example, there's an ERROR for file
input1_0007
, but no BEGIN line for it. Is yourListTest
a thread-safe collection? If not, two threads updating it at the same time could very well corrupt the list.Also, you said that your code is making a request every 500 ms. But your log shows three requests within a single one-second period.
Granted, that doesn't explain the timeout, unless for some reason the
ServicePointManager
or something else decided to kill it because of too many outstanding requests. You might look at the exception stack trace to see where the timeout exception was thrown.Also, you might consider changing your code so that it never makes another request to the camera until the first one is done. So instead of a timer that fires every 500 ms, you start a one-shot timer with a 500 ms delay. The timer callback gets the picture and then re-initializes the timer for another 500 ms. This way, there is never more than one outstanding request for a picture, and you avoid weird concurrency problems. As it stands now, it's possible for pictures to be displayed out of order.
I think you're having concurrency problems. If multiple threads can be executing this code at the same time (you indicated that it is possible), then your
ListTest
can get corrupted unless it's a thread-safe list of some kind.