Re: multi-threaded app/ using lock



On Feb 8, 12:30 pm, "Gina_Marano" <ginals...@xxxxxxxxx> wrote:
Hey All,

I am using multiple child threads per main thread to download files.
It sometimes appears as if the same file is being downloaded twice. I
am using "lock". Am I using it correctly? Any blantant threading
errors here?

Any opinions would be greatly appreciated.

namespace MainThreadManagement
{
public delegate string ThreadFinishedCallback();

public class WorkerThread
{
protected string ftpSite;

protected WorkerThread(string aFtpSite)
{
ftpSite = aFtpSite;
}
}

public class MainThread : WorkerThread
{
List<string> arrFileNames = new List<string>();

protected Object GetNextFileLockObj = new Object(); //locking
object
protected Object NotifyThreadFinishedLockObj = new Object(); //
locking object

public MainThread(string aFTPSite): base(aFTPSite)
{
}

public void ProcessFilesToDownload()
{
System.Threading.Thread aThread = null;
ChildThread aChildThread;
ArrayList arrChildThreads = new ArrayList();
string sFileName;

//get a list of the files to download
arrFileNames = GetListOfFiles();

//assume maximum of 10 child threads
for (int i = 0; i < 10; i++)
{
sFileName = GetNextFileName();

if (sFileName == "") //no more
break;

aChildThread = new ChildThread(
new ThreadFinishedCallback(NotifyThreadFinished),
sFileName);

aThread = new System.Threading.Thread(
new ThreadStart(aChildThread.DownloadFile));

arrChildThreads.Add(aThread);
}

//lets get all threads ready then start them.
for (int j = 0; j < arrChildThreads.Count; j++)
{
aThread = (System.Threading.Thread)arrChildThreads[j];
aThread.Start();
}

for (int j = 0; j < arrChildThreads.Count; j++)
{
aThread = (System.Threading.Thread)arrChildThreads[j];
aThread.Join();
}
}

private string GetNextFileName()
{
string sFileName;
lock (GetNextFileLockObj)
{
if (arrFileNames.Count > 0)
{
sFileName = arrFileNames[0];
arrFileNames.RemoveAt(0);
}
return sFileName;
}
}

//called from child threads using callback
private ChilKatFtp.FTPListing NotifyThreadFinished(IsSuccess
aSuccess, string aParent)
{
lock (NotifyThreadFinishedLockObj)
{
string sFileName = "";
sFileName = GetNextFileName();
return sFileName;
}
}
}

}

namespace ChildThreadManagement
{
public class ChildThread : WorkerThread
{
ThreadFinishedCallback NotifyThreadFinished;
string aFileName;

public ChildThread(ThreadFinishedCallback aNotifyThreadFinished,
string aFileName, string aFTPSite)
: base(aFTPSite)
{
NotifyThreadFinished = aNotifyThreadFinished;
sFileName = aFileName;
}

public void DownloadFile()
{
Ftp FtpClient = null;
bool bSuccess;

try {
do {
bSuccess = getFtpConnection(ftpSite, ref FtpClient);
if (bSuccess)
bSuccess = DoDownloadFile(FtpClient, sFileName);

if (bSuccess)
lImageAttribProp = NotifyThreadFinished(lIsSuccess,
lImageAttribProp.sOrderAttribID);
else
sFileName = ""; //causes loop to end
} while (sFileName != ""); }
finally {
if (FtpClient != null)
{
if (FtpClient.IsConnected))
FtpClient.Disconnect();
FtpClient.Dispose();
}
}
}

private bool DoDownloadFile(Ftp FtpClient, string aFileName)
{
//this part works, for simplity of post removed code
return true;
}

private bool getFtpConnection(string aFTPSite, ref Ftp aFtpClient)
{
//this part works, for simplity of post removed code
return true;
}
}



}- Hide quoted text -

- Show quoted text -

Hi,

The DownloadFile method will loop until there is an error downloading
the file. I'm surprised the problem isn't more wide spread. I'm not
seeing the need for two different lock objects. The code is difficult
to follow. And I agree that it can't possibly be what you really
have.

Brian

.



Relevant Pages

  • multi-threaded app/ using lock
    ... I am using multiple child threads per main thread to download files. ... public delegate string ThreadFinishedCallback; ... sFileName = GetNextFileName; ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Internet Table Transfer
    ... URLDownloadToFile is an api function. ... ActiveX component. ... callbacks on the progress of the download. ... Pointer to a string value containing the URL to be downloaded. ...
    (microsoft.public.excel.programming)
  • Re: How to download a file from the internet using inet control
    ... 'Download an URL to a local file without caching. ... Public Function DownloadURL2FileNoCache(ByRef sSourceUrl As String, ... Dim BufferAs Byte ... Dim lNumberOfBytesRead As Long ...
    (microsoft.public.vb.general.discussion)
  • Re: Internet download APIs only get first part of file, rest is blank
    ... filling up the rest of the string. ... Code for the download function follows ... ... Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As ...
    (microsoft.public.vb.general.discussion)
  • Question for Randy Birch About Download File
    ... I am using the following code from Randy's site to download some extra files ... DownloadTempRegKey As String 'temporary download destination folder ... Private Declare Function RegQueryValueEx Lib "advapi32.dll" _ ... Dim dldata As FileRegistryDownloadData ...
    (microsoft.public.vb.general.discussion)