|
Right.... fixed.
If you use some of the methods suggested in these comments to replace AppDomain.GetCurrentThreadId() with a method from System.Threading.Thread.... to fix the deprecation warning, this will NOT work in windows 7. So, instead, can I suggest the method used by Dana Hall in teh comments below.
In other words, within the class defition, add:
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId();
Then modify this line:
hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, hookProc, IntPtr.Zero, AppDomain.GetCurrentThreadId());
To this line:
hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, hookProc, IntPtr.Zero, GetCurrentThreadId());
Carl
|
|
|
|
|
This is a good article. I found the same issue as you Mr C P.
|
|
|
|
|
good article, you might want to show some of the code in the article and a screen shot of the messagebox to make it a better article.
|
|
|
|
|
I have change your code to make a countdown messagebox. I know change the text to show the countdown : SetWindowText(hWnd, nCountDown); but it change the caption of the messagebox not the real text.
Is someone know how to achive this issue ? Thanks
|
|
|
|
|
If two threads calls this MessageBoxEx, would this be thread safe?
I am a bit skeptical with the timer event ID being set to a constant 42.
~Kanna
|
|
|
|
|
Timers belong to an HWND. An HWND belongs to the thread it was created on. The WM_TIMER will be received on the thread the HWND belongs to. So what does it matter what the timer ID is? As long as no one else is using it for a given HWND you're good to go. And since this is a MessageBox its pretty safe no one else is using ID 42.
|
|
|
|
|
This looks brilliant but I am not sure it will work in PocketPC(2003)
I am getting 9 error messages the first of which is
The type or namesapce name MessageBoxoptions could not be found (are you missin etc...
The type or namespace nane IWin32Windows etc (eight messages based on this!!
Richard
|
|
|
|
|
This will not work on the pocket or compact framework. The implementation uses windows hooks which aren't available on the pocket.
Sorry!
modified on Tuesday, January 19, 2010 6:01 PM
|
|
|
|
|
hi
Great work
and thanks a lot for sharing too..
azam's
|
|
|
|
|
I want also say thanks for this gr eat article. Great work!!!
|
|
|
|
|
As compact framework does not have this User32.dll then how can we implement this functionality.
List of functions impoted not in coredll.dll
1.SendMessage
2.SetWindowsHookEx
3.GetWindowTextLength
4.GetWindowText
Can somebody help me out,
my mail id is yuvrajaa@integramicro.com/yuvraj_1981@walla.com
Yuvraj
|
|
|
|
|
Most functions should come from coredll.dll. However, some bad news. Windows CE doesn't support windows hooks. So you're SOL, sorry.
|
|
|
|
|
Anyone had any luck getting it to work with MessageBoxOptions.ServiceNotification? We have a service that does some checks, makes system changes if necessary, and then tells the user they need to reboot. Hoping to put up a message box with a countdown to an auto-reboot. Works great, until I put the MessageBoxOptions.ServiceNotification option on. Nuts.
|
|
|
|
|
[quote]http://www.gipsysoft.com/messagebox. You'll find a C++ implementation[/quote]
Why not use the MessageBoxTimeOut API function instead of having a whole freaking class?
|
|
|
|
|
Very interesting.
1) I had no idea there was such a function.
2) It's undocumented, it's generally not a good idea to use undocumented functions. Still, I would think you could try to use the undocumented version and if it's not there you could use this code.
3) Looking at another article on this site it appears that MessageBoxTimeOut is only available on WinXP. People do still write code for Win9X, WinNT, etc...
4) This technique for making a MessageBox with a timeout allows you do change the text of the buttons or move them around or do whatever else you need to do with MessageBox. The C++ code at gipsysoft does all of these things and more. I only needed the timeout so that's all I ported to C#.
|
|
|
|
|
Hello,
I like your MessageBoxEx with timer solution. I recently upgraded to Visual Studio 2005 beta 1 and get the following warning:
Warning 2 'System.AppDomain.GetCurrentThreadId()' is obsolete: 'AppDomain.GetCurrentThreadId has been deprecated because it does not provide a stable Id when managed threads are running on fibers (aka lightweight threads). To get a stable identifier for a managed thread, use the Thread object returned from Thread.CurrentThread.'
I cannot find a working substitute. Any ideas would be appreciated.
Thanks,
Dana Hall
|
|
|
|
|
That is interesting but I wouldn't worry about fibers much. They were intended to make the porting of Unix applications easier. I don't imagine they are used much in Windows apps.
SetWindowsHookEx is a windows API after all. Perhaps in the new version of the .NET framework there is an managed alternative to SetWindowsHookEx. If you want to get rid of the warning use P/Invoke to get the current thread id.
Regards,
Rodger
|
|
|
|
|
Rodger,
Thanks for the help. This is what I did and it worked. I added this P/Invoke signature.
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId();
Then modified this line:
hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, hookProc, IntPtr.Zero, AppDomain.GetCurrentThreadId());
To this line:
hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, hookProc, IntPtr.Zero, GetCurrentThreadId());
I am new to .NET, so this was a fun learning experiment.
Thanks again,
Dana Hall
|
|
|
|
|
This is off the top of my head, and I don't know if it will work in 2005, but try this instead...
System.Threading.Thread.CurrentThread.GetDomainID()
Or
System.Threading.Thread.CurrentThread.CurrentContext.ContextID()
|
|
|
|
|
Threading.Thread.CurrentThread.GetHashCode
GetHashCode on the thread object returns the thread ID. From MS Docs
|
|
|
|
|
I haven't tried this suggestion yet, but I have spent two days researching this problem on the NET to no avail until I stumbled across this message. Thanks very much. Maybe Microsoft should seriously think about a free brain transplant for some of its employees - who in their right minds would go looking for the thread ID under GetHashCode?
John Whattam
|
|
|
|
|
(int)AppDomain.GetCurrentThreadId()
change to:
System.Threading.Thread.CurrentThread.ManagedThreadId
|
|
|
|
|
I use VS 2010 with .NET 4 and i replaced
Int32 mainThreadId = AppDomain.GetCurrentThreadId();
with
Int32 mainThreadId = Process.GetCurrentProcess().Threads[0].Id;
It's not ideal but it works for my personal use and is not deprecated (for the moment).
|
|
|
|
|
This class is exactly what I was looking for. Very helpful and so easy - just one extra parameter.
Thanks again, I'll get a lot of use out of this.
Anne
|
|
|
|
|
please put a picture to your article
|
|
|
|