Adephx
Adephx

Reputation: 317

AppDomain.GetCurrentThreadId(); made it work, not sure how?

Warning: I started coding in c# 1 month ago with no pre-existent programming knowledge.

I'm using the following method to center the MessageBox inside the parent window: How to get MessageBox.Show() to pop up in the middle of my WPF application?

I received warning claiming that the AppDomain.GetCurrentThreadId(); method is obsolete, so I found a "solution" here: http://www.pinvoke.net/default.aspx/kernel32/GetCurrentThreadId.html

As the code wouldn't work with uint, I changed it into int and then changed the AppDomain.GetCurrentThreadId(); into GetCurrentThreadId();

    [DllImport("kernel32.dll")]
    static extern int GetCurrentThreadId();

I've written a fairly complex database program that seems to work flawlessly so far and I understand most of the code behind it, so I would really appreciate if someone could explain to me what exactly I did with the mentioned "solution".

Upvotes: 1

Views: 584

Answers (1)

IS4
IS4

Reputation: 13207

GetCurrentThreadId is a WinApi function, located in kernel32.dll (implemented in C). C# allows you to specify how to call this function via a mechanism called P/Invoke which dynamically finds the function in the specified library by its name, and properly marshals the arguments and return value, if any.

If .NET cannot provide what you need, it is generally fine to resolve to using P/Invoke, but note that this is not a 100% portable solution. On other platforms, the function is likely not to be present, but if you limit yourself to Windows (as you already do, looking at the answer you linked), it is okay.

Upvotes: 2

Related Questions