G
Guest
the Hi
the method GetLastWin32Error should be a wrapper to win32's GetLastError.
"Retrieves the calling thread's last-error code value. The last-error code
is maintained on a per-thread basis. Multiple threads do not overwrite each
other's last-error code" (from MSDN)
But from time to time, when I use GetLastWin32Error, I got a returned value
which tell's an Error occured - also it happened on another .Net's Thread.
I looked on MSDN article about "GetLastWin32Error" I couldn't find that this
method is Thread Safe.
How can I be sure that I get an error from my thread, and not from another
one?
See following code for examle:
(it is not always find the exceptions - stop and restart multiple times)
[DllImport("kernel32.dll", SetLastError=true)]
static extern bool SetVolumeLabel(string lpRootPathName, string
lpVolumeName);
static void Main(string[] args)
{
System.Threading.ThreadStart threadStartErrors = new
System.Threading.ThreadStart(ThrowingErrors);
System.Threading.Thread threadErrors = new
System.Threading.Thread(threadStartErrors);
System.Threading.ThreadStart threadStartGetErrors = new
System.Threading.ThreadStart(GetErrors);
System.Threading.Thread threadGetErrors = new
System.Threading.Thread(threadStartGetErrors);
threadErrors.Start();
threadGetErrors.Start();
}
static void ThrowingErrors()
{
while (true)
{
SetVolumeLabel("XYZ:\\", "My Imaginary Drive ");
System.Threading.Thread.Sleep(100);
}
}
static void GetErrors()
{
while (true)
{
if (Marshal.GetLastWin32Error() != 0)
{
try
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
System.Threading.Thread.Sleep(150);
}
}
the method GetLastWin32Error should be a wrapper to win32's GetLastError.
"Retrieves the calling thread's last-error code value. The last-error code
is maintained on a per-thread basis. Multiple threads do not overwrite each
other's last-error code" (from MSDN)
But from time to time, when I use GetLastWin32Error, I got a returned value
which tell's an Error occured - also it happened on another .Net's Thread.
I looked on MSDN article about "GetLastWin32Error" I couldn't find that this
method is Thread Safe.
How can I be sure that I get an error from my thread, and not from another
one?
See following code for examle:
(it is not always find the exceptions - stop and restart multiple times)
[DllImport("kernel32.dll", SetLastError=true)]
static extern bool SetVolumeLabel(string lpRootPathName, string
lpVolumeName);
static void Main(string[] args)
{
System.Threading.ThreadStart threadStartErrors = new
System.Threading.ThreadStart(ThrowingErrors);
System.Threading.Thread threadErrors = new
System.Threading.Thread(threadStartErrors);
System.Threading.ThreadStart threadStartGetErrors = new
System.Threading.ThreadStart(GetErrors);
System.Threading.Thread threadGetErrors = new
System.Threading.Thread(threadStartGetErrors);
threadErrors.Start();
threadGetErrors.Start();
}
static void ThrowingErrors()
{
while (true)
{
SetVolumeLabel("XYZ:\\", "My Imaginary Drive ");
System.Threading.Thread.Sleep(100);
}
}
static void GetErrors()
{
while (true)
{
if (Marshal.GetLastWin32Error() != 0)
{
try
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
System.Threading.Thread.Sleep(150);
}
}