使用C#2.0和MethodInvoker委托,我有一个GUI应用程序从GUI线程或工作线程接收一些事件.
我使用以下模式处理表单中的事件:
private void SomeEventHandler(object sender, EventArgs e)
{
MethodInvoker method = delegate
{
uiSomeTextBox.Text = "some text";
};
if (InvokeRequired)
BeginInvoke(method);
else
method.Invoke();
}
Run Code Online (Sandbox Code Playgroud)
通过使用这种模式,我不会复制实际的UI代码,但我不确定的是这种方法是否合适.
特别是这条线
method.Invoke()
Run Code Online (Sandbox Code Playgroud)
它是否使用另一个线程进行调用,或者它是否有点转换为直接调用GUI线程上的方法?
可以在编译时创建一个数组;
int[] myValues = new int[] { 1, 2, 3 } ;
Run Code Online (Sandbox Code Playgroud)
但我想做这样的事情;
List<int> myValues = new List<int>() { 1, 2, 3 };
Run Code Online (Sandbox Code Playgroud)
编译器说没有.有没有办法在不使用LINQ(C#3.0)的情况下执行此操作(C#2.0)?
假设按订阅顺序调用事件订阅者是否安全?
例:
void One(object sender, EventArgs e) {}
void Two(object sender, EventArgs e) {}
event EventHandler foo;
foo += One;
foo += Two;
Run Code Online (Sandbox Code Playgroud)
当事件被触发时,One()总是在Two()之前调用吗?
编辑:
你应该不依赖它,我只是想.这个想法是,多播代表与COMMAND模式类似.所以我只是想知道.通常你会使用一个保存COMMAND命令的集合,这样你就可以做undo/redo/whatever.
我有一个转发器,在转发器的每个ItemTemplate中都是一个带有OnCheckedChanged事件处理程序集的asp:复选框.复选框将AutoPostBack属性设置为true.选中任何复选框时,将触发事件处理程序.如果未选中任何内容,则不会触发事件处理程序.
知道为什么事件不会发生,以及我如何解雇它?谢谢.
简化的转发器代码:
<asp:Repeater ID="rptLinkedItems" runat="server">
<ItemTemplate>
<asp:CheckBox ID="chkLinked" runat="server"
Checked="false" OnCheckedChanged="chkLinked_CheckedChanged" />
</ItemTemplate>
</asp:Repeater>
Run Code Online (Sandbox Code Playgroud)
该集合与中继器绑定如下:
protected override void OnPreRenderComplete(EventArgs e)
{
if (!Page.IsPostBack)
{
m_linkedItems = GetLinkedItems();
rptLinkedItems.DataSource = GetLinkableItems();
rptLinkedItems.ItemDataBound += new RepeaterItemEventHandler
(rptLinkedItems_ItemDataBound);
rptLinkedItems.DataBind();
}
base.OnPreRenderComplete(e);
}
Run Code Online (Sandbox Code Playgroud)
OnItemDataBound事件处理程序如下:
private void rptLinkedItems_ItemDataBound(Object sender, RepeaterItemEventArgs args)
{
if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
{
CategoryItem item = args.Item.DataItem as CategoryItem;
Literal litItemName = args.Item.FindControl("litItemName") as Literal;
CheckBox chkLinked = args.Item.FindControl("chkLinked") as CheckBox;
litItemName.Text = item.Text;
chkLinked.Checked = …
Run Code Online (Sandbox Code Playgroud) 无论如何设置尚未初始化的对象上的静态(私有)变量的值?该SetValue
方法需要一个实例,但我希望有办法解决这个问题.
我有一套C#(v2)应用程序,我在Win7(以及较小程度上的Vista)中使用注册表虚拟化.
我有一个共享的注册表配置区域,我的应用程序需要在HKLM\Software\Company中访问...在Vista之前,所有内容都只是根据需要写入并从该位置读取.
代码适当地检测到写入该注册表项的失败并且会适当地回退(写入HKCU而不是通知用户他们应用的设置只会影响当前用户).
在Vista中,注册表虚拟化破坏了所有这一切,因为我们用于HKLM写入的访问检查将"成功"静默并虚拟化为HKCR\VirtualStore\Machine ...而不是.在这种情况下,用户会认为他们已经保存了机器范围的配置,而只是写入了虚拟商店.
遗憾的是,即使尝试枚举HKLM reg键的权限,也会显式返回指示用户是否访问的结果.
当我们添加Vista支持时,我们使用的解决方法是执行对HKLM的探测写入...然后在HKCR\VirtualStore\Machine ...中检查相同的值并注意如果找到该值则发生虚拟化.
Win7似乎已经打破了这个(再次),因为针对显式虚拟位置(HKCR)的查询现在显示来自HKLM位置的合并结果,即使写入未被虚拟化.
有没有人有任何解决这个问题的建议?
约束: - 我需要一个无需提升的解决方案(当我没有管理员级别权限时,我将回退到HKCU中的每用户配置,但我需要能够可靠地检测到这种情况).
它需要使用v2 C#应用程序(我看到的C++代码的一个选项是嵌入一个禁用.exe虚拟化的清单,但我无法在C#V2中执行此操作,请参阅Windows中的禁用文件夹虚拟化) .
它需要在没有"安装程序"的情况下工作(这排除了在注册表项上禁用虚拟化的能力,我们需要REG FLAGS ...命令).
我有一个ToolStripButton用作单选按钮.选中时,按钮周围会出现蓝色轮廓,但没有背景颜色.对于用户来说,检查按钮是不够清楚的,因此我想更改背景颜色以使检查状态更加明显.
当Checked属性设置为true时,如何更改高亮颜色?
这是一段代码:
this.hideInactiveVehiclesToolstripButton.CheckOnClick = true;
this.hideInactiveVehiclesToolstripButton.ForeColor = System.Drawing.Color.Blue;
this.hideInactiveVehiclesToolstripButton.AutoSize = false;
this.hideInactiveVehiclesToolstripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.hideInactiveVehiclesToolstripButton.Image = global::ClientUI.Properties.Resources.toggleInactive;
this.hideInactiveVehiclesToolstripButton.ImageTransparentColor = System.Drawing.Color.Black;
this.hideInactiveVehiclesToolstripButton.Name = "hideInactiveVehiclesToolstripButton";
this.hideInactiveVehiclesToolstripButton.Size = new System.Drawing.Size(48, 48);
this.hideInactiveVehiclesToolstripButton.Text = "Hide Inactive Vehicles";
this.hideInactiveVehiclesToolstripButton.Click +=new System.EventHandler(this.hideInactiveVehiclesToolstripButton_Click);
Run Code Online (Sandbox Code Playgroud) 我有一个C#应用程序,我收到此错误:
"由于先前的功能评估超时,功能评估被禁用.您必须继续执行以重新启用功能评估."
我在stackoverflow和msdn上看到了许多与此错误相关的帖子,但没有找到解决方案.大多数人都说这个错误来自多线程应用程序,可以通过删除所有断点来解决.在我的情况下,我的应用程序是单线程,我也删除了所有的断点,但我在调试应用程序时仍然收到此错误.当我运行应用程序时.没有调试,我的应用程序只是挂起,我必须通过visual studio停止它.我试图找到它挂起的代码,然后找到它挂起的行.这是代码片段:
MatchCollection matchesFound = Regex.Matches(content,
keywordPattern,
RegexOptions.Multiline);
int matchCount = matchesFound.Count;
Run Code Online (Sandbox Code Playgroud)
当执行到达第二行时,即当代码尝试获取Count属性的值时,我的应用程序将挂起.我的正则表达式很好,因为我在Expresso中测试了它,我确信应用程序在执行Matches()方法时没有被绞死.如果我通过调试来到那一行,我会得到上面提到的错误.有谁知道为什么会出现这个错误以及如何解决它?
我正在使用Visual Studio 2005.
我有一个.NET应用程序,抛出以下异常:
System.ComponentModel.Win32Exception : Not enough quota is available to process this command
at MS.Win32.UnsafeNativeMethods.PostMessage(HandleRef hwnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at MS.Win32.ManagedWndProcTracker.HookUpDefWindowProc(IntPtr hwnd)
at MS.Win32.ManagedWndProcTracker.OnAppDomainProcessExit()
at MS.Win32.ManagedWndProcTracker.ManagedWndProcTrackerShutDownListener.OnShutDown(Object target)
at MS.Internal.ShutDownListener.HandleShutDown(Object sender, EventArgs e)
Run Code Online (Sandbox Code Playgroud)
我无法亲自重现此异常,但我收到了很多来自用户的异常报告.
提到的"配额"是什么?堆栈跟踪让我相信它可能是Windows消息队列的问题.
任何关于可能导致此错误或如何修复错误的想法都将非常感激.
编辑,进一步信息: 这是在所有机器上的32位Windows XP上,并且异常不在我的代码中,而是某种.NET Framework事件处理程序.应用程序本身不会进行任何PostMessage调用.
使用C#2.0,我可以指定一个默认参数值,如下所示:
static void Test([DefaultParameterValueAttribute(null)] String x) {}
Run Code Online (Sandbox Code Playgroud)
由于此C#4.0语法不可用:
static void Test(String x = null) {}
Run Code Online (Sandbox Code Playgroud)
那么,值类型的C#2.0是否相同?例如:
static void Test(int? x = null) {}
Run Code Online (Sandbox Code Playgroud)
以下尝试无法编译.
// error CS1908: The type of the argument to the DefaultValue attribute must match the parameter type
static void Test([DefaultParameterValueAttribute(null)] int? x) {}
// error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression
static void Test([DefaultParameterValueAttribute(new Nullable<int>())] int? x) {}
Run Code Online (Sandbox Code Playgroud)