(我有解决这个问题的方法,但这不是我第一次被咬,所以我试图确切地了解发生了什么。)
ShowDialog一张表格。Pushedthen )ReleasedPushed,它会调用form.Hide()Released,它会更改按钮的外观。发生的情况是,有时(但并非每次)非 Gui 线程在尝试发送Released. 无一例外,Gui 继续“工作”,但无论在哪个方向,都不可能与非 Gui 线程进行进一步的通信。
线程的(简化的)调用堆栈如下所示:
System.Threading.WaitHandle.WaitOne()
(...)
System.Windows.Forms.Control.WaitForWaitHandle()
(...)
System.Windows.Forms.Control.Invoke()
(...)
GuiCode.OnStatusChanged()
(...)
NonGuiCode.SetStatus()
Run Code Online (Sandbox Code Playgroud)
ShowDialog如果我替换为,问题就会消失,但是 - 有趣的是 - 它会变得更好(发生的频率较低),但如果我注释掉执行onShow的代码,问题不会完全消失。HidePushed
更新
感谢nobugz,我发现了死锁(我以前只在数据库中遇到过它)!显然,用 Control.BeginInvoke 替换 Control.Invoke 可以解决此问题(状态事件有时仍然“卡住”,但它不会阻止所有后续通信)。
我正在研究可扩展列表视图。我已经在子行的 imageview 上设置了 onClickHandler,因为内联点击列表在这种情况下不起作用。但我需要 int groupPosition 和 childPosition 来在我的程序中进行一些处理。这些可以从 OnChildClick() 获得。
问题是我需要 OnclickHandler 和 OnChildClick() (对于 groupPosition 和 ChildPosition)。有没有办法获得这两个值?我们可以手动调用(调用)OnChildClick 方法吗?
public class ExpList extends ExpandableListActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
getExpandableListView().setOnChildClickListener(new OnChildClickListener()
{
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id)
{
// some calculations here....
return false;
}
});
}
public void onClickHandler (View v)
{
switch (v.getId()) {
case R.id.imgcall:
//Here I …Run Code Online (Sandbox Code Playgroud) 假设我想在 java 中动态加载一个类并调用它的start()(没有参数)方法:
Class<?> c = Class.forName("AbuseMe");
c.getMethod("start").invoke(c.newInstance());
Run Code Online (Sandbox Code Playgroud)
这是一个好的/安全的方法吗?
我正在创建一个网站,在安全方面我非常强迫症,所以我听说如果您将所有 .php 文件存储在 public_html 文件夹之外,并使用 public_html 文件夹内的另一个 .php 文件调用它们,那么您的风险攻击力较低。这是真的吗,如果是的话我该怎么做。我读过一些有关使用 .htaccess 的内容,但我不确定这是否是正确的方法。我虽然我可以使用 include 但我不确定 include 如何与参数一起使用。
我对 Java (6/7/8) 语法有一个愚蠢的问题 - 这两个方法调用片段总是等效的吗?
和this
this.myMethod(4);
Run Code Online (Sandbox Code Playgroud)没有this
myMethod(4);
Run Code Online (Sandbox Code Playgroud)注意:当然,问题是关于参数的每个数字、类型和组合
较弱的语句:给定程序P,我可以仅通过在每个方法调用前面P'删除来创建程序吗?this.
我考虑了本地类、匿名类、内部类和各种继承,但没有发现任何矛盾。所以我相信这两个片段实际上是相同的。不幸的是我无法找到任何合适的证明(例如来自官方语法)。
你能通过反证证明我错了,或者给我一些构建等价证明的线索吗?多谢。
编辑:等价被证明是错误的(见下面的评论)那较弱的陈述呢?
Winforms、C#、VS2010。
我有一个在我的应用程序的生命周期内运行的轮询线程。
有时它会在我的主窗体上调用一个事件。我已经很多年没有碰过代码了,它运行成功,但现在我需要在参数列表中添加一个“out”参数。我在网上搜索过,但我发现的所有线程都与反射有关,并且尝试转换为我的上下文很复杂。我的不使用反射。
有人可以帮忙解决这个问题吗?在反射线程上,我读到人们似乎检查一些对象数组以获取输出参数结果,我在代码中没有使用它,而且我也不知道从哪里获取它。
private bool OnNeedUpdateCreateEvent(string title, string message,
bool creatingNew, out string newPlanName)
{
newPlanName = "";
// 1st pass through this function.
// Check to see if this is being called from another thread rather
// than the main thread. If so then invoke is required
if (InvokeRequired)
{
// Invoke and recall this method.
return (bool)Invoke(new onNeedToUpdatePlanEvent(OnNeedUpdateCreateEvent),
title, message, creatingNew, out newPlanName); <- wrong out param
}
else
{
// 2nd pass through this function due …Run Code Online (Sandbox Code Playgroud) 我要打印 hi GrandFather
但它似乎打印喜父亲。我不明白如何区分findSpecial和findVirtual
我想要有人可以帮助我。谢谢
class GrandFather{
void thinking(){
System.out.println("hi GrandFather");
}
}
class Father extends GrandFather{
void thinking(){
System.out.println("hi Father");
}
}
class Son extends Father{
void thinking(){
MethodType mt=MethodType.methodType(void.class);
//MethodHandle mh=MethodHandles.lookup().findVirtual(GrandFather.class,"thinking",mt).bindTo(this);
MethodHandle mh;
mh = MethodHandles.lookup().findSpecial(GrandFather.class,"thinking",mt,getClass());
mh.invoke(this);
}
}
public static void main(String[] args){
(new MethodHandleTest().new Son()).thinking();
}
Run Code Online (Sandbox Code Playgroud)

下面是一个相当可怕的模式,我有时会用它来做一个简单的调用.这段代码让我感到有点内疚,即使我不确定为什么.这可怕吗?合理?以后会在我脸上炸掉?
public void myMethod(object args)
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(delegate
{
myMethod(args);
}));
return;
}
//Do Stuff
}
Run Code Online (Sandbox Code Playgroud) 我继承了一些代码,它有两个非UI线程来更新各种WinForm控件.
代码使用InvokeRequired和Invoke来更新UI; 但是,我仍然偶尔得到错误:跨线程操作无效:控制'lvReports'访问除了它之外的其他线程.
我怀疑我正在处理一个竞争条件,我需要在下面的方法中引入一个锁,但是说,我可以找到几十个关于如何安全地从非UI线程更新UI但没有示例或讨论的示例如何处理在竞赛场景中更新相同控件的两个线程.
所以我的问题是:如何在给定竞争条件并且我需要从非UI线程更新UI时,如何重写下面的代码来处理更新UI?
// two separate theads call this method in a instance of a WinForm
private void LoadReports()
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(this.LoadReports));
}
else
{
// some code removed to keep exampe simple...
SetCtlVisible(lvReports, true);
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate { lvReports.Refresh(); });
}
else
{
lvReports.Refresh();
}
}
}
delegate void SetVisibleCallback(Control ctl, bool visible);
private void SetCtlVisible(Control ctl, bool visible)
{
if (ctl.InvokeRequired)
{
SetVisibleCallback d = new SetVisibleCallback(SetCtlVisible);
ctl.Invoke(d, new object[] { ctl, …Run Code Online (Sandbox Code Playgroud) 我有2个DLL,A.dll包含:
namespace Alphabet
{
public delegate void TestHandler();
public class A
{
private void DoTest()
{
Type type = Assembly.LoadFile("B.dll").GetType("Alphabet.B");
Object o = Activator.CreateInstance(type);
string ret = type.InvokeMember("Hello", BindingFlags.InvokeMethod | BindingFlags.Default, null, o, null);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和B.dll包含
namespace Alphabet
{
public class B
{
public event TestHandler Test();
public string Hello()
{
if (null != Test) Test();
return "Hello";
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用InvokeMember从B.dll获取结果,我也想Test()在返回结果之前使用B.dll .那么,我如何通过反射delegate将其连接event到B.dll?
任何帮助将不胜感激!