首页 新闻 赞助 找找看

winform利用HttpWebRequest进行数据采集程序运行一段时间后就自动关闭了

0
悬赏园豆:50 [已解决问题] 解决于 2012-11-29 19:31

用C#语言写的winform程序,运行一段时间后程序就自动关闭了,给整个事件加上try...catch也无法捕获到异常。在事件管理器中发现如下错误日志:

EventType clr20r3, P1 collect.winform.exe, P2 1.0.0.0, P3 508a69ce, P4 system.windows.forms, P5 2.0.0.0, P6 4f681deb, P7 1521, P8 12f, P9 system.notsupportedexception, P10 NIL.

查阅了相关资料,很有可能是HttpWebRequest的用法引起的,我的代码如下:

View Code
 1 public static string GetRequestData(string url)
 2         {
 3             WebRequest request = HttpWebRequest.Create(url);
 4             Stream  stream = null;
 5             WebResponse response = null;
 6             MemoryStream stmMemory = new MemoryStream();
 7             byte[] buffer = new byte[64 * 1024];
 8             int i;
 9             try
10             {
11                 response = request.GetResponse();
12                 stream = response.GetResponseStream();
13                 while ((i = stream.Read(buffer, 0, buffer.Length)) > 0)
14                 {
15                     stmMemory.Write(buffer, 0, i);
16                 }
17                 byte[] arraryByte = stmMemory.ToArray();
18                 return Encoding.GetEncoding("utf-8").GetString(arraryByte);
19             }
20             catch (Exception ex)
21             {
22                 throw ex;
23             }
24             finally
25             {
26                 stream.Close();
27                 stream.Dispose();
28                 response.Close();
29                 stmMemory.Close();
30                 stmMemory.Dispose();
31             }
32         }
33 
34         public static bool DownloadFile(string url, string fileFullpath)
35         {
36             WebRequest request = HttpWebRequest.Create(url);
37             request.Timeout = 6000;
38             WebResponse response =null;
39             Image imgObj = null;
40             try
41             {
42                 response = request.GetResponse();
43                 imgObj = Image.FromStream(response.GetResponseStream());
44                 if (!Directory.Exists(fileFullpath.Substring(0,fileFullpath.LastIndexOf("/"))))
45                     Directory.CreateDirectory(Directory.GetParent(fileFullpath).FullName);
46                 imgObj.Save(fileFullpath);
47             }
48             catch (Exception ex)
49             {
50                 throw ex;
51             }
52             finally
53             {
54                 response.Close();
55                 if (imgObj != null) imgObj.Dispose();
56             }
57             return true;
58         }

希望前辈们能够提供以下解决思路,谢谢。

 

psforever的主页 psforever | 菜鸟二级 | 园豆:461
提问于:2012-10-29 10:11
< >
分享
最佳答案
0

这个你是定时做的吗?

收获园豆:50
chenping2008 | 大侠五级 |园豆:9836 | 2012-10-29 15:19

没有用到定时。

psforever | 园豆:461 (菜鸟二级) | 2012-10-30 10:08

@psforever: 不做定时,你的程序只能运行一次,难道你用了无限循环的?

chenping2008 | 园豆:9836 (大侠五级) | 2012-10-30 10:13

@chenping2008: 我先将采集地址信息都获取到了,全部都放在一个集合中,然后再遍历这个集合,程序一般在运行了大概一天多的时间后就自动关了。

psforever | 园豆:461 (菜鸟二级) | 2012-10-30 13:50

@psforever: 数据量很大,难道是内存吃紧了?不太清楚楼主的程序是如何写的?

chenping2008 | 园豆:9836 (大侠五级) | 2012-10-30 14:39

@chenping2008: 数据量确实有点大,我之前也怀疑过时内存不够导致的,但是通过我长时间的监视(呵呵,其实就是每隔一段时间看看系统内存使用情况)发现,内存并无大幅度的变化。

我的实现方式基本上就是:我开辟了一个线程用于去采集,首先获取到所有的外层采集链接存入List集合中,然后遍历此集合采集链接页面的数据,然后在遍历采集最里层的链接数据。并且没采集成功一个添加一项数据ListView中。我用全局的Application_ThreadException事件也无法捕获到任何异常,程序在运行一天多的时间后就关闭了。

psforever | 园豆:461 (菜鸟二级) | 2012-10-30 16:05
其他回答(1)
0

使用多线程,判断掉了一个再自动开一个线程

jason2013 | 园豆:1998 (小虾三级) | 2012-10-29 10:16

但现在关键是我都不知道是具体是哪里出错,我怎么判断“掉了一个”?

支持(0) 反对(0) psforever | 园豆:461 (菜鸟二级) | 2012-10-29 12:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册