none
怎样才能不使用Control.Update自动更新界面

    问题

  • 代码如下:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Threading;
    
    namespace CS_Invoke_test
    {
        public class MyFormControl : Form
        {
            public delegate void AddListItem();
            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 myItem;
                for (int i = 1; i < 6; i++)
                {
                    myItem = "MyListItem" + i.ToString();
                    myListBox.Items.Add(myItem);
                    myListBox.Update();
                    Thread.Sleep(300);
                }
            }
            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;
            }
    
            public void Run()
            {
                myFormControl1.Invoke(myFormControl1.myDelegate);
            }
        }
    }

    在AddListItemMethod方法里,能不能不使用Control.Update来更新界面

    谢谢

    2016年6月6日 7:23

答案

  • Hi,

    Application.DoEvent() 也可用来更新界面UI的:

            public void AddListItemMethod()
            {
                String myItem;
                for (int i = 1; i < 6; i++)
                {
                    myItem = "MyListItem" + i.ToString();
                    myListBox.Items.Add(myItem);
                    Application.DoEvent();//用这个替换
                    Thread.Sleep(300);
                }
            }

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    2016年6月7日 7:35

全部回复

  •  myListBox.BeginUpdate();

     for (int i = 1; i < 6; i++)
     
    {
          myItem
    = "MyListItem" + i.ToString();
           myListBox
    .Items.Add(myItem);
           //myListBox
    .Update();
           
    Thread.Sleep(300);
    }

     myListBox.EndUpdate();


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2016年6月6日 7:38
  • Hi,

    Application.DoEvent() 也可用来更新界面UI的:

            public void AddListItemMethod()
            {
                String myItem;
                for (int i = 1; i < 6; i++)
                {
                    myItem = "MyListItem" + i.ToString();
                    myListBox.Items.Add(myItem);
                    Application.DoEvent();//用这个替换
                    Thread.Sleep(300);
                }
            }

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    2016年6月7日 7:35