multithreading - out of memory exception while using multithread in C# -
i new threading , stuck here on issue. have application using multi threading. have function uploads thousands of images on ftp server. each images creating new thread. thread calls function connect ftp server, upload file , returns boolean value if uploaded.
my problem is, since uploading thousands of images , each creating own thread, after sometime out of memory exception , application freezes.
my codes follows:
public int16 uploadfiles(string[] files) { foreach (var filename in files) { if (uploadfile(filename)) { strlogtext += "\r\n\tfile: " + filename + " downloaded."; } } } private bool uploadfile(string filename) { var blndownload = false; var thread = new thread(() => downloadfilenow(filename, out blndownload)) {isbackground = true}; thread.start(); return blndownload; } private void downloadfilenow(string filename, out bool blndownload) { //get file path , name on source ftp server var srcfolder = getsrcfoldername(filename); //get local folder name downloaded files var trgfolder = getlocalfolder(filename, "d"); var reqftp = (ftpwebrequest) webrequest.create(new uri("ftp://" + _strsourceftpurl + srcfolder + "/" + filename)); reqftp.method = webrequestmethods.ftp.downloadfile; reqftp.usebinary = true; reqftp.credentials = new networkcredential(_strsourceftpuser, _strsourceftppassword); var outputstream = new filestream(trgfolder + "\\" + filename, filemode.create); try { var response = (ftpwebresponse) reqftp.getresponse(); var ftpstream = response.getresponsestream(); const int buffersize = 2048; var buffer = new byte[buffersize]; if (ftpstream != null) { int readcount = ftpstream.read(buffer, 0, buffersize); while (readcount > 0) { outputstream.write(buffer, 0, readcount); readcount = ftpstream.read(buffer, 0, buffersize); } ftpstream.close(); } response.close(); blndownload = true; } catch (webexception ex) { _log.writelog("error in downloading file (" + filename + "):\r\n\t" + ex.message, ""); //delete newly created file local system outputstream.close(); if (file.exists(trgfolder + "/" + filename)) file.delete(trgfolder + "/" + filename); } catch (exception ex) { _log.writelog("error in downloading file (" + filename + "):\r\n\t" + ex.message, ""); } { outputstream.close(); outputstream.dispose(); } blndownload = false; }
please , let me know how can limit number of threads being created @ time, there not more 10-20 threads running.
you can't create many threads. 1 alternative use parrelle extensions.
public void uploadfiles(string[] files) { files.asparallel().forall(filename => { if (uploadfile(filename)) { strlogtext += "\r\n\tfile: " + filename + " downloaded."; } }); }
Comments
Post a Comment