0

I am trying to use threading for the first time, and came up with this:

Thread myThread  = new Thread(() =>
  {
    test = Class1.getNumbers(param1, param 2);
    MessageBox.Show(test.toString());
  });

This doesn't do anything for me though. What did I do wrong?

Brian Gideon
  • 47,849
  • 13
  • 107
  • 150
TheGateKeeper
  • 4,420
  • 19
  • 66
  • 101
  • 4
    You're missing the call to `Start`. Try adding `myThread.Start()` after you define `myThread`. – M.Babcock Apr 12 '12 at 20:28
  • 4
    You are trying to show a message box from a worker thread. UI elements can only be created on the main thread. – Lubo Antonov Apr 12 '12 at 20:28
  • For learning threading in the .NET platform, I highly recommend reading through this: http://www.yoda.arachsys.com/csharp/threads/index.shtml – Katie Kilian Apr 12 '12 at 20:29
  • 3
    @lucas1024 : But `MessageBox.Show()` is a thread-safe static method. – H H Apr 12 '12 at 20:34
  • What do I do if I want to add some data from the thread to the UI? Do I have to pass the data from the thread to the main thread? – TheGateKeeper Apr 12 '12 at 20:42
  • 2
    @TheGateKeeper : _What do I do if I want to add some data from the thread to the UI?_ That's a separate question and it has been asked & answered here many times already. – H H Apr 12 '12 at 20:46
  • i just wanted to add that I dig the title of your question – Aaron Anodide Apr 12 '12 at 23:20

4 Answers4

6

You didn't start the thread.

myThread.Start();

(You also have a syntax error on the MessageBox call...)

jhenderson2099
  • 956
  • 8
  • 17
4

I am trying to use threading for the first time

Try to avoid using the Thread class directly. That is rarely needed.
Look into:

  • Backgroundworker
  • Task Parallel Library
  • ThreadPool

More or less in that order.

They all provide layers on top of threading to help you.

H H
  • 263,252
  • 30
  • 330
  • 514
  • I think in the mind of a beginner this might confuse because avoiding the "Thread class" and avoiding "Multi threading" are distinct but sound the same... just a thought - another thought - it might be a bad thing to skip the Thread class along the same lines as learning EF without ever learning rdbms/sql... (i'm not trying to critisize, just thinking..) – Aaron Anodide Apr 12 '12 at 20:38
  • @Gabriel: Yes, a good learning track would be to use the Thread _in a Console App_. And then use the Bgw in WinForms. – H H Apr 12 '12 at 20:45
3

Read this free chapter from a very good book (honest plug - no inscentive here):

It's consice and gives you the know-how you want in an incremental way so you don't go too high level too fast. The answer your question is in the first few paragraphs...

Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
1

You need to start the thread by calling Start.

Also, it is important to note that MessageBox.Show pumps messages. That is why it is working correctly1 without an explicit call to Application.Run on that thread. Most UI forms and controls do not work this way though. In general do not attempt to access or create any UI elements from a worker thread.


1A MessageBox can technically work from a thread other than the main UI thread, but it can cause some weird problems. For example, this message box could get stuck behind a modal dialog displayed by the UI thread. It is for this reason, among others, that it is not advised to display UI elements from a worker thread even if they are done so with self pumping calls like Form.ShowDialog or MessageBox.Show.

Brian Gideon
  • 47,849
  • 13
  • 107
  • 150