开发者

C# - Cross-thread operation - Create Control in thread, add to main form

开发者 https://www.devze.com 2023-04-12 23:56 出处:网络
I have an older form that I really don\'t want to rewrite at this point, so what I\'m doing is loading the form and then adding it to a panel in the new UI form. This is working fine, but it\'s slow.

I have an older form that I really don't want to rewrite at this point, so what I'm doing is loading the form and then adding it to a panel in the new UI form. This is working fine, but it's slow. The old form does a lot of loading and gathering of data and it's not very efficient. So as a result larger records take up to 30 seconds to load. As you know, creating the form then "locks up" the main UI for about 30 seconds while it loads the old form. This is the action I'm trying to prevent. I want to load the new form, display a "Loading" gif in the blank panel, and then once the old form is loaded remove the "Loading" image and add the form as a control.

And here starts the problem.

I've tried creating a Background Worker but this causes a STA error (old form has a few threaded data loadings of it's own), and since I can't change the worker to STA I stopped trying.

I've tried to create an Invoke (and BeginInvoke) and while this works, it doesn't really load the old form in the thread. It simply sends it back to the UI t开发者_运维知识库hread and does the work there. Again this hangs the UI. I.E.: Not what I want.

I've tried to create a delegate and trigger it as an event in the thread, but I get the same results as below...

I've created a thread, set STA on it, started it and then did a while loop with a DoEvents waiting on it to finish. Of course this all seems to work up to the point of accually adding the form to the panel, and then I get the "Control 'ChartForm' accesses from a thread other than the thread it was created on". In this error 'ChartForm' is the old chart that was loaded in the thread.

I've tried the above method, but I instead used a private static field to hold the creating of the old form, and then adding it to the panel once the thread is completed. This is in the method that created the thread, just after the while loop. Same error.

So, I've used the above method in other places with DataTables and didn't have any issue getting the data back to the main thread for use with DataBinding. I know that this is a little different but I didn't think that it would be this hard to do.

Below is the code that I have tried to use that seems to be the closest to what I want.

private static _ChartForm;
private void LoadPatientChart()
{
    ClearMainPanel(); // Removes any loaded ChartForms from Panel
    if (_Patient == null) // Test to make sure a patient is loaded
        return;

    loadingPanel.Visible = true; // Displays the "Loading" gif

    Thread thread = new Thread(new ThreadStart(this.GetChartForm));
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    while (thread.ThreadState != ThreadState.Stopped)
        Application.DoEvents(); // Keeps the UI active and waits for the form to load

    this.ChartPanel.Controls.Add(_ChartForm); // This is where the error is
    loadingPanel.Visible = false; // Hide the "Loading" gif

}

private void GetChartForm()
{
    ChartForm chartForm = new ChartForm(_Patient.AcctNum.ToString(), false);
    chartForm.TopLevel = false;
    chartForm.FormBorderStyle = FormBorderStyle.None;
    chartForm.Dock = DockStyle.Fill;
    chartForm.Visible = true;
    _ChartForm = chartForm;
}


It's really not a good idea to create UI controls on any other thread than the UI thread. It is technically possible, but it's difficult to manage, especially if the new thread is a "temporary" one.

What you really need to do is refactor out the work that the ChartForm is doing (on construction it appears?) and do that work on a background thread, and then return it to your UI thread and then create your ChartForm passing in the results of that work. IMHO this is a better design anyways; although it may be a lot of work for you.


I don't think what you want is possible without refactoring this "old form". There is only one UI thread, and all UI elements must be created on that thread to be displayed to the user.

I would suggest refactoring the form to display initially without any data (or maybe with a loading image), and then have the form start a background task using BackgroundWorker to perform the long running tasks that are not UI related (going to a database, etc.) Once the worker is complete, then you can run the code that initializes the Form's data elements. This will keep the UI responsive for as long as possible while the blocking tasks are performed.


I've tried to create an Invoke (and BeginInvoke) and while this works, it doesn't really load the old form in the thread. It simply sends it back to the UI thread and does the work there. Again this hangs the UI. I.E.: Not what I want.

You must update the user interface on the main thread, you do not have any choice, if its still hanging then your doing the calculations in the wrong thread.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号