zoukankan      html  css  js  c++  java
  • 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);
                    



  • 相关阅读:
    [bzoj4942][noi2017]整数【线段树】
    面向对象的几个函数
    各种编码方式及其来历
    java nio中,HeapByteBuffer与DirectByteBuffer的区别
    volatile关键字的作用
    git常用命令
    关于git的一些零碎知识
    rm(操作系统的删除文件)与git rm的区别
    php的数组
    SQL Server中模式(schema)、数据库(database)、表(table)、用户(user)之间的关系
  • 原文地址:https://www.cnblogs.com/dennysong/p/5137653.html
Copyright © 2011-2022 走看看