Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
5k views
in .NET FTP by (160 points)
Hi, I'm trying to make an applikation that upploads 10 files at the same time to an FTP server.

What I do is the following: I create 10 ftpClient objects, and open 10 connections to the FTP. I then run the following code for 10 files:

if (aryFtpT[j].busy == false)
{
aryThreads[j] = new Thread(new ThreadStart(aryFtpT[j].threadPutMethod));
aryThreads[j].Start();
// IF I USE THIS INSTEAD IT WORKS:
//aryFtpT[j].threadPutMethod();
}
public class ftpThread {
public FTPClient ftp = null;
public bool busy = false;
public string file = "";
public string fileAndPath = "";
public string remPath = "";
// code to open FTP connection here.
public void threadPutMethod(){
busy = true;
ftp.Put(fileAndPath,remPath + "\\" + file);
busy = false;
}
}

As long as I only use one of the connections its works like a charm. As soon as i put them in a thread i get a lot of weird error messages.

"Object referense not set to an object"
"No connection could be made because the target machine actively refused it"
"unable to write data to the transport connection"
+ others i don't remember now.

Any ideas as to what I'm doing wrong?

7 Answers

0 votes
by (162k points)
Does your thread code work for just one thread?

Hi, I'm trying to make an applikation that upploads 10 files at the same time to an FTP server.

0 votes
by (160 points)
Not quite sure if this is what you had in mind but i tried this:
if (aryFtpT[1].busy == false)
{
aryFtpT[1].putFile(fileName,fileAndPath,rmDir + pathName);
aryThreads[1] = new Thread(new ThreadStart(aryFtpT[1].threadPutMethod));
aryThreads[1].Start();
upload = true;
}
Meaning i only used one of the objects/connections and one of the threads. This still gave me the same error messages.
0 votes
by
Any reply on this post?
I also ran into the same problem when downloading with multiple threads.
The code works well with 1 thread, but when I use .Net ThreadPool class or even limited the max number of thread to 5 using Interlock, I still get the "No connection could be made because the target machine actively refused it"

Have anyone else successfully implement multi-threading
0 votes
by
I found the solution from other posts. The FTPClient is not thread-safe so I create one instance of FTPClient in each thread and it works great so far.

Thanks
0 votes
by
I CREATED AN ARRAY OF FTPCLIENT THREAD OBJECTS. WORKS OK FOR USING THE DIR TO GET A LIST OF FILES. UNFORTUNATELY I CAN'T GET THE "GET" TO WORK USING GET("D:\", FILENAME).

tHE SYSTEM.ERROR IS "ACCESS TO PATH D: IS DENIED".

ANYBODY HAVE IDEAS?
0 votes
by (51.6k points)
Use
get("D:\\"+fileName, fileName);


- Hans (EDT)
0 votes
by
thanks for the help.

As for those who are multi-threading, create a class that does the work. Include any public variables that you want to set from the main app.

When you spawn each class as a thread add both the thread and the class object to an array. The object reference lets you access the objects parameters later, as needed. The Thread Reference lets you control the thread, as needed from the main routine.

Dim arrFTP_GetThreads As New ArrayList
Dim arrFTPGetObjects As New ArrayList

When you create the object you can add a handler (i use the same one for each thread).

Dim objFTP_Get As New clsFTP_Get
AddHandler objFTP_Get.ThreadStatusUpdate, AddressOf sFTP_GetThreadEventHandler
{ set parameters of object here }
Dim myNewFTP_Thread As New Thread(AddressOf YourClass.YourRoutine)
arrFTP_GetThreads.Add(myNewFTP_GetThread) :
arrFTPGetObjects.Add(objFTP_Get)
myNewFTP_GetThread.Start()

Categories

...