473,671 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading for FTP in C#

Hi,

I am trying to write some threading code to my application. The reason
I've been tempted to do this is because, I am doing some FTP uploads,
and sometimes during the put method, the application just hangs ("Not
Responding") instead of the regular timeout and return. I am using a
library to perform FTP stuff and so there may be a bug in there. In
any case, what I am concerned with is my application executing with
graceful execution and termination. I have plenty of try and catch
clauses inside the methods, and I even know which line is causing the
problem (the put line), I even tried debugging it line by line, but
sometimes it just hangs up. This is happening with only this one
particular FTP server being run by a client.

So, I am thinking if I should create a thread that calls the
doFTPUpload method. If the method doesn't return within, say, 60
seconds, I forcefully terminate the call and show an error to the user
via GUI. The main idea is to keep the application from going into "Not
Responding" state by running the error prone code in a different
thread.

Now I have never done threading before but have read about it. So I am
hoping someone can push me in the right direction.

I have a class called Transmission that has doFTPUpload method. It
takes in three arguments, a client object (that contains information
about the FTP such as server name, user id, password, etc.),a logs
object which is used to enter information into log files, and a
textbox txtSummary which shows information to user as upload
progresses.

I make a call from my main method as follows:

try
{
t.doFTPUpload(c lient,logs,txtS ummary);
}
catch (Exception eUpload)
{
logs.addToLog(" Upload Failed!");
logs.addToLog(e Upload.ToString ());
}

Now how can I modify this code to start a new worker thread and make
the call to doFTPUpload inside this thread. Also how can I put a
timeout on it so that if the thread doesn't finish up in say 60
seconds, I manually kill the thread and go on with my execution inside
the main method?

Thanks.

Asad
Nov 17 '05 #1
3 4502
I had fun writing this. It should keep you busy for a while.
FYI, I didn't try to build it, so check for type-o's.

Minimum "using" statments required:

using System;
using System.Windows. Forms;

Implementation (without class declaration):

private delegate void ShowStatusInvok er(string Text);
private delegate void FtpUploadInvoke r(object client, object logs, TextBox txtSummary);
private FtpUploadInvoke r ftpUploadInvoke r; // Instance variable of delegate

public void DoUpload()
{
try
{
// Create a pointer to the doFTPUpload method
ftpUploadInvoke r = new FtpUploadInvoke r(t.doFTPUpload );

// Invoke the pointer asyncronously on a thread-pool thread.
// If the FTP class documentation states that the doFTPUpload method
// uses thread-pooling, then I recommend spawning a custom thread (not pooled)
// instead of using this implementation to prevent deadlocks.
// See System.Thread.T hread class.
IAsyncResult result = ftpUploadInvoke r.BeginInvoke(c lient, logs, txtSummary,
new AsyncCallback(U ploadComplete), null);

// Wait a maximum of 60 seconds for the invocation to complete
if (!result.IsComp leted && !result.AsyncWa itHandle.WaitOn e(60000, false))
// Operation has not completed within the maximum time alotted
{
logs.addToLog(" Upload Failed!");
logs.addToLog(" Operation has not completed within the maximum time alotted");
}
}
catch (FtpUploadExcep tion eUpload)
{
logs.addToLog(" Upload Failed!");
logs.addToLog(e Upload.ToString ());
}
}

/// <summary>
/// Callback for doFTPUpload on a thread-pool thread. Do not call any
/// Begin* methods from within the context of this method to avoid thread-pool deadlocks.
/// </summary>
public void UploadComplete( IAsyncResult result)
{
if (ftpUploadInvok er != null)
// This method can be used to obtain the return value
// but it's not necessary since FtpUploadInvoke r returns void
ftpUploadInvoke r.EndInvoke(res ult);

// It is important to note that GUI calls must occur on the main window thread
// which is the thread that the message loop is executing on.
// Check Form.InvokeRequ ired and use the Invoke method to properly make GUI calls
// from this thread.
ShowStatus("Upl oad Complete");
}

/// <summary>
/// Displays the specified <paramref name="Text" /> using a thread-safe invocation.
/// </summary>
public void ShowStatus(stri ng Text)
{
lock (this)
{
if (this.InvokeReq uired) // Form.InvokeRequ ired
// It is assumed that this method will be defined within a derived Form class
this.Invoke(new ShowStatusInvok er(ShowStatusCo re), new object[] { Text });
else
ShowStatusCore( Text);
}
}

/// <summary>
/// Displays the specified <paramref name="Text" />. This method is not thread-safe.
/// </summary>
private void ShowStatusCore( string Text)
{
txtStatus.Text = Text;
}

--
Dave Sexton
dave@www..jwaon line..com
-----------------------------------------------------------------------
"Asad" <as*******@gmai l.com> wrote in message news:ae******** *************** ***@posting.goo gle.com...
Hi,

I am trying to write some threading code to my application. The reason
I've been tempted to do this is because, I am doing some FTP uploads,
and sometimes during the put method, the application just hangs ("Not
Responding") instead of the regular timeout and return. I am using a
library to perform FTP stuff and so there may be a bug in there. In
any case, what I am concerned with is my application executing with
graceful execution and termination. I have plenty of try and catch
clauses inside the methods, and I even know which line is causing the
problem (the put line), I even tried debugging it line by line, but
sometimes it just hangs up. This is happening with only this one
particular FTP server being run by a client.

So, I am thinking if I should create a thread that calls the
doFTPUpload method. If the method doesn't return within, say, 60
seconds, I forcefully terminate the call and show an error to the user
via GUI. The main idea is to keep the application from going into "Not
Responding" state by running the error prone code in a different
thread.

Now I have never done threading before but have read about it. So I am
hoping someone can push me in the right direction.

I have a class called Transmission that has doFTPUpload method. It
takes in three arguments, a client object (that contains information
about the FTP such as server name, user id, password, etc.),a logs
object which is used to enter information into log files, and a
textbox txtSummary which shows information to user as upload
progresses.

I make a call from my main method as follows:

try
{
t.doFTPUpload(c lient,logs,txtS ummary);
}
catch (Exception eUpload)
{
logs.addToLog(" Upload Failed!");
logs.addToLog(e Upload.ToString ());
}

Now how can I modify this code to start a new worker thread and make
the call to doFTPUpload inside this thread. Also how can I put a
timeout on it so that if the thread doesn't finish up in say 60
seconds, I manually kill the thread and go on with my execution inside
the main method?

Thanks.

Asad

Nov 17 '05 #2
Hi,

Ok that looks pretty good, but could you please explain your overall
program flow? I have never used delegates before and so I'm a little
lost despite the excellent comments. Appreciated.

Asad

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
No problem. LOL, here's a book on it:

FtpUploadInvoke r is a delegate that has the same signature as the doFTPUpload method.
ftpUploadInvoke r is an instance variable that will be assigned a reference to an instance of the doFTPUpload method.
(Mind the case)

DoUpload() is the "entry point" for the upload procedure:

First, it assigns the ftpUploadInvoke r to an instance of the t class doFTPUpload method.
ftpUploadInvoke r = new FtpUploadInvoke r(t.doFTPUpload );
ftpUploadInvoke r.BeginInvoke invokes the doFTPUpload method, asyncronously. This will call the doFTPUpload method and immediately
return to the calling code and continue execution.
The AsyncCallback is another delegate defined in System which allows the BeginInvoke method to callback to an application-defined
method when it is completed. Here, I'm telling it to callback to an instance of the UploadComplete method I defined later on: IAsyncResult result = ftpUploadInvoke r.BeginInvoke(c lient, logs, txtSummary,
new AsyncCallback(U ploadComplete), null);
Check out the intellisense on the BeginInvoke method... it dynamically adjusts for your FtpUploadInvoke r declaration.

Note: <null> is arbitrary data of Type, "Object" that I'm not using in this code. The IAsyncResult "result" variable has a
property that will let you access the data if you need to use it.

Next, the application pauses (although it doesn't have to) in order to wait for the asyncronous operation to complete. As I stated
above, when it's completed the "UploadComplete " method will automattically be invoked on a thread-pool thread. I set the timeout to
60000 milliseconds since you asked for a 60 second timeout: if (!result.IsComp leted && !result.AsyncWa itHandle.WaitOn e(60000, false))
WaitHandle.Wait One returns false if the method call times-out before completing, so I used your log calls to log the error.

Note: <false> specifies that we do not want to exit any system-acquired locks before proceeding. Specifying true is useful for when
you are in a locked context, yet the asyncronous call will try to aquire the same lock (causing a dead-lock). You can acquire a
lock using the C# lock statement which I illustrated for you below. Locking synchronizes multiple threads by locking access to an
object. Only one thread may acquire a lock on an object at any given time. If the lock cannot be acquired, the thread will block
until the lock is acquired.

UploadComplete( ) is our asyncronous callback for when the doFTPUpload method completes:

EndInvoke allows us to retrieve any return value that the method may return, although this implementation declares the delegate as
void since I assumed that t.doFTPUpload returns void. (You weren't using any return value in your sample code). Because it's
returning void, I place the call to EndInvoke as an example, but without actually using an return value. Let's say, for example,
that doFTPUpload returns an Integer. You can modify this line to consume the return value as follows: int doFTPUploadRetu rnValue = ftpUploadInvoke r.EndInvoke(res ult);
ShowStatus() was intended to show you that UploadComplete is being called in a seperate thread than your application's main thread.
If you want to display something to the user upon an error (as you stated in your first post) or completion of the doFTPUpload
invocation while in the context of a different thread, you'll need to check if any of the Form controls require invocation. This is
because the form controls use the message loop to do work, and so methods on the controls may require an invocation on the
application's main thread to function properly. The invocation can be performed easily using the Form.Invoke() method which always
invokes the specified delegate on the thread that the message loop is running on. This is the call to the ShowStatus function,
which ensures that the control's are manipulated on the appropriate thread: ShowStatus("Upl oad Complete");
ShowStatus():

Here's that C# "lock" statement I mentioned above. Locking "this" object, which should be a Form instance, will ensure that any
threads calling this method must wait for a previous invocation to first complete. You can lock any object that is not a ValueType,
but for simplicity I've used "this".
InvokeRequired returns true if the calling thread is not the thread that the message loop is running on, and if so I use
[Form].Invoke with a custom delegate to call the core implementation of the ShowStatus method. If InvokeRequired returns false, I
just call ShowStatusCore immediately since the call is on the thread that the message loop is running on: lock (this)
{
if (this.InvokeReq uired) // Form.InvokeRequ ired
// It is assumed that this method will be defined within a derived Form class
this.Invoke(new ShowStatusInvok er(ShowStatusCo re), new object[] { Text });
else
ShowStatusCore( Text);
}
ShowStatusCore( ):

Place any code that you want here. I still recommend not using any Begin* methods in this scope because this method is invoked
indirectly from a method called on a thread-pool thread (UploadComplete ).

Begin* methods:

It's stated that any system-defined functions that are labeled Begin* and are intended for asynchronous calls, of otherwise
synchronous methods, will be invoked using a thread-pool thread. The System.Threadin g.ThreadPool defines the maximum number of
running worker threads in the pool to be 25 * [Number of Processors]. This usually means 25. (There is also IOCompletionPor t
threads, but I'm not going to discuss that here.) So if you have too many nested Begin* calls there is a chance of a dead-lock.
This means that one thread will be dependant on another thread being invoked, yet the latter thread will block until the calling
thread is freed. Dead-lock.

So, overall:

1. Asynchronous invocation of your upload method
2. Waits for a specified timeout before failing, or completes immediately
3. Runs a callback method if it completes before the timeout period elapses
4. Uses synchronization to alert the user of a change in application status that may occur on concurrent threads
Hope it helps :)

--
Dave Sexton
dave@www..jwaon line..com
-----------------------------------------------------------------------
"Asad Khan" <as*******@gmai l.com> wrote in message news:eK******** ******@TK2MSFTN GP15.phx.gbl... Hi,

Ok that looks pretty good, but could you please explain your overall
program flow? I have never used delegates before and so I'm a little
lost despite the excellent comments. Appreciated.

Asad

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
6725
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the language's 30+ year evolution. What to you think python largest compromises are? The three that come to my mind are significant whitespace, dynamic typing, and that it is interpreted - not compiled. These three put python under fire and cause...
2
2973
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def get_all_files(path): """return all files of folder path, scan with subfolders
77
5263
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go through the whole thing!) to check for accuracy, effectiveness of examples, etc. Feel free to mail...
6
555
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service and the final process(3) gets called. what am I doing wrong with the threading? by the way Directory.GetFiles(IncomingXMLPath1).Length is some global outcome from process 1. Thanks 1)
2
2242
by: Vjay77 | last post by:
In this code: Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not (Me.downloadUrlTextBox.Text = "") Then Me.outputGroupBox.Enabled = True Me.bytesDownloadedTextBox.Text = "" Me.totalBytesTextBox.Text = ""
11
5015
by: Paul Sijben | last post by:
I am stumped by the following problem. I have a large multi-threaded server accepting communications on one UDP port (chosen for its supposed speed). I have been profiling the code and found that the UDP communication is my biggest drain on performance! Communication where the client and the server are on the same machine still takes 300ms or sometimes much more per packet on an Athlon64 3000+ running Linux (Fedora Core 5 x64). I must...
17
6410
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. Note, the Windows task manager shows 2 CPUs on the Performance tab with hyper-threading is turned on. Both Python 2.3.5 and 2.4.3 (downloaded from python.org) have this problem. The operating system is MS Windows XP Professional.
0
1586
by: kingcrowbar.list | last post by:
Hello Everyone I have been playing a little with pyGTK and threading to come up with simple alert dialog which plays a sound in the background. The need for threading came when in the first version i made, the gui would freeze after clicking the close button until pygame finished playing the sound. In Windows it was acceptable because it could be ignored easily, but in
7
2368
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has finished. I am trying to follow this example : http://www.codeproject.com/cs/miscctrl/progressdialog.asp But although the messages still get moved, the progress window never does anything. Here is my code in full, if anybody who knows...
126
6688
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard: http://www.cse.wustl.edu/~schmidt/ACE.html the same way that the STL (and subsequently BOOST) have been subsumed? Since it already runs on zillions of platforms, they have obviously worked most of the kinks out of the generalized threading and processes idea (along with many...
0
8478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8397
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8919
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8599
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6230
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2052
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.