Click here to Skip to main content
15,916,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am doing downloading of bulk files via sftp Rebex. Since I was getting out of memory exception for large volume of files , updated the code as required and duplication issue seen was rectified by registering and unregistered the event handling during each loop process. I want to to register event handler once and invoke the same without registering and unregistering during each loop

What I have tried:

Core Code is as below:
private List<SFTPFile> ListInwardCBFiles()
  {
     _listSftp = new Sftp();
     SFTPFile objSFTPFile;
     List<SFTPFile> ListSFTPFile = new List<SFTPFile>();
     try
     {
         if (ConnectSftp(_listSftp))
         {
             foreach (InwardMessageType t in Enum.GetValues(typeof(InwardMessageType)))
             {

                 foreach (CBExtension r in Enum.GetValues(typeof(CBExtension)))
                 {
                 var listItemReceived = new SftpListItemReceivedEventHandler((s, e) =>
                      {
                          SftpItem item = e.Item;
                          objSFTPFile = new SFTPFile();
                          objSFTPFile.FileType = Enum.GetName(typeof(InwardMessageType), t);
                          objSFTPFile.Extension = Enum.GetName(typeof(CBExtension), r);
                          objSFTPFile.Status = SFTPStatus.L.ToString();
                          objSFTPFile.FileName = item.Name.Trim();
                          objSFTPFile.FileSize = item.Size;
                          objSFTPFile.Mode = SFTPMode.D.ToString();
                          objSFTPFile.BankCode = Common.BANKCODE;
                          ListSFTPFile.Add(objSFTPFile);
                          Common.WriteLog(MethodInfo.GetCurrentMethod().Name + " FileType: " + objSFTPFile.FileType + " FileName: " + objSFTPFile.FileName, ServiceThread.Listing.ToString());
                          e.Ignore();
                      });
                     _listSftp.ListItemReceived += listItemReceived;  //register
                     _listSftp.GetList(GetSFTPServerPath(Enum.GetName(typeof(InwardMessageType), t)) + "//" + "*." + Enum.GetName(typeof(CBExtension), r));
                     _listSftp.ListItemReceived -= listItemReceived; //unregister
                 }
             }

         }
     }
     catch (Exception ex)
     {
         Common.WriteLog("Exception while listing files in the folder " + Environment.NewLine + ex.Message, ServiceThread.Listing.ToString());

     }
     finally
     {
         DisConnectSftp(_listSftp);
     }

     return ListSFTPFile;
 }
 #endregion
Posted
Updated 13-Mar-18 21:39pm
v2

1 solution

Why don't you just use a Boolean flag that determines if the code in your listItemReceived() method needs to be executed, then you only need to register the event handler once.
 
Share this answer
 
Comments
ranio 14-Mar-18 5:43am    
Even if Boolean option set to listItemReceived() how can we avoid the duplication since we are looping through each message type and their respective CB Extension types. Need to list the files with all message type and CB extension type present in a directory.
ranio 14-Mar-18 8:01am    
I have registered the event handler once without unregistering for each loop cycles. But I am unable to list files since it is unable to pass additional parameters(Inward message types and their respective values) to the event handler SftpListItemReceivedEventHandler .

Code is as below:
#region ListInwardCBFiles
private List<sftpfile> ListInwardCBFiles()
{
_listSftp = new Sftp();
SFTPFile objSFTPFile;
List<sftpfile> ListSFTPFile = new List<sftpfile>();
try
{
if (ConnectSftp(_listSftp))
{
objSFTPFile = new SFTPFile();
foreach (InwardMessageType t in Enum.GetValues(typeof(InwardMessageType)))
{

foreach (CBExtension r in Enum.GetValues(typeof(CBExtension)))
{

_listSftp.ListItemReceived += client_ListItemReceived;//register
_listSftp.GetList(GetSFTPServerPath(Enum.GetName(typeof(InwardMessageType), t)) + "//" + "*." + Enum.GetName(typeof(CBExtension), r));

}
}

}
}
catch (Exception ex)
{
Common.WriteLog("Exception while listing files in the folder " + Environment.NewLine + ex.Message, ServiceThread.Listing.ToString());

}
finally
{
DisConnectSftp(_listSftp);
}

return ListSFTPFile;
}
#endregion


void client_ListItemReceived(object sender, SftpListItemReceivedEventArgs e)
{

var listItemReceived = new SftpListItemReceivedEventHandler((s, e1) =>
{
SftpItem item = e1.Item;

objSFTPFile.FileType = Enum.GetName(typeof(InwardMessageType), t);
objSFTPFile.Extension = Enum.GetName(typeof(CBExtension), r);
objSFTPFile.Status = SFTPStatus.L.ToString();
objSFTPFile.FileName = item.Name.Trim();
objSFTPFile.FileSize = item.Size;
objSFTPFile.Mode = SFTPMode.D.ToString();
objSFTPFile.BankCode = Common.BANKCODE;
ListSFTPFile.Add(objSFTPFile);
Common.WriteLog(MethodInfo.GetCurrentMethod().Name + " FileType: " + objSFTPFile.FileType + " FileName: " + objSFTPFile.FileName, ServiceThread.Listing.ToString());
e1.Ignore();
});

_listSftp.ListItemReceived += client_ListItemReceived;//register
_listSftp.GetList(GetSFTPServerPath(Enum.GetName(typeof(InwardMessageType), t)) + "//" + "*." + Enum.GetName(typeof(CBExtension), r));

}
}

}
}
RickZeeland 14-Mar-18 9:27am    
Shouldn't SFTPFile objSFTPFile; be defined at Class level ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900