- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个带有用于拖放文件的按钮的 C# 应用程序。我可以从我的桌面上获取 6 个文件并将其放到按钮上并让它处理这 6 个文件。
但是,当我从 DragDrop 事件启动线程并将文件路径传递到从 DragDrop 事件内启动的新线程时,一旦线程接收到 FilePath 参数,文件路径就不正确。
如果我通过将 6 个文本文件拖到我的按钮上来执行我的代码(对于这个例子我不得不从中删除很多代码),我将在我的控制台中看到以下内容:
++ 使用这些参数调用测试线程:false, TestButton,test.txt,c:\test.txt
++ 使用这些参数调用测试线程:false, TestButton,test2.txt,c:\test2.txt
++ 使用这些参数调用测试线程:false, TestButton,test3.txt,c:\test3.txt
++ 使用这些参数调用测试线程:false, TestButton,test4.txt,c:\test4.txt
++ 使用这些参数调用测试线程:false, TestButton,test5.txt,c:\test5.txt
++ 使用以下参数调用测试线程: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
如您所见,线程中的文件路径与线程启动前传递给线程的文件路径不匹配。与传递给线程的文件名相比,所有 FilePaths 都是关闭的。并且一些文件路径是重复的,例如 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);
}
最佳答案
您的所有线程都共享同一个 file
变量。
如果其中一个线程仅在 UI 线程开始下一次迭代后才开始运行,它将使用 file
变量的下一个值。
你需要在循环内部声明一个单独的变量,这样每个线程都会得到自己的变量。
例如:
foreach (string dontUse in files)
{
string file = dontUse;
...
}
由于 file
变量现在在循环范围内,每次迭代都会获得一个单独的变量。
关于c# - 从 DragDrop 事件中将数据传递到线程的线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6783710/
在Wpf中拖动期间,如何更改鼠标光标(或使用装饰器)以指示droptarget将不接受拖动的项目? 我曾尝试在e.Effects = DragDropEffects.None事件期间设置DragEnt
我真的很难在 Silverlight 4 中获得一个简单的拖放示例。 这是我所拥有的: XAML 代码隐藏 public partial c
我找不到将文件放入富文本框后会发生的事件。 *当您将文件放入 Rtb 时,如果您下载了从 richtextbox 派生的用户控件,则图标的图像将复制到控件上,此控件有一些方法可以操作 RTF,其中包括
System.InvalidOperationException: DragDrop registration did not succeed. ---> System.Threading.Threa
我们在 Visual Studio 解决方案中有一些安装项目(其他项目类型 -> 安装和部署 -> 安装项目)。该项目有另一个库类型项目,其安装类名为 InstallationCore,如项目输出。在
在 WPF 中,我想使用右键单击来启动拖放。类似于 Windows 资源管理器,右键单击,稍微移动,你会得到装饰器来指示操作中的拖放。不要移动,松开按钮时会出现上下文菜单。 我已经启动了这一切,但是当
情况: 我有一个使用 angular-dragdrop 的应用程序在三个单独的列表之间移动项目。 拖放工作正常。但我还需要对同一列表中的项目重新排序。 我在文档中看到也可以在 onDrop 上检索索引
我正在尝试编写一些代码以允许我的应用程序的用户拖放 DataGridView 中的行以重新排序。问题是,被拖动的行在被拖放时消失了——所以拖放的效果只是删除该行。这是我的代码: private R
我已经使用以下方法在列表框中成功实现了拖放: Bea Stollnitz - 如何在数据绑定(bind)的 ItemsControl 之间拖放项目: http://www.zagstudio.com/
我有一个带有用于拖放文件的按钮的 C# 应用程序。我可以从我的桌面上获取 6 个文件并将其放到按钮上并让它处理这 6 个文件。 但是,当我从 DragDrop 事件启动线程并将文件路径传递到从 Dra
该方法连续执行两次,没有明显的理由这样做。它发生在 VS2010 Express (4.0) 和 VS2008 (3.5) 中。 public GUI() { InitializeCompon
我正在尝试以 wpf 形式实现拖放功能。目的是通过拖拽实现用户控件的复制(类似于windows 7文件夹复制使用ctrl + Mouseleftbutton拖放)。当 ctrl 键没有按下时,drop
我在 Canvas 上有一个拖放操作,当一个对象被拖入和拖出它时,它应该做一些事情。我的问题是 DragEnter/DragLeave 事件在鼠标将对象移动到它上面时不断触发,而不仅仅是在进入/退出时
我使用这个组件来处理拖放文件 http://melander.dk/delphi/dragdrop unit DragThread; interface uses Classes,DragDrop
我正在寻找一种方法来确定在发生 DragDrop 事件后拖动了 toolStrip 中的哪个项目,我想要做的就是为工具条中的每个项目创建一个具有不同案例的切换案例,但我似乎无法找到一种比较它们的方法。
我有以下代码,我试图在其中实现拖放功能: public Location_Alert() { InitializeComponent(); #region Fo
从过去几个月开始,我就开始研究它。我在处理拖放事件时遇到问题。 我有两个并排放置的网格,我正在执行从左侧网格(网格 A)到右侧网格(网格 B)的拖放操作。我在 Grid B 上同时使用了 Before
我有一个固定位置的 CssLayout 和一个 DDVerticalLayout 子组件。我根据用户交互动态填充此垂直布局以创建 ListView 。当列表变得比显示区域大时,我遇到了问题。它可以正确
我使用 DragDrop.DoDragDrop 将文件从我的 C# WPF 应用程序复制到 Windows 资源管理器。但是,文件的实际复制导致我的 WPF 应用程序卡住,直到复制完成。此复制操作可能
我正在尝试 TDropFileTarget Melander 的 DragDrop 的组成部分套房。目标是在拖放文件后执行某些任务。另外,如果在处理过程中出现问题,我希望收到异常。 似乎 OnDrop
我是一名优秀的程序员,十分优秀!