본문 바로가기
C#

Task에서 메서드 호출 시 invalidoperationexception

by 자바초보자 2022. 12. 8.
728x90
private void button1_Click(object sender, EventArgs e)
        {
            //equipList();
            Task task = new Task(taskTest);
            task.Start();
        }

        private void taskTestAction(object sender, EventArgs e)
        {
            /*
            Console.WriteLine("====" + _equipMap.Count);
            string comboTmp = comboBox1.SelectedItem.ToString();
            Console.WriteLine("comboTmp : " + comboTmp);
            string macAddr = getMacAddrByComboBox();
            Console.WriteLine("macAddr : " + macAddr);
            */

            equipList();
        }

        private void taskTest()
        {
            this.Invoke(new EventHandler(taskTestAction));
private void equipTimer_Tick(object sender, EventArgs e)
        {
            equipList();
            /*
            Task task = new Task(equipTimerTask);
            task.Start();
            */


            /*
            var threadParameters = new System.Threading.ThreadStart(delegate { equipTimerInvoke(); });
            var thread2 = new System.Threading.Thread(threadParameters);
            thread2.Start();
            */
            
        }

        private void equipTimerInvoke()
        {
            if (comboBox1.InvokeRequired)
            {
                // Call this same method but append THREAD2 to the text
                Action safeWrite = delegate { equipTimerInvoke(); };
                comboBox1.Invoke(safeWrite);
            }
            else
            {
                equipList();
            }
        }
728x90

'C#' 카테고리의 다른 글

tcpclient  (0) 2023.01.12
비동기 메서드 호출 textbox 크로스쓰레드 에러시  (0) 2022.12.13