smnetserv
asked on
CallbackOnCollectedDelegate detected
Hi,
I am using a class to handle the keystrokes, this class is named globalKeyboardHook which is downloaded from internet. In my application there are two forms, when the first form is shown then only alphabets and number are allowed, for this i use the globalKeyboardHook class. When i show the second form then i will call globalKeyboardHook.unhook( ) and make my first form invisible. Similarly when i need to show the first form i will call globalKeyboardHook.hook(). So all keys other than alphabets and numbers are blocked. I have attached the globalKeyboardHook.cs
This works fine but some times it is throwing an exception. The exception is CallbackOnCollectedDelegat e was detected. In detail it says "A callback was made on a garbage collected delegate of type 'CafeControl!Utilities.glo balKeyboar dHook+keyb oardHookPr oc::Invoke '. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called."
Please help me
globalKeyboardHook.doc
I am using a class to handle the keystrokes, this class is named globalKeyboardHook which is downloaded from internet. In my application there are two forms, when the first form is shown then only alphabets and number are allowed, for this i use the globalKeyboardHook class. When i show the second form then i will call globalKeyboardHook.unhook(
This works fine but some times it is throwing an exception. The exception is CallbackOnCollectedDelegat
Please help me
globalKeyboardHook.doc
ASKER
Thank you very much for trying out, and waiting for your advice.
The tool which i am building is for a cyber cafe. In this case initially the system should be locked. For this i am using my first form, which covers entire screen. The user can key in the password (alphanumeric), and then after validation i will hide this form and the display the second form which will allow them to stop their usage. This second form is very small to hold a command button which i will show right top of screen, just before the form close button.
In this case all the keys should be blocked, if you can give me some other solution that is appreciated. the user can enter only the password, all other keys such as windows key, ctrl+Alt+Del, ctrl+shift+Esc, internet keys.
The tool which i am building is for a cyber cafe. In this case initially the system should be locked. For this i am using my first form, which covers entire screen. The user can key in the password (alphanumeric), and then after validation i will hide this form and the display the second form which will allow them to stop their usage. This second form is very small to hold a command button which i will show right top of screen, just before the form close button.
In this case all the keys should be blocked, if you can give me some other solution that is appreciated. the user can enter only the password, all other keys such as windows key, ctrl+Alt+Del, ctrl+shift+Esc, internet keys.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
global keyboard hook is GLOBAL meaning it catches all keystrokes the entire windows system. Unless you are trying to write a kiosk application or a keystroke logger this is NOT the code to be using. Try opening notepad and typing &%#! while your form alphanumeric form is showing: this is bound to get a reaction if you use it.
The exception speaks for itself globalKeyboardHook is getting invoked after you've made a call to unhook. The application no longer has access to the unmanaged sub system and your app is dead.
Now for the real question what should you be doing.... put this code (below) in the <form>.cs of the form that you want to allow alpha numeric only on. This will cause the form to constrain to upper and lower case alpha and numeric keys. I included some code to check numlock and if set allow the numeric keypad as well. I don't allow for . or - as being numeric you may need to add a bit of code for those cases if you want them.
oh and one more thing, STOP using globalKeyboardHook
Open in new window