简体   繁体   English

C# - 多线程 - InvokeRequired - 奇怪的行为?

[英]C# - Multithreading - InvokeRequired - Strange behaviour?

I've got a question regarding multithreading in an application. 我对应用程序中的多线程有疑问。 I've got 2 forms - one mainForm and one editForm. 我有2个表单-一个mainForm和一个editForm。 The editForm is called by the mainForm on user request. 根据用户请求,mainForm调用editForm。 The editForm has implemented a datagrid, which should be filled by a thread. editForm已实现了一个数据网格,该网格应由一个线程填充。 I've done it this way: 我这样做是这样的:

    try
{
                InitializeComponent();
                Thread loadingDatagridThread = new Thread(new ThreadStart(() =>
                {
                                this.LoadDataGrid();
                }));
                loadingDatagridThread.Name = "LoadingDatagridThread";
                loadingDatagridThread.Start();
}
catch (Exception exp)
{
                throw exp;
}

The "LoadDataGrid" method looks like this: “ LoadDataGrid”方法如下所示:

private void LoadDataGrid()
{
                try
                {
                                this.picBox_Loading.Visible = true;
                                if (this.InvokeRequired == true)
                                {
                                                this.HaulierList = new Classes.HaulierCollection(Classes.HaulierCollectionKind.ByStopFlag, new object[] { 0 });
                                                this.BeginInvoke(new LoadDataGridDelegate(this.LoadDataGrid));
                                                return;
                                }
                                else
                                {
                                 //Fill the grid
                                }
                }
}

The collection of the datagrid (HaulierCollection) is being loaded while it is in the thread. 数据网格的集合(HaulierCollection)在线程中时正在加载。 The datagrid then is being filled when in the Main Thread, when InvokeRequired is false. 数据网格然后被填充在主线程时,当InvokeRequired是假的。 This scenario works fine, if I work in Visual Studio and test it in Debug mode. 如果我在Visual Studio中工作并在“调试”模式下对其进行测试,则此方案工作正常。 I can close and re-open the editForm and the data of the datagrid is always being loaded. 我可以关闭然后重新打开editForm,并且datagrid的数据始终处于加载状态。 BUT if I start the EXE file of the solution from the debug folder without using Visual Studio Debug mode, the datagrid of the editForm is being filled only the first time I open it. 但是,如果我在不使用Visual Studio调试模式的情况下从调试文件夹中启动解决方案的EXE文件,则仅当我第一次打开它时,才会填充editForm的数据网格。 The next times the datagrid is always empty. 下次datagrid始终为空。 I found out, that this behaviour is caused by the InvokeRequired property. 我发现,此行为是由InvokeRequired属性引起的。 The first time I open the editForm it is InvokeRequired = true, but if I close the form and re-open it, it is always false. 第一次打开editForm时,它是InvokeRequired = true,但是如果我关闭该窗体并重新打开,则始终为false。 But why? 但为什么? I dispose the form, when the form is closed. 关闭表单后,我将处理该表单。

private void EditForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Dispose();
    }

Why is InvokeRequired always false, when re-opening the editForm? 重新打开editForm时,为什么InvokeRequired始终为false? Can anybody help please? 有人可以帮忙吗?

It was the handleCreated issue. 这是handleCreated问题。 I put my thread start code into the form loaded (shown) event of my editForm and then it worked. 我将线程启动代码放入editForm的表单加载(显示)事件中,然后它起作用了。 Thanks for help. 感谢帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM