gpt4 book ai didi

c# - windows窗体控件的多线程访问c#

转载 作者:行者123 更新时间:2023-11-30 21:17:12 27 4
gpt4 key购买 nike

我正在尝试遍历 ListBox 上的选定值以查看是否有匹配项,如果匹配则设置一个 bool 值。我有这段代码在没有线程时可以工作:

for (int i = 0; i < FileTypesExcludedListBox.SelectedItems.Count; i++)
if (currentFiles[currentFileLoc].EndsWith(FileTypesExcludedListBox.SelectedItems[i].ToString()))
doNotCompare = true;

现在我已经更改了应用程序,所以这是在没有创建控件的线程内发生的。我以前使用过匿名委托(delegate)来更新文本标签,但我需要返回值,但我不确定该怎么做...

最佳答案

使用窗体的调用方法 (this.Invoke) 从线程中执行委托(delegate)。

private delegate void YourDelegate(obj param1,obj param2);

private void YourFunction(obj param1,obj param2)
{
//Write Code Here
}

//Call this from your Thread
this.Invoke(new YourDelegate(YourFunction), new object[] { param1,param2});

关于c# - windows窗体控件的多线程访问c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4920575/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com