Connecting Tech Pros Worldwide Help | Site Map

Thread not starting.

Newbie
 
Join Date: Oct 2008
Posts: 17
#1: May 27 '09
Hi all,

I have a class that is written to display logging messages to a Form that I wrote. Because I don't want any other code to be stopped by the Form I decided to run the it in a separate thread. That thread however does not seem to be working.

This is the calling code:
Expand|Select|Wrap|Line Numbers
  1. windowThread = new Thread(new ThreadStart(Log.startWindow));
  2. windowThread.Name = "Window";
  3. windowThread.Start();
And this is the method called:
Expand|Select|Wrap|Line Numbers
  1. public static void startWindow()
  2. {
  3.     MessageBox.Show("HEY YOU");
  4.     LogWindow form = new LogWindow();
  5.     Window = form;
  6.     form.ShowDialog();
  7. }
If a just call startWindow() it runs without a problem. When it's called as shown here no MessageBox shows up and the Window variable remains null.

I've tried putting a breakpoint in the method but it never does anything. I can however see in the Threads window that the it does start and run. It just doesn't do anything.

The part that is really bothering me is that this code has worked in the past. I wrote a demonstration application that used this project as a library and it worked fine. Since then this code hasn't been touched.

Thanks in advance for you help,
Fr33dan

Edit: Crap wrote all this then realized I was in the wrong forum. Is it possible for a someone to move this to the C# forum?
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 700
#2: May 28 '09

re: Thread not starting.


If i understood you correctly, you want to
1. "Somewhere" in form1, start a thread which calls class function (or object) which in turn takes care of displaying logs?

If you create a thread then to update the UI with "that" thread... You will have to use delegates for that ...You cant update the UI from other threads directly ....
Post some( more) sample code and explain further...
Newbie
 
Join Date: Oct 2008
Posts: 17
#3: May 28 '09

re: Thread not starting.


Actually there is no other form at the moment since I'm working on backbone. Right now I only use the log class from a test file that writes a single statement to the log.

The log class is completely static and I use a static initializer to set everything up. My calling code is in that initializer (Along with the initialization of various other forms of logging output).

I am aware of that cross-threading calls cannot be made directly so I do use the invoke method later when I need to update the window. That code however is what initially gave me a problem (the window was null because this code never ran).

I would post more code but I'm out of the office at the today.
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 700
#4: May 29 '09

re: Thread not starting.


To have a different thread update the UI you can do the following....
* Have a class inherit from EventArgs...
* Declare a delegate and event in this class...
* Declare a Timer function ( assuming that you want to periodically update the UI)
Expand|Select|Wrap|Line Numbers
  1. public class MyClass : System.EventArgs
  2.     {
  3.         private  System.Timers.Timer aTimer;
  4.         // Timer to execute so that UI is updated regurarly 
  5.         // 
  6.  
  7.  
  8.         public  delegate void MyDelegate(string e);
  9.         public static event MyDelegate del;
  10.  
  11.  
  12.         public MyClass()
  13.         {          
  14.  
  15.             aTimer = new System.Timers.Timer(10000);
  16.  
  17.             aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
  18.  
  19.             aTimer.Interval = 2000;
  20.             aTimer.Enabled = true;
  21.  
  22.         }
  23.  
  24.  
  25.         private static void OnTimedEvent(object source, ElapsedEventArgs e)
  26.         {
  27.             if (del != null)
  28.             {
  29.                 del("Prr time is  :" + DateTime.Now.ToString());
  30.             }
  31.  
  32.             //Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
  33.         }
  34.  
  35.     }
  36.  
* The class constructor will initialize the timer function...
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 700
#5: May 29 '09

re: Thread not starting.


Run the program i attached ... you will get an idea.....

1. In the form load you start a thread which calls method to initialize the class object ...
2. You handle the event in the form.cs....( event defined in class)
3. In the event handler you call control.invoke... which basically makes it possible to update the UI part from non UI thread....

"Executes the specified delegate on the thread that owns the control's underlying window handle." MSDN
Attached Files
File Type: zip New WinRAR ZIP archive (2).zip (35.1 KB, 4 views)
Newbie
 
Join Date: Oct 2008
Posts: 17
#6: Jun 2 '09

re: Thread not starting.


Thank you for your help.

Your response made me realize that I had been over thinking the issue and missing the obvious solution. If I used the asynchronous BeginInvoke method (as opposed to Invoke) to call the methods on my Form the need for me to implement multiple threads goes away.

Thanks,
Fr33dan
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 700
#7: Jun 3 '09

re: Thread not starting.


Welcome! Do continue to post on Bytes ....
Reply