我刚刚学会了IntPtr.Zero无法与默认(IntPtr)进行比较的困难方法.有人可以告诉我为什么吗?
IntPtr.Zero == new IntPtr(0) -> "could not evaluate expression"
IntPtr.Zero == default(IntPtr) --> "could not evaluate expression"
IntPtr.Zero == (IntPtr)0 -> "could not evaluate expression"
IntPtr.Zero.Equals(IntPtr.Zero) --> "Enum value was out of legal range" exception
IntPtr.Zero.Equals(default(IntPtr)) --> "Enum value was out of legal range" exception
IntPtr.Zero == IntPtr.Zero --> true
new IntPtr(0) == new IntPtr(0) --> true
Run Code Online (Sandbox Code Playgroud) 办公室的一些人感到不舒服,不得不禁用"Outlook加载项的宏安全性",以使我的加载项工作.
如何获取加载项的安全证书,以便尽管当前存在安全设置但仍加载它?这值得吗?这需要花很多钱吗?
无论表单是最大化,最小化还是FormWindowState为Normal,调用Form.Visible都将返回true.
我想知道的是如何判断表单是否打开但是隐藏在另一个应用程序窗口后面.
如果是这种情况,我想把它带到前面,实际上让用户可以看到它.
我尝试了BringToFront()方法,但它没有用.我也试过调用Show()方法但是如果表单在另一个应用程序的窗口后面,它仍然是那样.
我发现问题的唯一解决方法是将Form的FormWindowState设置为Minimized/Maximized然后正常,但这有点像黑客并且看起来不太好.
有人能告诉我如何判断表格是否在另一个窗口后面以及如何将其带到前面?
这是使用VS 2005编写的.NET 2.0应用程序.它在运行.NET 2.0的系统上运行良好,但在运行.NET 4.0的系统上崩溃.这是代码的关键部分:
string selectCommand1 = ....
string connectionString1 = ....
using (OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand1, connectionString1))
{
try
{
adapter.Fill(table1);
}
catch
{
MessageBox.Show("error");
}
}
string selectCommand2 = ....
string connectionString2 = ....
using (OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand2, connectionString2))
{
try
{
adapter.Fill(table2);
}
catch
{
MessageBox.Show("error");
}
}
Run Code Online (Sandbox Code Playgroud)
同样,它在.NET 2.0下运行,在.NET 4.0下崩溃
ConnectionStrings 1和2引用不同的.xls文件.
我发现,解决这个问题的方法是OleDbDataAdapter的的using语句之前声明和初始化类型的OleDbConnection的字段变量,设置ConnectionString属性和Open()吧.如此:
OleDbConnection connection = new OleDbConnection();
.......
connection.ConnectionString = connectionString1;
connection.Open();
using (OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand1, connection))
{
try …Run Code Online (Sandbox Code Playgroud) 以下代码不会登录到yahoo.应该如何重写?
(当然,"用户名"和"密码"将替换为我的实际帐户名和密码.)
static void Main(string[] args)
{
string input = string.Format("username={0}&passwd={1}", "<username>", "<password>");
WebRequest request = HttpWebRequest.Create("https://login.yahoo.com/config/login");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(input);
writer.Close();
StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream());
string x = reader.ReadToEnd();
Console.Read();
}
Run Code Online (Sandbox Code Playgroud) 以下代码的缺点是,在主线程重置waithandle之后,工作线程既不会立即终止也不会执行最终操作.相反,它将继续执行它正在进行的操作,直到它到达循环的下一次迭代,此时它将无限期地被阻塞.
static void Main()
{
ManualResetEvent m = new ManualResetEvent(true); // or bool b = true
Thread thread = new Thread(new ThreadStart(delegate()
{
while(m.WaitOne()) //or while(b)
{
//do something
}
//perform final operation and exit
}));
thread.Start();
//do something
m.Reset(); //or b = false
//do something else
}
Run Code Online (Sandbox Code Playgroud)
下面的代码有一个缺点,它使用Abort()方法(有人说它应该不惜一切代价避免),但完全正是我正在寻找的:强制工作线程突破循环主线程告诉它这样做,执行最后一个操作,然后退出.
static void Main()
{
Thread thread = new Thread(new ThreadStart(delegate()
{
try
{
while(true)
{
//do something
}
}
catch(ThreadAbortException e)
{
//perform final operation and exit
}
}));
thread.Start();
//do …Run Code Online (Sandbox Code Playgroud) 应用程序本身长度为2000行,因此在此处粘贴代码是没有意义的,特别是因为其中一个用户收到的异常没有给出关于我的代码的哪个部分导致问题的任何提示.
顺便说一句,该应用程序只是一个带有datagridview的Windows窗体,通常显示的行数不超过几百行,还有一些其他控件.在它崩溃之前,它正在非常缓慢地加载datagridview的每一行的单元格.(但没有其他用户遇到过同样的问题.)
例外文本如下.有人可以查看它并告诉我它是由我的代码出错的东西引起的,还是可能与遇到此异常的用户的特定设置不兼容?
我注意到下面的描述说内存已损坏.这是否意味着用户的计算机有坏RAM?
************** Exception Text **************
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Drawing.SafeNativeMethods.Gdip.GdipDrawRectangleI(HandleRef graphics, HandleRef pen, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Drawing.Graphics.DrawRectangle(Pen pen, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Windows.Forms.ControlPaint.DrawFlatCheckBox(Graphics graphics, Rectangle rectangle, Color foreground, Brush background, ButtonState state)
at System.Windows.Forms.ControlPaint.DrawFlatCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state)
at System.Windows.Forms.ControlPaint.DrawCheckBox(Graphics graphics, Int32 x, Int32 y, Int32 width, Int32 height, ButtonState …Run Code Online (Sandbox Code Playgroud) 此表单具有NotifyIcon对象.当用户单击"关闭"按钮时,我希望表单不要关闭但是变得不可见.然后,如果用户想要再次看到该表单,他可以双击系统托盘中的图标.如果用户想要关闭表单,他可以右键单击该图标并选择"关闭".
有人能告诉我如何使关闭按钮不关闭表单但让它隐形吗?
(或者,如果有人能够想出一种更好的方法来实现同样的目标)
如果你有一个变量,那么就有一个与该变量相关的内存地址,在指针变量的情况下,该内存地址的"值"是对内存地址的引用,该地址保存指针指向的实际数据. .
所以,如果我有:
for (int x = 0; x < 2; x++)
{
char * a = (char*)malloc(20);
printf("%p\r\n", &a);
printf("%p\r\n", a);
}
Run Code Online (Sandbox Code Playgroud)
输出应该是这样的:
00999999
04427310
00999999
0442ECF0
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,第一个和第三个内存地址对于循环的每次传递期间声明的指针变量保持不变,我的理解是这是因为前一个变量超出范围而下一个可用地址是相同的地址.
这种泛化可以扩展到循环内声明的所有变量,还是有异常?
举个例子,我看过很多C#代码如下:
XmlNode n = GetXmlNode();
String[] attributes = new String[n.Attributes.Count];
for (int x = 0; x < n.Attributes.Count; x++)
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
现在,如果这是java,我们可以编写类似于以下的代码,但我们会犯getAttributes()两次调用该方法,如果我没有弄错,有一条规则说不是多次调用同一个方法,只是声明一个变量来保存对方法调用返回的对象的引用,然后根据需要多次使用该变量.
Node n = GetXmlNode();
String[] attributes = new String[n.getAttributes().getLength()];
for (int x = 0; x < n.getAttributes().getLength(); x++)
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
但是,由于C#属性只是一个getter方法和一个封装在一个类型成员中的setter方法,它是否遵循相同的规则应该遵守?
或者规则不适用于这种情况,因为假设在标准库中调用C#属性和Java get方法只返回对字段的引用而不是执行繁重的工作是"安全的"吗?
.net ×8
c# ×6
winforms ×2
.net-2.0 ×1
.net-4.0 ×1
add-in ×1
ado.net ×1
button ×1
c++ ×1
certificate ×1
default ×1
excel ×1
exception ×1
form-control ×1
forms ×1
httprequest ×1
intptr ×1
java ×1
loops ×1
methods ×1
ms-office ×1
pointers ×1
properties ×1
security ×1
thread-abort ×1