C#中的[帮助]线程错误 [英] [Help] thread error in C#

查看:85
本文介绍了C#中的[帮助]线程错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是C#的初学者。我有函数调用AutoCompleteCBO来自动完成下面的组合框。但是当我在表单Load事件中调用此函数时,系统显示消息:当前线程必须设置为单线程单元(STA)模式才能进行OLE调用。确保您的Main函数标记了STAThreadAttribute。我已经尝试在Program.cs中的main()顶部添加[STAThread],但仍然有此错误。请帮助我。



< pre> private  void  AutoCompleteCBO( ComboBox cbo,DataTable dt, string  rowName)
{

AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
cbo.AutoCompleteSource = AutoCompleteSource.CustomSource;
for int i = 0 ; i < dt.Rows.Count; i ++)
{
auto.Add(dt.Rows [i] [rowName]的ToString());
}
cbo.AutoCompleteCustomSource = auto;

}





我的尝试:



我已经尝试在Program.cs的main()顶部添加[STAThread] 

解决方案

猜测,您的方法是从UI线程以外的线程执行的 - 调用对UI控件(如组合框)的任何访问权限,或者将该代码移回主线程。

HI all, I'm a beginner in C#. I have function call AutoCompleteCBO to autocomplete combobox belows. But when i call this function in form Load event, system show message: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. I already tried add [STAThread] on the top of the main() in Program.cs but still have this error. Pls help me.

<pre>private void AutoCompleteCBO(ComboBox cbo, DataTable dt, string rowName)
        {
            
                AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
                cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                cbo.AutoCompleteSource = AutoCompleteSource.CustomSource;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    auto.Add(dt.Rows[i][rowName].ToString());
                }
                cbo.AutoCompleteCustomSource = auto;    
           
        }



What I have tried:

I already tried add [STAThread] on the top of the main() in Program.cs 

解决方案

At a guess, your method is being executed from a thread other than the UI thread - either Invoke any access to UI controls such as your combo box, or move that code back to the main thread.


这篇关于C#中的[帮助]线程错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆