C# 这是在c中动态调用的最佳方式吗#

C# 这是在c中动态调用的最佳方式吗#,c#,C#,这是在c#中动态调用某些内容的最佳方式吗 基本上,我试图避免在不需要额外代码块的地方使用它们 还是使用同步上下文更好 // Something that might need to be invoked private void MightnInvoke() { // Invoke if we need to. if (this.InvokeRequired) this.Invoke(new Action(this.MightnInvoke));

这是在c#中动态调用某些内容的最佳方式吗

基本上,我试图避免在不需要额外代码块的地方使用它们

还是使用同步上下文更好

    // Something that might need to be invoked
    private void MightnInvoke()
    {
       // Invoke if we need to.
      if (this.InvokeRequired) this.Invoke(new Action(this.MightnInvoke));

        // Do stuff here.             

    }
更新:


回到这里,在分析线程并发性之后,我给出的同步上下文方法(作为示例)确实是错误的。不要无用地使用它,因为你打算稍微修改一下它,使之成为线程安全的
此类型的任何公共静态(在Visual Basic中共享)成员都是线程安全的。任何实例成员都不能保证线程安全。

这会让我相信您的实现可能不是线程安全的(尽管如果它正在工作,那么可能是我错误地解释了一些东西)

就我个人而言,这看起来有点冗长

我在生产中使用了以下方法,并且从来没有遇到过线程问题

 public void SyncContext(object state)
        {
            try
            {
                int id = Thread.CurrentThread.ManagedThreadId;
                Console.Writeline("Run thread: " + id);

                SynchronizationContext CommandContext = state as SynchronizationContext;

                   // Do stuff here and then use the CommandContext.
                    var Somestate = "Connected";
                    CommandContext.Send(Sometask, Somestate.ToString());              

                    Thread.Sleep(250);

            }
            catch (System.ComponentModel.InvalidAsynchronousStateException)
            {

            } 

  public void Sometask(object state)
        {
               // We can work in here and be on the same thread we came from.

            string Target = state as string;

            if (Target == "Connected")
            { }

        }
使用方法如下:

public delegate void ActionCallback();
public static void AsyncUpdate(this Control ctrl, ActionCallback action)
{
    if (ctrl != null && (ctrl.IsHandleCreated && !ctrl.IsDisposed && !ctrl.Disposing))
    {
        if (!ctrl.IsHandleCreated)
            ctrl.CreateControl();

        AsyncInvoke(ctrl, action);
    }
}

private static void AsyncInvoke(Control ctrl, ActionCallback action)
{
    if (ctrl.InvokeRequired)
        ctrl.BeginInvoke(action);
    else action();
}

在官方的MSDN文档中,它说
此类型的任何公共静态(在Visual Basic中共享)成员都是线程安全的。任何实例成员都不能保证线程安全。

这会让我相信您的实现可能不是线程安全的(尽管如果它正在工作,那么可能是我错误地解释了一些东西)

就我个人而言,这看起来有点冗长

我在生产中使用了以下方法,并且从来没有遇到过线程问题

 public void SyncContext(object state)
        {
            try
            {
                int id = Thread.CurrentThread.ManagedThreadId;
                Console.Writeline("Run thread: " + id);

                SynchronizationContext CommandContext = state as SynchronizationContext;

                   // Do stuff here and then use the CommandContext.
                    var Somestate = "Connected";
                    CommandContext.Send(Sometask, Somestate.ToString());              

                    Thread.Sleep(250);

            }
            catch (System.ComponentModel.InvalidAsynchronousStateException)
            {

            } 

  public void Sometask(object state)
        {
               // We can work in here and be on the same thread we came from.

            string Target = state as string;

            if (Target == "Connected")
            { }

        }
使用方法如下:

public delegate void ActionCallback();
public static void AsyncUpdate(this Control ctrl, ActionCallback action)
{
    if (ctrl != null && (ctrl.IsHandleCreated && !ctrl.IsDisposed && !ctrl.Disposing))
    {
        if (!ctrl.IsHandleCreated)
            ctrl.CreateControl();

        AsyncInvoke(ctrl, action);
    }
}

private static void AsyncInvoke(Control ctrl, ActionCallback action)
{
    if (ctrl.InvokeRequired)
        ctrl.BeginInvoke(action);
    else action();
}

put
this.Invoke(新操作(this.MightnInvoke))放在括号内,或者去掉上面代码片段中的花括号。你到底在说什么?语法是正确的。是的,它会编译,但大括号没有任何意义。大括号主要是为了美观,但我会删除它们。方法是正确的。你查过了吗?这至少可以防止代码重复,并实现一个标准模式,用于在UI threadput
this.Invoke(新操作(this.MightnInvoke))上调用方法放在括号内,或者去掉上面代码片段中的花括号。你到底在说什么?语法是正确的。是的,它会编译,但大括号没有任何意义。大括号主要是为了美观,但我会删除它们。方法是正确的。你查过了吗?如果您在“退出”并忽略它时捕获
System.ComponentModel.InvalidAsynchronousStateException
,或者运行Environment.Exit(70),至少可以防止代码重复,并实现在UI线程上调用方法的标准模式;在退出方法的末尾,它工作得很好。您的方法效率更高,谢谢:)是的,即使捕获和忽略确实有效,这也是一个很好的迹象,表明实现不太正确。正如您所提到的,调用似乎更有效,这似乎是我在SynchronizationContext上找到的文章的普遍共识。如果您在“退出”时捕获
System.ComponentModel.InvalidAsynchronousStateException
,并忽略它,或者运行Environment.Exit(70);在退出方法的末尾,它工作得很好。您的方法效率更高,谢谢:)是的,即使捕获和忽略确实有效,这也是一个很好的迹象,表明实现不太正确。正如您所提到的,调用SynchronizationContext似乎更有效,这似乎是我在SynchronizationContext上找到的文章的普遍共识。