Thread not starting.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fr33dan
    New Member
    • Oct 2008
    • 57

    #1

    Thread not starting.

    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:
    Code:
    windowThread = new Thread(new ThreadStart(Log.startWindow));
    windowThread.Name = "Window";
    windowThread.Start();
    And this is the method called:
    Code:
    public static void startWindow()
    {
        MessageBox.Show("HEY YOU");
        LogWindow form = new LogWindow();
        Window = form;
        form.ShowDialog();
    }
    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
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    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...

    Comment

    • Fr33dan
      New Member
      • Oct 2008
      • 57

      #3
      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.

      Comment

      • PRR
        Recognized Expert Contributor
        • Dec 2007
        • 750

        #4
        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)
        Code:
        public class MyClass : System.EventArgs
            {
                private  System.Timers.Timer aTimer;
                // Timer to execute so that UI is updated regurarly 
                // 
                
               
                public  delegate void MyDelegate(string e);
                public static event MyDelegate del;
                
        
                public MyClass()
                {          
        
                    aTimer = new System.Timers.Timer(10000);
                    
                    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
                    
                    aTimer.Interval = 2000;
                    aTimer.Enabled = true;
        
                }
                
        
                private static void OnTimedEvent(object source, ElapsedEventArgs e)
                {
                    if (del != null)
                    {
                        del("Prr time is  :" + DateTime.Now.ToString());
                    }
                    
                    //Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
                }
        
            }
        * The class constructor will initialize the timer function...

        Comment

        • PRR
          Recognized Expert Contributor
          • Dec 2007
          • 750

          #5
          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

          Comment

          • Fr33dan
            New Member
            • Oct 2008
            • 57

            #6
            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

            Comment

            • PRR
              Recognized Expert Contributor
              • Dec 2007
              • 750

              #7
              Welcome! Do continue to post on Bytes ....

              Comment

              Working...