C# Determine if the current thread is the main GUI thread.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nelsonbrodyk
    New Member
    • Mar 2008
    • 81

    C# Determine if the current thread is the main GUI thread.

    Hey All,
    Just curious, does anyone know if there is a way to check if the current executing thread is the main WPF GUI thread?

    I want to have it so that a non-threaded remoting call will fail if the current thread is the WPF GUI thread. The reason for this is that a long running call locks up the GUI, and during this time, we would like it to stay responsive.

    Does checking to see if it's a background thread do the trick?

    Thanks!
  • nelsonbrodyk
    New Member
    • Mar 2008
    • 81

    #2
    Anyone have any ideas?

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      Can you do it with the System.Reflecti on namespace?

      Comment

      • nelsonbrodyk
        New Member
        • Mar 2008
        • 81

        #4
        hmmmm how would that work?

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Originally posted by nelsonbrodyk
          hmmmm how would that work?
          Not sure did you check it out?

          Comment

          • nelsonbrodyk
            New Member
            • Mar 2008
            • 81

            #6
            Well, the thing is I have no idea how reflection would allow me to get the current main thread. It really doesn't make sense that I could get the applications GUI thread via reflection, since reflection is used to instantiate or use a class remotely.

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              Originally posted by nelsonbrodyk
              Well, the thing is I have no idea how reflection would allow me to get the current main thread. It really doesn't make sense that I could get the applications GUI thread via reflection, since reflection is used to instantiate or use a class remotely.
              Are you just trying to get a handle to the main process thread or the currently active window?

              Comment

              • nelsonbrodyk
                New Member
                • Mar 2008
                • 81

                #8
                Originally posted by RedSon
                Are you just trying to get a handle to the main process thread or the currently active window?
                hmmm, well the here is the challange:

                We have calls the we make for remoting over http. We do not want these calls to hang the UI. So we need to make the call, and still allow the UI to not lock up. So we need to spawn a separate thread correct?
                If so, once we get the data back, I believe we need to then allow UI updates. How does one go about this in C#? I figured if we had a handle on the gui thread we can just call BeginInvoke?

                Comment

                • jhaxo
                  New Member
                  • Dec 2007
                  • 57

                  #9
                  Originally posted by nelsonbrodyk
                  hmmm, well the here is the challange:

                  We have calls the we make for remoting over http. We do not want these calls to hang the UI. So we need to make the call, and still allow the UI to not lock up. So we need to spawn a separate thread correct?
                  If so, once we get the data back, I believe we need to then allow UI updates. How does one go about this in C#? I figured if we had a handle on the gui thread we can just call BeginInvoke?
                  Can you just name all your threads? then check thread.Name

                  Comment

                  • jhaxo
                    New Member
                    • Dec 2007
                    • 57

                    #10
                    Originally posted by jhaxo
                    Can you just name all your threads? then check thread.Name
                    Is this helpful?

                    You can use BeginInvoke to invoke a delegate in the UI as soon as the thread doing your call is completed.

                    first define the delegate. here is an example.

                    public delegate void mydelegate(stri ng aparameter);

                    mydelegate del = new mydelegate(some function);

                    then in the thread BeginInvoke(del ,new object[]{"im done"});

                    Comment

                    • RedSon
                      Recognized Expert Expert
                      • Jan 2007
                      • 4980

                      #11
                      Originally posted by jhaxo
                      Is this helpful?

                      You can use BeginInvoke to invoke a delegate in the UI as soon as the thread doing your call is completed.

                      first define the delegate. here is an example.

                      public delegate void mydelegate(stri ng aparameter);

                      mydelegate del = new mydelegate(some function);

                      then in the thread BeginInvoke(del ,new object[]{"im done"});
                      I think you can also accomplish this with message queues, if those exist in C#. You create another thread then pass messages between them.

                      Comment

                      • nelsonbrodyk
                        New Member
                        • Mar 2008
                        • 81

                        #12
                        Originally posted by RedSon
                        I think you can also accomplish this with message queues, if those exist in C#. You create another thread then pass messages between them.
                        We are actually using it currently by using begin invoke. It's really the only way I could see it working. However, in one part, we actually want to check to see if they did call begin invoke. If they didn't then they are running on the main thread and we throw an exception. Maybe naming them would work?

                        Comment

                        • openshac
                          New Member
                          • Feb 2009
                          • 1

                          #13
                          this.InvokeRequ ired

                          private void DoSomething(str ing message)
                          {
                          if (this.InvokeReq uired && this.IsHandleCr eated && !this.IsDispose d)
                          {
                          this.Invoke(new MethodInvoker(d elegate
                          {
                          DoSomething(mes sage);
                          }));
                          }
                          else
                          {
                          // your code here
                          }
                          }

                          Comment

                          • Plater
                            Recognized Expert Expert
                            • Apr 2007
                            • 7872

                            #14
                            Application.Mes sageLoop
                            "Gets a value indicating whether a message loop exists on this thread."
                            Could probably do that yeah?

                            Comment

                            • tlhintoq
                              Recognized Expert Specialist
                              • Mar 2008
                              • 3532

                              #15
                              hmmm, well the here is the challange:

                              We have calls the we make for remoting over http. We do not want these calls to hang the UI. So we need to make the call, and still allow the UI to not lock up. So we need to spawn a separate thread correct?
                              If so, once we get the data back, I believe we need to then allow UI updates. How does one go about this in C#? I figured if we had a handle on the gui thread we can just call BeginInvoke?
                              Another thought is that you don't need to be able to identify the GUI thread at all. Maybe you are making more work than you need to.
                              Spawn a new thread to do the task (from the GUI thread/class or some other thread/class), and register for an event that is inside the class you spawn.
                              When that second thread completes its task it then raises an event (optionally with eventargs that contain data you need to pass back to the first class). It doesn't know or care who is listening, because you don't want or need it to be tightly bound. The first thread hears this event and reacts accordingly (which can include disposing of the second class, unregistering from events it throws etc)
                              Events tutorial (including Form to Form which is the same as class to class)

                              Comment

                              Working...