跨线程操作无效:控制"totalCost"是从其他线程(不是在其上创建的线程)访问的 [英] Cross-thread operation not valid: Control 'totalCost' accessed from a thread other than the thread it was created on

查看:91
本文介绍了跨线程操作无效:控制"totalCost"是从其他线程(不是在其上创建的线程)访问的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 OrderCost.localhost;
using System.Web;
using System.Web.Services;

namespace OrderCost
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //invoke web method asynchronously
        /*this method is the callback that will run when the HowMuchWillItCost
         * web method completes*/
        private void howMuchCallBack(IAsyncResult ar)
        {            
            try
            {
                Service1 prodService = (Service1)ar.AsyncState;                
                decimal totCost = prodService.EndHowMuchWillItCost(ar);                
                totalCost.Text = totCost.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error1 Getting Response: " + ex.Message,
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //create event method with button to invoke the Web method.
        private void howMuchButton_Click(object sender, EventArgs e)
        {
            
            Service1 prodService = new Service1();      
            
            try
            {
                AsyncCallback callBack = new AsyncCallback(howMuchCallBack);
                IAsyncResult running = prodService.BeginHowMuchWillItCost((productName.Text),
                    System.Convert.ToInt32(numberRequired.Text),callBack,prodService);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error2 messagen sending request: " + ex.Message,
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }       
    }
}



调试后,我发现错误消息是



After debugging I figure out the error message is

totalCost.Text = 'totalCost.Text' threw an exception of type 'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException' 

[System.InvalidOperationException] = {"Cross-thread operation not valid: Control 'totalCost' accessed from a thread other than the thread it was created on."}


请帮我解决这个问题.

谢谢,


Please help me fix this.

thanks,

推荐答案

您不能从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview扫描仪和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

-SA
You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


,您需要调用UI对象来执行此操作.如果您搜索此错误消息,将获得大量帮助.
you need to invoke the UI objects to do this. If you google this error message, you''ll find tons of help.


这篇关于跨线程操作无效:控制"totalCost"是从其他线程(不是在其上创建的线程)访问的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆