OnTransferProgress

I can't find any samples or posts describing how onTransferProgress works in c# or in vb.net, only vb6 and I can't seem to convert it and get it working. Shouldn't there be an onTransferProgressEventArgs or something?

Please give me a simple sample.

If you think my question isn't sufficient, please tell me what more you need to help me in this matter.

I have come this far:

ftp2.OnTransferProgress += new IFTPConnectionEvents_OnTransferProgressEventHandler(ftp2_OnTransferProgress);

that should be right. but I don't know how to construct my :

private void ftp2_OnTransferProgress(...

it should look something like

private void ftp2_OnTransferProgress(object sender, OnTransferProgressEventArgs e) {  ... }

Hello ..

Please get the latest version from:
https://www.smartftp.com/ftplib/download

I've added event handling to the C# and the VB.NET samples.

Regarding your specific question:


private void ftp2_OnTransferProgress(int nBytesTransferredLo, int nBytesTransferredHi)

{

  // Get the bytes transferred from the property!

  ftp2.LastTransferBytes;

}


Regards,
-Mat

Thanks for you quick reply!

I tried the new version and the code you suggested, I still can't find any event code in the samples.

When I try the code you provided I get rid of the errors = thats good. But it doesn't fire, and now the file won't upload, it just stops on 0 kb and the application freezes.


		private void ftp2_OnTransferProgress(int nBytesTransferredLo, int nBytesTransferredHi)

		{

			// Get the bytes transferred from the property!

			Console.WriteLine(ftp2.LastTransferBytes);

			//Application.DoEvents();

		}





		public void upload()

		{



			Identd.Start(txtIdentd.Text);



			string DirName = txtDir.Text;

			int ftpPort = Convert.ToInt32(txtPort.Text);





			// Load License Key

			sfFTPLib.Global global = new sfFTPLib.Global();

			if (global.LoadLicenseKeyData("xxx"))

				System.Console.WriteLine("");



			// Create an instance of the Ftp class.

			ftp2 = new FTPConnectionMTA();



			ftp2.OnTransferProgress += new IFTPConnectionEvents_OnTransferProgressEventHandler(ftp2_OnTransferProgress);



			ftp2.Host = txtHostname.Text;

			ftp2.Port = Convert.ToInt32(txtPort.Text);

			ftp2.Username = txtUsername.Text;

			ftp2.Password = txtPassword.Text;

			ftp2.LogFile = "ftp.log";

			ftp2.Passive = true;

			ftp2.Timeout = 10;

			ftp2.LISTOption = 6;

			ftp2.PassiveForceIP = false;





			ftp2.DataProtection = enumDataProtection.ftpDataProtectionPrivate;



			if (chkSSLEnabled.Checked)

			{

				ftp2.Protocol = enumProtocol.ftpProtocolSSLExplicit;

			}



			sfFTPLib.enumError err = ftp2.Connect();



			if (err == sfFTPLib.enumError.ftpErrorSuccess)

			{





				ftp2.MakeDirectory(DirName);

				Console.WriteLine(ftp2.LastReply.ToString());



				ftp2.ChangeDirectory(DirName);



				if((ftp2.UploadFile(txtSaveTo.Text + fileName, DirName + fileName, 0, 0) == sfFTPLib.enumError.ftpErrorSuccess))

				{

					System.Console.WriteLine("DownloadFile() successful.");

					System.Console.WriteLine("LastTransferBytes = {0} B", ftp2.LastTransferBytes);

					System.Console.WriteLine("LastTransferTime = {0} s", ftp2.LastTransferTime);

					System.Console.WriteLine("LastTransferSpeed = {0} B/s", ftp2.LastTransferSpeed);

				}

				else

				{

					System.Console.WriteLine("DownloadFile() failed. LastError = {0}", ftp2.LastError);

					System.Console.WriteLine("LastReplyCode = {0}", ftp2.LastReplyCode);

					System.Console.WriteLine("LastReply = {0}", ftp2.LastReply);

				}

	   }

private void ftp2_OnTransferProgress(int nBytesTransferredLo, int nBytesTransferredHi)
--->Console.WriteLine(ftp2.LastTransferBytes);

When calling a method from the FTPConnectionMTA object an exception is created.

Unable to cast COM object of type 'sfFTPLib.FTPConnectionMTAClass' to interface type 'sfFTPLib.IFTPConnection'.
This operation failed because the QueryInterface call on the COM component for the interface with IID '{65D75061-6343-4124-9D64-64F1EABB34C2}'
failed due to the following error: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY))."}
System.Exception {System.InvalidCastException}

I'm trying to figure out why this is the case. In the meanwhile calculate the BytesTransferred from the function arguments:


private void ftp2_OnTransferProgress(int nBytesTransferredLo, int nBytesTransferredHi)

		{

			Int64 nTransferBytes = nTransferBytesHi;

			nTransferBytes <<= 32;

			nTransferBytes += nTransferBytesLo;   

			Console.WriteLine("TransferBytes = {0}", nTransferBytes);		 

		}


-Mat


private void ftp2_OnTransferProgress(int nBytesTransferredLo, int nBytesTransferredHi)

		{

			Int64 nTransferBytes = nTransferBytesHi;

			nTransferBytes <<= 32;

			nTransferBytes += nTransferBytesLo;   

			Console.WriteLine("TransferBytes = {0}", nTransferBytes);		 

		}


-Mat
[/quote]


that works fine, it prints out the transfered bytes into the console, but when I try to add code that infects the UI like progressbar.Value it hits me with exceptions

"A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll"

And when the process is done it says:

"Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang."


nothing else than printing to console seems to work :/ Is there maybe another way of affecting a progressbar with your lib?

Thanks for all your help.

I fixed it Or at least found a way around it to make it work!

I just started my uploadfile function as a new thread and invoked the mainform from it. now it runs smoothly

Yep that's the correct way when GUI interaction is required.

The latest version (.21) allows you to access the FTPConnection object in the events as well now. I'm not sure if this causes a new issue when updating the GUI from the events. The reason is that the events are called from a MTA thread and the native GUI in .NET runs in a STA thread. Previously the events were called from a STA thread. Please let me know if you have any problems.

Thanks for bringing this issue up.

Regards,
-Mat