Last post Feb 23, 2014 09:17 PM by Fuxiang Zhang - MSFT
Feb 21, 2014 02:38 AM|abfrank|LINK
I have this code below that gets the username of the current loggon user in a window service application but my problem is that it is too slow and sometime result to OUT OF MEMORY EXCEPTION. The code queries WMI, i want a way to make it faster or another
code to get the current loggon username in a window application.
EnumerationOptions options = new EnumerationOptions();
options.Rewindable = false;
options.ReturnImmediately = true;
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT username FROM Win32_ComputerSystem WHERE Domain='local'", options);
System.Management.ManagementObjectCollection collection = searcher.Get();
string currentUser = (string)collection.Cast<System.Management.ManagementBaseObject>().First()["UserName"];
Feb 23, 2014 09:17 PM|Fuxiang Zhang - MSFT|LINK
my problem is that it is too slow and sometime result to OUT OF MEMORY EXCEPTION.
Thank you post the issue to asp.net forum.
For the exception "OUT OF MEMORY EXCEPTION", that means your window service application short of MEMORY in a heavy task.
# Increase the window service application memory. Allocating more memory to your application.
# It is very important disposing the object memory in time. You can try to ue the using keyword or dispose method complete it.
There are simlar issues with solutions, please check them.
Hope this helps, thanks.