-3

I need to generate a GUI for a WinForm in Thread1 using the result of Thread2.

However, Thread1 should start from Thread2.

If the result of Thread2 is equal to 1, I need to show a grid on my WinForm. If not, I need to show a tab control on my WinForm.

Serzhan Akhmetov
  • 2,898
  • 2
  • 13
  • 26
  • 2
    Why Are You Writing This Way It's Hard To Read – Sergey Berezovskiy Feb 19 '14 at 08:56
  • This makes zero sense to me. – Gusdor Feb 19 '14 at 08:58
  • 2
    possible duplicate of [How to update the GUI from another thread in C#?](http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c) – Lex Li Feb 19 '14 at 09:00
  • You can't update the UI from another thread. There are multiple answers already on how to marshal changes to the UI thread for updating. In .NET 4.5 it's very easy to do using `async\await`. In .NET 4.0 it's slightly more involved using Tasks. In previous versions it's a bother using either BackgroundWorker or Invoke – Panagiotis Kanavos Feb 19 '14 at 09:06
  • [link](http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c) this is not my answer – Mohsen Vaziri Feb 19 '14 at 09:06
  • Please, do not include information about a language used in a question title unless it wouldn't make sense without it. Tags serve this purpose. – Ondrej Janacek Feb 19 '14 at 09:39

4 Answers4

1

The UI thread must build it or atleast add it to the form.

But in Thread 2 you can collect all required information or maybe controls and pass them by invoke or as BackgroundWorker result to the UI thread.

ZoolWay
  • 5,411
  • 6
  • 42
  • 76
  • can you help me by code? – Mohsen Vaziri Feb 19 '14 at 09:04
  • 1
    @MohsenVaziri first *you* post some code showing what you are trying to do. – L.B Feb 19 '14 at 09:08
  • I am Not Idea For Coding For this Problem, And Can Not Find Result For This Problem By Searching – Mohsen Vaziri Feb 19 '14 at 09:10
  • We do not know how you create your controls for data for that controls. The controls itself should not be required on another thread. Maybe your `BackgroundWorker` should just load the data whcih will be loaded into your UI controls which already exist. Have a look here: http://stackoverflow.com/questions/6365887/can-you-link-to-a-good-example-of-using-backgroundworker-without-placing-it-on-a The DoWorkEventArgs class has a Result object where you can pass your data to a thread in the foreground handled by OnRunWorkerCompleted event. – ZoolWay Feb 19 '14 at 09:13
1

You should only keep a single UI thread, and never create any UI elements in another thread.

Microsoft has many articles on this topic and demonstrates the correct way to handle async operations, such as

http://msdn.microsoft.com/en-us/library/ms951089.aspx

Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • I Know But I Need to Make My UI From Other Thread But I Work On abnormal Hardware. – Mohsen Vaziri Feb 19 '14 at 09:16
  • No matter what hardware you are on, you should follow the rule of thumb. Otherwise, you expect issues that nobody but yourself has to face. – Lex Li Feb 19 '14 at 09:43
0

If I understood you correctly there is a need of creating GUI elements in one thread for later using them in GUI thread.

And as far as I remember it is not possible because control remembers the thread it was created on and later checks whether calls are made within that thread.

I'd recommend you to generate data only in background thread and pass it to gui thread where you can bind this data or generate ui elements to represent it. Moreover it conforms to the best practice of separation of concerns.

Lex Li has posted the link in comments to the question devoted to passing data to GUI thread.

Community
  • 1
  • 1
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
0

I am not sure, I can understand your problem correctly but here is the code to create UI controls in a different thread.

var th = new Thread(() =>
    {
        //A sample form with a RichTextBox control.
        var f = new Form();
        f.Controls.Add(new RichTextBox() { Dock = DockStyle.Fill });

        Application.Run(f);
    });

th.SetApartmentState(ApartmentState.STA);
th.Start();
L.B
  • 114,136
  • 19
  • 178
  • 224