ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – 带有out参数的control.invoke

2019-06-23 01:52:52  阅读:390  来源: 互联网

标签:c multithreading winforms invoke controls


Winforms,C#,VS2010.

我有一个轮询线程,运行我的应用程序的生命周期.

偶尔它会在我的主表单上调用一个事件.多年来我没有触及代码并且运行成功,但现在我需要在参数列表中添加“out”参数.我在网上搜索过,但我发现的所有主题都是关于反射的,并且试图转换为我的上下文很复杂.我不使用反射.

有人可以帮忙解决这个问题吗?在我读过的反射线程中,人们似乎检查了一些对象数组的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 to invoke, or invoke not required
      return InputDlg(this, title, message, creatingNew, out newPlanName);
    }

} 

解决方法:

这很像你已经知道的,你还没有找到阵列.它由编译器自动创建. Invoke method的签名是:

public object Invoke(
    Delegate method,
    params object[] args
)

params关键字使编译器自动创建数组.很好的语法糖,但它对你没有帮助.你必须自己做,像这样:

if (!creatingNew) {
    // Invoke and recall this method.
    object[] args = new object[] { title, message, creatingNew, null };
    var retval = (bool)Invoke(new onNeedToUpdatePlanEvent(OnNeedUpdateCreateEvent), args);
    newPlanName = (string)args[3];
    return retval;
}
// etc..

标签:c,multithreading,winforms,invoke,controls
来源: https://codeday.me/bug/20190623/1266237.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有