• Invoke


    Control.Invoke

    The delegate can be an instance of EventHandler, in which case the sender parameter will contain this control, and the event parameter will contain EventArgs.Empty. The delegate can also be an instance of MethodInvoker, or any other delegate that takes a void parameter list. A call to an EventHandler or MethodInvoker delegate will be faster than a call to another type of delegate.

    写法1

    Control.BeginInvoke(new Action(DoSomething), null);
    
    private void DoSomething()
    {
        MessageBox.Show("What a great post");
    }
    OR
    Control.BeginInvoke(new Action(() => MessageBox.Show("What a great post")));

    写法2

    Control.BeginInvoke((MethodInvoker) delegate { 
        MessageBox.Show("What a great post");
    });

    写法3

    public static class FormsExt
    {
        public static void InvokeOnMainThread(this System.Windows.Forms.Control control, Action act)
        {
            control.Invoke(new MethodInvoker(act), null);
        }
    }
    then
    rtbOutput.InvokeOnMainThread(() =>
    {
        // Code to run on main thread here
        rtbOutput.AppendText(fields[0].TrimStart().TrimEnd().ToString() + " Profile not removed.  Check Logs.
    "); }));
    });

    写法4 

    //样例:显示时间
    public
    Form1() { // Create a timer that will call the ShowTime method every second. var timer = new System.Threading.Timer(ShowTime, null, 0, 1000); } private void ShowTime(object x) { // Don't do anything if the form's handle hasn't been created // or the form has been disposed. if (!this.IsHandleCreated && !this.IsDisposed) return; // Invoke an anonymous method on the thread of the form. this.Invoke((MethodInvoker) delegate { // Show the current time in the form's title bar. this.Text = DateTime.Now.ToLongTimeString(); }); }
    this.Invoke((MethodInvoker)(() => 
                {
                   //some action 
                }));
                        
                        
    this.Invoke(new Action(() =>
                {
                    //some action                        
                }));
                
    //
    // 摘要:
    //     Executes the specified delegate on the thread that owns the control's underlying
    //     window handle.
    //
    // 参数:
    //   method:
    //     A delegate that contains a method to be called in the control's thread context.
    //
    // 返回结果:
    //     The return value from the delegate being invoked, or null if the delegate has
    //     no return value.
    public object Invoke(Delegate method);
                        
    //获取当前进程对象
    Process curProcess = Process.GetCurrentProcess();                
    PerformanceCounter count = new PerformanceCounter("Process", "Thread Count", curProcess.ProcessName);
    
    //
    // 摘要:
    //     Initializes a new, read-only instance of the System.Diagnostics.PerformanceCounter
    //     class and associates it with the specified system or custom performance counter
    //     and category instance on the local computer.
    //
    // 参数:
    //   categoryName:
    //     The name of the performance counter category (performance object) with which
    //     this performance counter is associated.
    //
    //   counterName:
    //     The name of the performance counter.
    //
    //   instanceName:
    //     The name of the performance counter category instance, or an empty string (""),
    //     if the category contains a single instance.
    //
    // 异常:
    //   T:System.InvalidOperationException:
    //     categoryName is an empty string ("").-or- counterName is an empty string ("").-or-
    //     The category specified is not valid. -or-The category specified is marked as
    //     multi-instance and requires the performance counter to be created with an instance
    //     name.-or-instanceName is longer than 127 characters.-or-categoryName and counterName
    //     have been localized into different languages.
    //
    //   T:System.ArgumentNullException:
    //     categoryName or counterName is null.
    //
    //   T:System.ComponentModel.Win32Exception:
    //     An error occurred when accessing a system API.
    //
    //   T:System.PlatformNotSupportedException:
    //     The platform is Windows 98 or Windows Millennium Edition (Me), which does not
    //     support performance counters.
    //
    //   T:System.UnauthorizedAccessException:
    //     Code that is executing without administrative privileges attempted to read a
    //     performance counter.
    public PerformanceCounter(string categoryName, string counterName, string instanceName);
                    



  • 相关阅读:
    网站安全配置Nginx防止网站被攻击
    JAVA程序打包方法-挺好
    Kettle6.1连接MongoDB报错
    基于Rancher搭建Kubernetes
    Tomcat性能调优实战
    const int * 和 int * const 傻傻分不清楚
    洛谷P1028数的计算
    【转】sizeof()用法总结
    百练4103:踩方格(DFS)
    百练2815:城堡问题(DFS)
  • 原文地址:https://www.cnblogs.com/dennysong/p/5137653.html
Copyright © 2020-2023  润新知