Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Threaded Form

Threaded Form

Scheduled Pinned Locked Moved C#
csharpcom
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xfitr2
    wrote on last edited by
    #1

    I am having trouble with closing a form created in a seperate thread. This is what I am trying to accomplish: I have a program that runs in the taskbar. When a user double-clicks a key, a form is created in a new thread and is shown on top of all other forms. When the user clicks anywhere outside the form, the thread will close, but the program will continue to run in the background. This is similar to the Google Desktop functionality. The code works the first time the form is created in a new thread, but if the form has been closed once, you have to click on the form to make it active and then click outside of it before it will close. I have tried using the Deactivate and LostFocus events, but they both only work the first time the form is shown. Any suggestions would be greatful. I have included a snippet of code below. public class MyClass {   private Thread _thread;   private FMain _fmain;   private UserActivityHook _keyhook;   public MyClass() {     _keyhook = new UserActivityHook(false, true);     _keyhook += new KeyEventHandler(KeyDoubleClick);   }   private void KeyDoubleClick(object sender, KeyEventArgs e) {     if (e.KeyValue == 163) {       _thread = new Thread(new ThreadStart(OpenForm));       _thread.Start();     }   }   private void OpenForm() {     _fmain = new FMain();     _fmain.Deactivate += new EventHandler(form_Close);     _fmain.LostFocus += new EventHandler(form_Close);     _fmain.ShowDialog();   }   private void form_Close(object sender, EventArgs e) {     _thread.Abort();   } } References: The UserActivityHook was fortunately posted by George Mamaladze; http://www.codeproject.com/csharp/globalhook.asp[^]

    M 1 Reply Last reply
    0
    • X xfitr2

      I am having trouble with closing a form created in a seperate thread. This is what I am trying to accomplish: I have a program that runs in the taskbar. When a user double-clicks a key, a form is created in a new thread and is shown on top of all other forms. When the user clicks anywhere outside the form, the thread will close, but the program will continue to run in the background. This is similar to the Google Desktop functionality. The code works the first time the form is created in a new thread, but if the form has been closed once, you have to click on the form to make it active and then click outside of it before it will close. I have tried using the Deactivate and LostFocus events, but they both only work the first time the form is shown. Any suggestions would be greatful. I have included a snippet of code below. public class MyClass {   private Thread _thread;   private FMain _fmain;   private UserActivityHook _keyhook;   public MyClass() {     _keyhook = new UserActivityHook(false, true);     _keyhook += new KeyEventHandler(KeyDoubleClick);   }   private void KeyDoubleClick(object sender, KeyEventArgs e) {     if (e.KeyValue == 163) {       _thread = new Thread(new ThreadStart(OpenForm));       _thread.Start();     }   }   private void OpenForm() {     _fmain = new FMain();     _fmain.Deactivate += new EventHandler(form_Close);     _fmain.LostFocus += new EventHandler(form_Close);     _fmain.ShowDialog();   }   private void form_Close(object sender, EventArgs e) {     _thread.Abort();   } } References: The UserActivityHook was fortunately posted by George Mamaladze; http://www.codeproject.com/csharp/globalhook.asp[^]

      M Offline
      M Offline
      Michael Sync
      wrote on last edited by
      #2

      I don't think you need to close the form. Why don't you just hide the form when it is deactivated and show the form when it is activated?

      Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)

      X 1 Reply Last reply
      0
      • M Michael Sync

        I don't think you need to close the form. Why don't you just hide the form when it is deactivated and show the form when it is activated?

        Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)

        X Offline
        X Offline
        xfitr2
        wrote on last edited by
        #3

        Thank you for the suggestion. My problem though is trying to get the form to Hide/Close once the user clicks/types outside the form. Is there an event fired when this happens that I am not aware of or do I need to use a global hook? If I was to use a hook, how do I know that the source of the hook was not within the form?

        M 1 Reply Last reply
        0
        • X xfitr2

          Thank you for the suggestion. My problem though is trying to get the form to Hide/Close once the user clicks/types outside the form. Is there an event fired when this happens that I am not aware of or do I need to use a global hook? If I was to use a hook, how do I know that the source of the hook was not within the form?

          M Offline
          M Offline
          Michael Sync
          wrote on last edited by
          #4

          What about Deactivate event of form? private void Form1_Deactivate(object sender, EventArgs e) { this.Hide (); } I think that this "Deactivate" event is good enough. I dont think you need to use Window Hooking.

          Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)

          S 1 Reply Last reply
          0
          • M Michael Sync

            What about Deactivate event of form? private void Form1_Deactivate(object sender, EventArgs e) { this.Hide (); } I think that this "Deactivate" event is good enough. I dont think you need to use Window Hooking.

            Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)

            S Offline
            S Offline
            Spacix One
            wrote on last edited by
            #5

            you might be able to use .ActiveForm.Close() if you call it from the same thread which generated(showed) the form, else you'll get a "cross-thread exception" Is this form some type of information "pop-up" ? if so you could just call .Show() and then set a timer to call the .ActiveFrom.Close() on it. Also keep your current if you click on it and then click away it closes. This would be a better user experience, as they don't have to keep clicking away close the dialog. another problem is that you are calling abort() to your thread and not allowing it to exit cleanly All threads should include a method to stop their execution loop, .abort() is to throw an exception on a soft-locked thread. A situation where you know that it should have quit already but it is stuck in some loop. The exception doesn't allow the thread to finish its task. Think what will happen if a thread was in the middle of saving some file that was only 1/2 written when it was aborted.


            -Spacix All your skynet questions[^] belong to solved

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups