473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Threading.....

I am writing a FTP backup service for my servers. Right now, it works
well, but it is single threaded and takes a couple of hours to
complete a backup of the websites directory, even to a server on the
same network. I want to spin off 8 threads to handle the uploading.
When one files is finished uploading, a thread becomes available and a
new one is opened.

So, say that I have the following code..... This is just Pseudo
Code...

For x As Integer = 0 To UBound(arrayOfFiles)

'-- Old Code Here Was Something Like This.....
'-- ftpConnection.UploadFile(arrayOfFiles(x), strRemotePath)

'-- What I want to do here is just continue with this single thread
and just add all these to a Queue....
'-- I will then have a System.Timers.Timer look for items in the
global queue and upload them..

'-- So, I wrote my own queue, inheriting from a something in
System.Collections and my new code will be like this..

Queue.Add(arrayOfFiles(x), strRemotePath)

Next

The question is... What should my code look like in the Timer? I think
I might need to use some Events and Delegates, maybe a custom event
like Queue.Item(x).UploadFile ???

I just need a little sample on multi-threading for this type of
purpose and I am good. All the samples that I have found thus far are
either junk code, or just didn't fit into what I was trying to
accomplish.

Any help is appreciated. Thanks.

Jul 17 '07 #1
4 1492

<Ta*****************@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
>I am writing a FTP backup service for my servers. Right now, it works
well, but it is single threaded and takes a couple of hours to
complete a backup of the websites directory, even to a server on the
same network. I want to spin off 8 threads to handle the uploading.
When one files is finished uploading, a thread becomes available and a
new one is opened.

So, say that I have the following code..... This is just Pseudo
Code...

For x As Integer = 0 To UBound(arrayOfFiles)

'-- Old Code Here Was Something Like This.....
'-- ftpConnection.UploadFile(arrayOfFiles(x), strRemotePath)

'-- What I want to do here is just continue with this single thread
and just add all these to a Queue....
'-- I will then have a System.Timers.Timer look for items in the
global queue and upload them..
Why not use the Thread.Sleep(waittime) in a while loop?
>
'-- So, I wrote my own queue, inheriting from a something in
System.Collections and my new code will be like this..

Queue.Add(arrayOfFiles(x), strRemotePath)

Next

The question is... What should my code look like in the Timer? I think
I might need to use some Events and Delegates, maybe a custom event
like Queue.Item(x).UploadFile ???
You can user a Timer with Timer.Start/Timer.Stop or Thread.Sleep

You would be using the Timer.AddressOf(TimerElapsed_method) or
Thread.AddressOf(StartThread_method) the delegate method for each
>
I just need a little sample on multi-threading for this type of
purpose and I am good. All the samples that I have found thus far are
either junk code, or just didn't fit into what I was trying to
accomplish.
You have to cut your own path, as you might not find an example with exactly
what you're looking for. You should be able to find some examples that use
System.Timers and System.Threading namespaces out there on Google.

What's the big deal anyway? The Timer_Elapsed or the Thread.Sleep wait time
has elapsed. You run the methods in the delegates, they work with the queue
until complete, and they go back to sleep until the time has elapsed again.

I like using Thread.Sleep, because you better awareness of the a Thread
Exception in a While True loop using Thread.Sleep, like Thread exceptions.

You also might want to look at FileSystemWatch, look it up.

Jul 17 '07 #2
Ta*****************@gmail.com wrote:
I am writing a FTP backup service for my servers. Right now, it works
well, but it is single threaded and takes a couple of hours to
complete a backup of the websites directory, even to a server on the
same network. I want to spin off 8 threads to handle the uploading.
Having 8 threads working their way through the /same/ list of files
could very well be /slower/ than having a single Thread on its own
because of the contention caused by having lots of things accessing the
same list of things to do.

Threads work /much/ better if you can give them a [simple] job to do,
pat them on the head and forget all about them; just let them get on
with it.

Now, if you could have, say, one Thread per /Server/ you might get some
improvement, because each Server's list of files will be self-contained
within the Thread so it doesn't need to "talk" to anything else.

Anyway ...
'-- What I want to do here is just continue with this single thread
and just add all these to a Queue....
'-- I will then have a System.Timers.Timer look for items in the
global queue and upload them..

'-- So, I wrote my own queue, inheriting from a something in
System.Collections and my new code will be like this..

Queue.Add(arrayOfFiles(x), strRemotePath)
OK; A Queue is a good idea but I don't think you'd want a Timer in the
Threads. Timers introduce delays and you want these things processed
just as fast as possible.

You'll need to synchronise access to the Queue, have each Thread pull an
item off the Queue, process it and loop round for another one. If the
Thread runs out of things to do, it should quietly die off.

HTH,
Phill W.
Jul 17 '07 #3
<Ta*****************@gmail.comwrote:
>I am writing a FTP backup service for my servers. Right now, it works
well, but it is single threaded and takes a couple of hours to
complete a backup of the websites directory, even to a server on the
same network. I want to spin off 8 threads to handle the uploading.
When one files is finished uploading, a thread becomes available and a
new one is opened.
Sounds like a decent plan. I could make a case for using Async I/O instead,
but in this case, it would probably complicate the problem for very little
benefit.

Create a class that some number of threads in it, and has a "PostWorkItem"
method, where the workItem is a directory.

When a thread finishes, it moves on to the next available work item. The
code below I just wrote (I haven't run it, or even tried to compile it!),
but it should be enough to get you started. You'll need to improve the
shutdown logic and other related stuff, and add in some status / progress
updates, a Count Property, a Remining Items property, and a bunch of other
similar stuff. Don't forget to do basic locking around the Queue when you
access it!
Imports System.Threading

Public Class Uploader
Private _syncroot As New Object
Private _shutdownEvent As New ManualResetEvent(False)
Private _workItems As New Queue(Of String)
Private _workerThreads As New List(Of Thread)

Public Sub Uploader()
For i As Integer = 0 To 8
Dim newThread As New Thread(AddressOf DoWork)
newThread.Name = "Worker Thread " & i.ToString()
newThread.Start()
_workerThreads.Add(newThread)
Next
End Sub

Public Sub PostWorkItem(ByVal directoryToUpload As String)
SyncLock _syncroot
_workItems.Enqueue(directoryToUpload)
End SyncLock
End Sub

Public Sub Shutdown()
_shutdownEvent.Set()
ForEach Thread t In _workerThreads
t.Join()
Next
End Sub

Private Sub DoWork()
While Not _shutdownEvent.WaitOne(1000, False)
Dim directoryToUpload As String = String.Empty
SyncLock _syncroot
If _workItems.Count 0 Then
directoryToUpload = _workItems.Dequeue()
Else
directoryToUpload = Nothing
End If
End SyncLock

If directoryToUpload <Nothing Then
'*** Do long running FTP Process here
End If
End While
End Sub

End Class
--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins
Jul 18 '07 #4
<Ta*****************@gmail.comwrote:
>I am writing a FTP backup service for my servers. Right now, it works
well, but it is single threaded and takes a couple of hours to
complete a backup of the websites directory, even to a server on the
same network.
'-- I will then have a System.Timers.Timer look for items in the
global queue and upload them..
You don't really want to use a Timer for this. When the timer gives you a
callback, that callback is running on a ThreadPool thread. There are a
limited number of these threads (varying with the number of processors in
your system, and the version of the .Net framework you're running), and if
you tie all these threads up (aka: Induce Threadpool Starvation), your
application won't work right.

Threadpool threads are ideal for handling small, compute-bound tasks.
They're a poor choice for performing long-running I/O bound tasks.

Given that you're going to be "owning" this thread for a few hours, you're
not hitting target use case for the ThreadPool thread. Your remining choices
are:
1 - Async I/O
2 - Your own threads

In this case, owning and managing your own threads (as I did in the sample I
just posted) is probably the easiest way to go. Async I/O would probably get
you better performance, but it generally takes a while for people to wrap
their heads around.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins
Jul 18 '07 #5

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

Similar topics

65
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...
2
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...
77
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...
6
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...
2
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...
11
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...
17
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. ...
0
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...
7
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...
126
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:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.