Control..::.Invoke 메서드

method
형식: System..::.Delegate

args 매개 변수에 있는 매개 변수의 수 및 형식과 동일한 매개 변수를 갖는 메서드의 대리자입니다.

args
형식: array<System..::.Object>[]()[]

지정된 메서드에 인수로 전달하는 개체의 배열입니다. 메서드가 인수를 사용하지 않으면 이 매개 변수는 nullNothingnullptrNull 참조(Visual Basic의 경우 Nothing)이 될 수 있습니다.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

   public class MyFormControl : Form
   {
      public delegate void AddListItem(String myString);
      public AddListItem myDelegate;
      private Button myButton;
      private Thread myThread;
      private ListBox myListBox;
      public MyFormControl()
      {
         myButton = new Button();
         myListBox = new ListBox();
         myButton.Location = new Point(72, 160);
         myButton.Size = new Size(152, 32);
         myButton.TabIndex = 1;
         myButton.Text = "Add items in list box";
         myButton.Click += new EventHandler(Button_Click);
         myListBox.Location = new Point(48, 32);
         myListBox.Name = "myListBox";
         myListBox.Size = new Size(200, 95);
         myListBox.TabIndex = 2;
         ClientSize = new Size(292, 273);
         Controls.AddRange(new Control[] {myListBox,myButton});
         Text = " 'Control_Invoke' example ";
         myDelegate = new AddListItem(AddListItemMethod);
      }
      static void Main()
      {
         MyFormControl myForm = new MyFormControl();
         myForm.ShowDialog();
      }
      public void AddListItemMethod(String myString)
      {
            myListBox.Items.Add(myString);
      }
      private void Button_Click(object sender, EventArgs e)
      {
         myThread = new Thread(new ThreadStart(ThreadFunction));
         myThread.Start();
      }
      private void ThreadFunction()
      {
         MyThreadClass myThreadClassObject  = new MyThreadClass(this);
         myThreadClassObject.Run();
      }
   }
   public class MyThreadClass
   {
      MyFormControl myFormControl1;
      public MyThreadClass(MyFormControl myForm)
      {
         myFormControl1 = myForm;
      }
      String myString;

      public void Run()
      {


         for (int i = 1; i <= 5; i++)
         {
            myString = "Step number " + i.ToString() + " executed";
            Thread.Sleep(400);
            // Execute the specified delegate on the thread that owns
            // 'myFormControl1' control's underlying window handle with
            // the specified list of arguments.
            myFormControl1.Invoke(myFormControl1.myDelegate,
                                   new Object[] {myString});
         }
      }
   }

 

 

 

ProgressBar 업데이트 시키기

image

 

image

image

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
using System.Diagnostics;

//버튼클릭스 progressBar 를 업데이트 합니다.
namespace DevWindowForm
{

    //델리게이트 생성
    delegate void myDelegate(int i , int max);
  

    public partial class Form1 : Form
    {
        //스레드생성
        System.Threading.ThreadStart ts;
        System.Threading.Thread myThread;
        
        public Form1()
        {
            InitializeComponent();

          
        }

        //progressBar를 업데이트 합니다
        //덱스트박스에 값을 출력합니다.
        private void updateProgress(int _Value, int _Max)
        {
          
            this.progressBar1.Maximum = _Max;
            progressBar1.Value = _Value;
            this.textBoxComment.Text += _Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ts = new ThreadStart(UpdateNum);
            myThread = new Thread(ts);
            myThread.Start();
        }


        private void UpdateNum() {

            int t_i = 0;
            while (t_i < 1000) {
                try
                {
                    //델리게이트호출
                    this.Invoke(new myDelegate(updateProgress), new object[] { t_i, 1000 });

                    Thread.Sleep(10);
                    
                }
                catch (Exception ex)
                {
                    break;
                }               
            
            }
        
        }



    }
}

+ Recent posts