asp.net 页面使用了多线程去保存内容中的远程图片,应用程序池经常崩溃 是什么原因?
1 /// <summary> 2 /// 保存图片 3 /// </summary> 4 void SaveData() 5 { 6 ThreadPool.SetMaxThreads(2, 2); 7 string remoteImages = Request.Form["data"]; 8 if (!string.IsNullOrEmpty(remoteImages)) 9 { 10 string[] imgArr = remoteImages.Split(','); 11 if (imgArr.Length > 0) 12 { 13 14 ManualResetEvent[] manualEvents = new ManualResetEvent[imgArr.Length]; 15 for (int i = 0; i < imgArr.Length; i++) 16 { 17 manualEvents[i] = new ManualResetEvent(false); 18 ThreadPool.QueueUserWorkItem(SaveImageToFtp, new object[] { imgArr[i], manualEvents[i] }); 19 } 20 WaitHandle.WaitAll(manualEvents); 21 } 22 } 23 24 if (saveFilePathList.Count > 0) 25 { 26 Response.Write("{\"status\":\"success\",\"data\":["); 27 for (int i = 0; i < saveFilePathList.Count; i++) 28 { 29 string[] arr = saveFilePathList[i]; 30 if (i > 0) 31 { 32 Response.Write(","); 33 } 34 Response.Write("{\"originalImgSrc\":\"" + arr[0] + "\",\"nowImgSrc\":\"" + arr[1] + "\"}"); 35 } 36 Response.Write("]}"); 37 return; 38 } 39 40 41 42 Response.Write("{\"status\":\"failed\"}"); 43 } 44 45 46 int suffixNum = 0; 47 48 int GetRndNum() 49 { 50 suffixNum++; 51 return suffixNum; 52 } 53 54 55 void SaveImageToFtp(object o) 56 { 57 object[] objArr = (object[])o; 58 string imageUrl = objArr[0].ToString(); 59 60 ManualResetEvent mre = (ManualResetEvent)objArr[1]; 61 62 63 if (!string.IsNullOrEmpty(imageUrl)) 64 { 65 int dotInx = imageUrl.LastIndexOf("."); 66 if (dotInx > 0) 67 { 68 string path = string.Empty; 69 DateTime now = DateTime.Now; 70 string ext = imageUrl.Substring(dotInx); 71 string folderName = now.Year + "-" + now.Month; 72 73 string fileName = (now - (new DateTime(2000, 1, 1))).TotalMilliseconds.ToString().Replace(".", string.Empty) + GetRndNum() + ext; 74 path = "/" + folderName + "/" + fileName; 75 76 using (WebClient wc = new WebClient()) 77 { 78 byte[] data = wc.DownloadData(imageUrl); 79 80 if (watermarkEnable) 81 { 82 data = WatermarkData(data); 83 } 84 85 86 ManualResetEvent[] manualEvents = new ManualResetEvent[ftpList.Count]; 87 for (int i = 0; i < manualEvents.Length; i++) 88 { 89 manualEvents[i] = new ManualResetEvent(false); 90 ThreadPool.QueueUserWorkItem(SaveDataToFtp, new object[] { data, ftpList[i], manualEvents[i], folderName, fileName }); 91 } 92 WaitHandle.WaitAll(manualEvents); 93 94 95 saveFilePathList.Add(new string[] { imageUrl, ImageServerDomain + path }); 96 } 97 } 98 } 99 100 mre.Set(); 101 } 102 103 104 105 106 void SaveDataToFtp(object o) 107 { 108 object[] objArr = (object[])o; 109 byte[] data = (byte[])objArr[0]; 110 string ftpUri = objArr[1].ToString(); 111 ManualResetEvent mre = (ManualResetEvent)objArr[2]; 112 113 string folderName = objArr[3].ToString(); 114 string fileName = objArr[4].ToString(); 115 116 117 if (!FtpExistsFolder(ftpUri, "/", folderName)) 118 { 119 FtpCreateFolder(ftpUri, "/" + folderName + "/"); 120 } 121 122 bool success = FtpSaveFile(ftpUri, data, "/" + folderName + "/" + fileName); 123 124 mre.Set(); 125 }
事件查看器里怎么说?
解决了,是 ftp 服务器 到期了,服务器链接不上
@foxidea: 解决了就好。不过,类似的问题,跟踪下错误信息就能很容易发现的。