Threaded Form
-
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[^] -
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[^]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. :)
-
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. :)
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?
-
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?
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. :)
-
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. :)
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