Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Backgroundworker_DoWork访问ImageList1,而不使用带有C的任何“委托”_C#_Winforms_Backgroundworker_Imagelist - Fatal编程技术网

C# 从Backgroundworker_DoWork访问ImageList1,而不使用带有C的任何“委托”

C# 从Backgroundworker_DoWork访问ImageList1,而不使用带有C的任何“委托”,c#,winforms,backgroundworker,imagelist,C#,Winforms,Backgroundworker,Imagelist,我有使用Net2的C系统Windows窗体,我只是一个初学者,因此请耐心等待,并希望在Backgroundworker进程中将图像添加到我的窗体1上的ImageList1中,以避免GUI冻结,同时在ListView中以文件名和图标显示包含10000多个文件的大目录中的文件名图标。 我将文件名存储在一个列表中,并使用AddRange方法将它们添加到Backgroundworker Finished事件中。图像不能以这种方式存储,否则会失去质量。我必须将它们直接添加到“嫁妆事件”部分的Backgro

我有使用Net2的C系统Windows窗体,我只是一个初学者,因此请耐心等待,并希望在Backgroundworker进程中将图像添加到我的窗体1上的ImageList1中,以避免GUI冻结,同时在ListView中以文件名和图标显示包含10000多个文件的大目录中的文件名图标。 我将文件名存储在一个列表中,并使用AddRange方法将它们添加到Backgroundworker Finished事件中。图像不能以这种方式存储,否则会失去质量。我必须将它们直接添加到“嫁妆事件”部分的Backgroundworker流程中。还有一个问题:在我的免费版MicrosoftVisualStudio2008中,出现了一条很长的错误消息,上面写着类似这样的奇怪信息:无法访问ImageList1,因为它是在另一个线程中创建的。。。????。
我打开一张微软的票,他们回答我,这是免费版本的问题,一个bug。是这样还是你能帮我。我在谷歌上找到了一些关于代理的信息,但恐怕我对如何利用这样一个结构知之甚少。我只知道如何声明一个int、一个字符串和一个列表,更不用说,我可以在表单上放置一些usercontrols,或者在事件中填充一些简单的代码。也许你们中的某个人也有类似的问题,可以提出一个准确的就地可理解的解决方案,随时可用。如何抑制ImageList中另一个线程的错误?也许在C语言中存在一个类似On Error Resume Next的语句,它强制代码继续执行,不管发生什么。然后,许多VB6应用程序将能够再次在夏普(dunno ofc)上运行。请帮助。

这是一种方法:

创建一个操作,并将访问ImageList1的代码放入该操作中。 在后台工作人员的工作中,使用ImageList1.InvokeActionNameHere替换已移动到操作的代码。 下面是一个简化的示例,代码将从backgroundworker更新标签的文本属性:

Action act = () =>
              {
                  label1.Text = "Test updating UI property from background thread";
              };
worker.DoWork += (o, args) =>
             {
                 label1.Invoke(act);
             };
worker.RunWorkerAsync();

您没有发布代码,因此我建议您使用这段代码,它应该可以工作:

使用backgroundWorker时,使用@Keith对我来说总是很方便的

BackgroundWorker bw = new BackgroundWorker { WorkerReportsProgress = true };

bw.DoWork += (sender, e) => 
   {
       //what happens here must not touch the form
       //as it's in a different thread

        Action AccessImageList = () => { int count = imgLst.Images.Count;//access the image list };
        this.Invoke(AccessImageList, new object[] { });
   };

bw.ProgressChanged += ( sender, e ) =>
   {
       //update progress bars here
   };

bw.RunWorkerCompleted += (sender, e) => 
   {
       //now you're back in the UI thread you can update the form
       //remember to dispose of bw now
   };

worker.RunWorkerAsync();