PDA

View Full Version : فراخوانی یک تابع به صورت چند نخی



ho3ein.3ven
دوشنبه 03 تیر 1392, 18:38 عصر
سلام
من نیاز دارم که یک تابع رو به صورت نامحدود همزمان اجرا کنم.در زمان اجرای تابع برنامه هنک می کنه و نمیشه که همزمان 2 بار تابع رو فراخوانی کرد . به همین دلیل تابع رو به صورت چند نخی فراخوانی می کنم اما وقتی تابع برای بار اول اجرا میشه و در حال انجام عملیات هست و تابع برای بار دوم فراخوانی میشه برنامه ارور میده.
کسی از دوستان راه حلی برای این مشکل داره ؟

tooraj_azizi_1035
دوشنبه 03 تیر 1392, 21:07 عصر
چه خطایی میده سعی کن کدهای مرتبط رو هم بذاری.

ho3ein.3ven
دوشنبه 03 تیر 1392, 21:30 عصر
این تابع رو فراخوانی می کنم :
private void Download(ref string filePath,ref string fileName)
{
FtpWebRequest reqFTP;
try
{
//filePath = <<The full path where the file is to be created.>>,
//fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message);
}
}


تابع بالا برای دانلود یک فایل از اف تی پی هست.

tooraj_azizi_1035
دوشنبه 03 تیر 1392, 21:57 عصر
شما قصد داری چندین فایل رو همزمان دانلود کنی:

private Queue<string> _downloadUrls = new Queue<string>();

private void downloadFile(IEnumerable<string> urls)
{
foreach (var url in urls)
{
_downloadUrls.Enqueue(url);
}

// Starts the download
btnGetDownload.Text = "Downloading...";
btnGetDownload.Enabled = false;
progressBar1.Visible = true;
lblFileName.Visible = true;

DownloadFile();
}

private void DownloadFile()
{
if (_downloadUrls.Any())
{
WebClient client = new WebClient();
client.DownloadProgressChanged += client_DownloadProgressChanged;
client.DownloadFileCompleted += client_DownloadFileCompleted;

var url = _downloadUrls.Dequeue();
string FileName = url.Substring(url.LastIndexOf("/") + 1,
(url.Length - url.LastIndexOf("/") - 1));

client.DownloadFileAsync(new Uri(url), "C:\\Test4\\" + FileName);
lblFileName.Text = url;
return;
}

// End of the download
btnGetDownload.Text = "Download Complete";
}

private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
// handle error scenario
throw e.Error;
}
if (e.Cancelled)
{
// handle cancelled scenario
}
DownloadFile();
}

void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;
progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
}

ho3ein.3ven
دوشنبه 03 تیر 1392, 22:54 عصر
شما قصد داری چندین فایل رو همزمان دانلود کنی:

private Queue<string> _downloadUrls = new Queue<string>();

private void downloadFile(IEnumerable<string> urls)
{
foreach (var url in urls)
{
_downloadUrls.Enqueue(url);
}

// Starts the download
btnGetDownload.Text = "Downloading...";
btnGetDownload.Enabled = false;
progressBar1.Visible = true;
lblFileName.Visible = true;

DownloadFile();
}

private void DownloadFile()
{
if (_downloadUrls.Any())
{
WebClient client = new WebClient();
client.DownloadProgressChanged += client_DownloadProgressChanged;
client.DownloadFileCompleted += client_DownloadFileCompleted;

var url = _downloadUrls.Dequeue();
string FileName = url.Substring(url.LastIndexOf("/") + 1,
(url.Length - url.LastIndexOf("/") - 1));

client.DownloadFileAsync(new Uri(url), "C:\\Test4\\" + FileName);
lblFileName.Text = url;
return;
}

// End of the download
btnGetDownload.Text = "Download Complete";
}

private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
// handle error scenario
throw e.Error;
}
if (e.Cancelled)
{
// handle cancelled scenario
}
DownloadFile();
}

void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;
progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
}


آقا مرسی از توجهت . ولی خب از این سورس شما سر در نیاوردم . اگر بشه که همین تابع رو جوری فراخوانی کرد که تداخل ایجاد نشه بهتره.

ho3ein.3ven
سه شنبه 04 تیر 1392, 12:38 عصر
دوستان کسی نظری نداره ؟