【发布时间】:2011-07-21 22:43:37
【问题描述】:
我有一个带有用于拖放文件的按钮的 C# 应用程序。我可以从我的桌面上取出 6 个文件并将其放到按钮上并让它处理这 6 个文件。
但是,当我从 DragDrop 事件启动线程并将文件路径传递给从 DragDrop 事件中启动的新线程时,一旦线程接收到 FilePath 参数,文件路径就会不正确。
如果我通过将 6 个文本文件拖到我的按钮上来执行我的代码(在这个示例中我不得不从中删除很多代码),我将在我的控制台中看到以下内容:
++ 使用以下参数调用测试线程:false、TestButton、test.txt、c:\test.txt
++ 使用以下参数调用 testthread:false、TestButton、test2.txt、c:\test2.txt
++ 使用以下参数调用 testthread:false、TestButton、test3.txt、c:\test3.txt
++ 使用以下参数调用 testthread:false、TestButton、test4.txt、c:\test4.txt
++ 使用以下参数调用 testthread:false、TestButton、test5.txt、c:\test5.txt
++ 使用以下参数调用 testthread:false、TestButton、test6.txt、c:\test6.txt
以上输出正确
以下输出不正确,请注意 FilePath 与上述控制台输出中的 CleanFileName 不匹配。
++ testthread 线程 - CallingfromPendingUploads == false ButtonName == TestButton CleanFileName == test.txt FilePath = c:\test2.txt
++ testthread 线程 - CallingfromPendingUploads == false ButtonName == TestButton CleanFileName == test1.txt FilePath = c:\test3.txt
++ testthread 线程 - CallingfromPendingUploads == false ButtonName == TestButton CleanFileName == test3.txt FilePath = c:\test4.txt
++ testthread 线程 - CallingfromPendingUploads == false ButtonName == TestButton CleanFileName == test4.txt FilePath = c:\test5.txt
++ testthread 线程 - CallingfromPendingUploads == false ButtonName == TestButton CleanFileName == test5.txt FilePath = c:\test5.txt
++ testthread 线程 - CallingfromPendingUploads == false ButtonName == TestButton CleanFileName == test6.txt FilePath = c:\test5.txt
如您所见,来自线程的 FilePath 与在线程启动之前传递给线程的 FilePath 不匹配。与传递给线程的文件名相比,所有文件路径都关闭。并且有些文件路径是重复的,例如 text5.txt。
我已经为此苦苦挣扎了好几个小时。谁能告诉我我做错了什么?
private void btnClick_DragDrop(object sender, DragEventArgs e)
{
string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
string ButtonName = "TestButton"
string[] files = new string[10];
files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
FileInfo fileInfo = new FileInfo(file);
Console.WriteLine("++ Filename: " + fileInfo.Name + " Date of file: " + fileInfo.CreationTime + " Type of file: " + fileInfo.Extension + " Size of file: " + fileInfo.Length.ToString());
string CleanFileName = System.Web.HttpUtility.UrlEncode(fileInfo.Name.ToString());
//Start thread
try
{
Console.WriteLine("++ Calling testthread with these params: false, " + ButtonName + "," + CleanFileName + "," + file);
new Thread(() => testthread(false, ButtonName, CleanFileName, file)).Start();
Console.WriteLine("++ testthead thread started @ " + DateTime.Now);
}
catch (Exception ipwse)
{
logger.Debug(ipwse.Message + " " + ipwse.StackTrace);
}
}
}
public void testthread(bool CalledfromPendingUploads, string ButtonName, string CleanFileName, string FilePath)
{
Console.WriteLine("++ testthread Thread - CallingfromPendingUploads == " + CalledfromPendingUploads.ToString() + " ButtonName == " + ButtonName + " CleanFileName == " + CleanFileName + " FilePath = " + FilePath);
}
【问题讨论】:
-
你应该使用 ThreadPool (虽然它没有帮助)
-
关闭循环变量的另一种情况:blogs.msdn.com/b/ericlippert/archive/2009/11/12/…
标签: c# multithreading drag-and-drop