我在教自己C#(我还不太了解).在这个简单的例子中:
bool? n = null;
Console.WriteLine("n = {0}", n);
Console.WriteLine("n.ToString() = {0}", n.ToString());
Console.WriteLine("n.GetHashCode() = {0}", n.GetHashCode());
// this next statement causes a run time exception
Console.WriteLine("n.GetType() = {0}", n.GetType());
Run Code Online (Sandbox Code Playgroud)
直观地,我理解为什么GetType()方法会抛出异常.实例n是null,这可以解释这个但是,为什么我在使用n.GetHashCode()和ToString()时出于同样的原因没有得到异常?
谢谢您的帮助,
约翰.
作为练习,我正在编写一些代码来显示进程内的O/S进程和O/S线程(如Sysinternals进程资源管理器那样).
我发现.net的ManagedThreadId(s)不是O/S线程ID.经过一番阅读后,我遇到了AppDomain.GetCurrentThreadId().不幸的是,该功能被标记为"过时"(这可能意味着将来"不可用").我找到的一个解决方案是使用InteropServices直接调用Win32 GetCurrentThreadId.我很好,但是,它感觉与.net哲学背道而驰.
我的问题是:是否有一种CLR"友好"的方式来获取当前线程的真实ID?
作为参考,这里是一段代码,显示了我到目前为止所尝试的内容.// 1和// 2显示正确的线程ID,// 3和// 4尝试以CLR友好的方式获取相同的信息(但它们不起作用.)
谢谢您的帮助,
约翰.
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId();
static void Main(string[] args)
{
// AppDomain.GetCurrentThreadId() is "obsolete"
int ThreadId1 = AppDomain.GetCurrentThreadId(); // 1
// not the ".net" way of doing things
int ThreadId2 = GetCurrentThreadId(); // 2
// "no joy" attempts to get the same results I got above
int ThreadId3 = Process.GetCurrentProcess().Threads[0].Id; // 3
int ThreadId4 = Thread.CurrentThread.ManagedThreadId; // 4
Console.WriteLine("ThreadId1: {0}, ThreadId2: {1}, ThreadId3: {2}, " +
"ThreadId4: {3}",
ThreadId1, …Run Code Online (Sandbox Code Playgroud) 阅读VS2008帮助文件我已经发现退出线程(在.net中)的简洁方法是使用return语句(在C#中)或让线程到达方法的末尾.
但是,我还没有找到允许我设置线程退出代码的方法或属性,也没有找到检索它的方法(因为它是使用Win32 API完成的).因此,问题是,我如何使用C#和.net做到这一点?
谢谢您的帮助,
约翰.
我正在使用Delphi编写一个简单的应用程序,需要接受删除的目录名称.虽然我使用的是Delphi,但这个程序是直接的Windows API(没有VCL,没有面向对象的东西)就像它在普通的C中一样.
我有一些代码已经使用普通窗口(由CreateWindowEx创建).当我转换该代码以使用对话框时,拖放功能打破了,我无法弄清楚原因.
这是我的代码(部分)工作得很好:
function WndProc (Wnd : hWnd; Msg, wParam, lParam : DWORD) : DWORD; stdcall;
{ main application/window handler function }
const
DROPPED_FILE_COUNT = -1;
var
FileCnt : integer; { number of files dropped }
Filename : packed array[0..MAX_PATH] of char; { filename buffer }
DropInfo : HDROP absolute wParam; { wParam points to Drop...}
I : integer; { for loop }
begin
WndProc := 0;
{ let the Windows default handler take care of everything }
case … 我刚学习Blend/Silverlight/VS2010/.net /等.我有一个驻留在网络驱动器上的简单项目.当我告诉VS2010重建项目时,我收到以下错误消息:
------ Rebuild All started: Project: MySilverlightApplication, Configuration: Debug Any CPU ------
MySilverlightApplication -> H:\PRJ\VisualStudio\ExpressionBlend\Unleashed\MySilverLightApplication\MySilverlightApplication\Bin\Debug\MySilverlightApplication.dll
C:\Program Files\MSBuild\Microsoft\Silverlight\v4.0\Microsoft.Silverlight.Common.targets(214,9):
error : Could not load the assembly
file:///H:\PRJ\VisualStudio\ExpressionBlend\Unleashed\MySilverLightApplication\MySilverlightApplication\obj\Debug\MySilverlightApplication.dll.
This assembly may have been downloaded from the Web. If an assembly has been downloaded from the Web,
it is flagged by Windows as being a Web file, even if it resides on the local computer.
This may prevent it from being used in your project. You can change this designation by changing the …