PDA

View Full Version : سوال: انداختن تابع کپی فایل داخل Thread



forodo
جمعه 04 بهمن 1392, 17:56 عصر
سلام
من متوجه نمی شم چطوری باید کدای زیر رو داخل Thread قرار بدم که وقتی عملیات کپی داره انجام می شه بتونم با برنامه ام کار کنم و صبر نکنه که کپی تموم بشه و دسترسی به برنامه ام رو برگردونه.
من اینجوری نوشتم:

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(CopyFolderAndIcon));
t.Start();
MessageBox.Show("Test");
}

این هم اون تابعم هستش:

private void CopyFolderAndIcon()
{
// ساخت شی از کنترل فولدربروزردیالوگ
FolderBrowserDialog fbd = new FolderBrowserDialog();
// دادن توضیحات به کنترل فولدربروزردیالوگ
fbd.Description = "Copy Films Of Ali";
// اگر فولدربروزردیالوگ اوکی بود
if (fbd.ShowDialog() == DialogResult.OK)
{
// اگر فولدر کپی می شود
if (Directory.Exists(radGridView2.CurrentRow.Cells["FilePath"].Value.ToString()))
{
#region ساخت آیکون فولدر

FolderIcon myFolderIcon = new FolderIcon(fbd.SelectedPath + "\\" + Path.GetFileName(radGridView2.CurrentRow.Cells["FilePath"].Value.ToString()));
myFolderIcon.CreateFolderIcon(radGridView2.Current Row.Cells["FilePath"].Value.ToString() + @"\FolderMarker.ico");
myFolderIcon = null;

#endregion
#region کپی کردن فولدر با تمام زیرشاخه هایش

DirectoryInfo di = new DirectoryInfo(radGridView2.CurrentRow.Cells["FilePath"].Value.ToString());
DirectoryInfo di1 = new DirectoryInfo(fbd.SelectedPath + "\\" + Path.GetFileName(radGridView2.CurrentRow.Cells["FilePath"].Value.ToString()));
RunFileOrFolder.tek = radGridView2.CurrentRow.Cells["FilePath"].Value.ToString();
RunFileOrFolder.CopyDirectory(di, di1);

#endregion
// پیغام اتمام کپی
MessageBox.Show("finish");
}
// اگر فایل کپی می شود
else if (File.Exists(radGridView2.CurrentRow.Cells["FilePath"].Value.ToString()))
{
#region کپی فایل

FileInfo fi = new FileInfo(radGridView2.CurrentRow.Cells["FilePath"].Value.ToString());
if (Directory.Exists(fbd.SelectedPath))
{
fi.CopyTo(fbd.SelectedPath + "\\" + Path.GetFileName(radGridView2.CurrentRow.Cells["FilePath"].Value.ToString()));
MessageBox.Show("finish File");
}
else
{
MessageBox.Show("There is no selected path", "Path", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}

#endregion
}
}
}

samin_panahi
جمعه 04 بهمن 1392, 18:00 عصر
سلام
الان اینی که نوشتید کار می کنه یا break می کنه؟

fmehrvarzi
جمعه 04 بهمن 1392, 19:02 عصر
Queue files = new Queue();

static void Main(string[] args)
{
string path = @"";
FileSystemWatcher listener = new FileSystemWatcher(path);
Thread t = new Thread(new ThreadStart(ProcessFiles));
t.Start();
listener.Created += new FileSystemEventHandler(listener_Created);
listener.EnableRaisingEvents = true;

while (Console.ReadLine() != "exit") ;
}


public static void listener_Created(object sender, FileSystemEventArgs e)
{
files.Enqueue(e.FullPath);
}

void ProcessFiles()
{
while(true)
{
if(files.Count > 0)
{
String file = files.Dequeue();
while (!IsFileReady(file)) ;

File.Copy(file, @"D:\levani\FolderListenerTest\CopiedFilesFolder\" + file);
}
}
}

کافیه در رویداد listener نام فایلتان را به صف اضافه کنید
منبع: stackoverflow.com/questions/11029043/copy-multiple-files-in-a-thread

forodo
جمعه 04 بهمن 1392, 19:11 عصر
سلام
الان اینی که نوشتید کار می کنه یا break می کنه؟
عمل کپی انجام می شه ولی هیچ کاری نمی تونم با کنترلهای دیگه انجام بدم یه جوری برنامه هنگ می کنه ولی Not Responding نمی زنه ها.

forodo
جمعه 04 بهمن 1392, 19:14 عصر
Queue files = new Queue();

static void Main(string[] args)
{
string path = @"";
FileSystemWatcher listener = new FileSystemWatcher(path);
Thread t = new Thread(new ThreadStart(ProcessFiles));
t.Start();
listener.Created += new FileSystemEventHandler(listener_Created);
listener.EnableRaisingEvents = true;

while (Console.ReadLine() != "exit") ;
}


public static void listener_Created(object sender, FileSystemEventArgs e)
{
files.Enqueue(e.FullPath);
}

void ProcessFiles()
{
while(true)
{
if(files.Count > 0)
{
String file = files.Dequeue();
while (!IsFileReady(file)) ;

File.Copy(file, @"D:\levani\FolderListenerTest\CopiedFilesFolder\" + file);
}
}
}

کافیه در رویداد listener نام فایلتان را به صف اضافه کنید
منبع: stackoverflow.com/questions/11029043/copy-multiple-files-in-a-thread
می خوام یاد بگیرم چطوری و کجا باید اون ترد رو استفاده کنم.
می خوام توی کدای خودم باشه و کد اضافه نکنم.
در ضمن کد کپی که گذاشتم فولدر رو با تمام زیرشاخه هاش کپی می کنه نه فقط یک فایل رو.

forodo
شنبه 05 بهمن 1392, 14:10 عصر
نمی دونم اصلاً کجا باید sleep رو بذارم!!!:متعجب:

forodo
یک شنبه 06 بهمن 1392, 14:15 عصر
دوستان کسی عمل کپی رو با thread انجام نداده؟