WinForms线程调用处理

时间:2014-06-06 08:27:14

标签: c# multithreading winforms

此代码从其创建的线程以外的其他线程运行 线程从StartScanning

的构造函数中获取
public StartScanning()
{
    InitializeComponent();

    Thread _IMSS_THREAD = new Thread(_IMSS_START_SCANNING);
    _IMSS_THREAD.IsBackground = true;
    _IMSS_THREAD.Start();
}

主要表格

StartScanning _IMSS_START_SCANNING = StartScanning._IMSS_CREATE_CONTROLE();
_IMSS_START_SCANNING._IMSS_ON_ALL_SCAN_COMPLETE += _IMSS_ON_SCAN_COMPLETE;
this._IMSS_MainPanel.Controls.Add(_IMSS_START_SCANNING);

在扫描完成用户控制时,此代码采用主要形式:

ScanComplete _IMSS_ON_COMPLETE = new ScanComplete();

public void _IMSS_ON_SCAN_COMPLETE(ref List<BetterListViewGroup> _IMSS_LIST_OF_GROUP_TARGETS)
{
    List<BetterListViewGroup> IMSS_LIST_OF_GROUP_TARGETS = _IMSS_LIST_OF_GROUP_TARGETS;
    _IMSS_ON_COMPLETE._IMSS_AddRangeTargets(ref IMSS_LIST_OF_GROUP_TARGETS);
    this.Invoke(new MethodInvoker(() =>
        {
            this._IMSS_MainPanel.Controls.Clear();
            this._IMSS_MainPanel.Controls.Add(_IMSS_ON_COMPLETE);
        }));
}

如果你看看这段代码,它运行正常,但它应该抛出 跨线程操作无效,导致我们启动程序时此UserControl

ScanComplete _IMSS_ON_COMPLETE = new ScanComplete();

在主线程(它是全局的)上创建并在我们使用

时创建
_IMSS_ON_COMPLETE._IMSS_AddRangeTargets(ref IMSS_LIST_OF_GROUP_TARGETS);

它向listview添加了一个{{1}}组的列表,它不在调用部分,但它不会抛出线程错误,为什么它不会抛出错误?

1 个答案:

答案 0 :(得分:0)

在构造函数中尝试:

public StartScanning()
{
    InitializeComponent();
    StartScanning.CheckForIllegalCrossThreadCalls = false;
}

请记住,这不是正确的方法,但这可以帮助您解决问题。搜索线程安全呼叫