跨线错误 [英] Cross thread error

查看:67
本文介绍了跨线错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何避免错误<< cross thread =">> ,我已经很多时间遇到了这个问题:(

How can I refrain error <<cross thread="">> ,I''ve encountered this problem a lot of time :(

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

        private void button1_Click(object sender, EventArgs e)
        {

            backgroundWorker1.RunWorkerAsync();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {


            s:
            if (button1.Left != 0)
            {
    button1.Left--; //Cross-thread operation not valid ! why it happend
                Thread.Sleep(20);
            }
            else
            {
                button1.Left++;
                Thread.Sleep(20);
                if (button1.Left == 250)
                    goto s;


            }

        }


    }



请帮助我兄弟. :doh:
这些类型的问题的解决方案是什么?...



please help me bros. :doh:
what''s the solution of these type of problems...?

推荐答案

您无法从不同于UI自己线程的线程更新用户界面.如果要更新UI,请创建可由后台工作人员更新的属性,然后定期检查以查看该属性是否已通过使用计时器等进行了更新,然后根据该属性更改UI.另一件事,请在任何情况下都不要使用GOTO,尤其是在多线程中.
应用程序,永远!
试试这个,

You cannot update your user interface from a different thread than the UI''s own thread. If you want to update the UI, then create a property which can be updated by the backgroundworker, then periodically check to see if that property has been updated by using a timer or some such, and then change the UI according to that property. One other thing, please do not use GOTO in any situation, but especially in multi thread
applications, EVER!
Try this,

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.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private int intLeft = 0;

        public int MoveLeft
        {
            get { return intLeft; }
            set { intLeft = value; }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
            timer1.Start();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i <= 50; i++)
            {
                MoveLeft = i;
                Thread.Sleep(200);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            SetUI();
        }

        private void SetUI()
        {
            button1.Left = MoveLeft;
        }

        private void BackgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
       {
           timer1.Stop();
           timer1.Dispose();
       }

    }


您只能从创建它们的线程访问GUI元素(即控件,表单等).在您的情况下,"button1"是一个GUI元素,因此BackgroundWorker线程无法直接访问它而不会引发异常.

使用
You can only access GUI elemnets (i.e. controls, forms, etc from the thread that created them. In your case, "button1" is a GUI element, so the BackgroundWorker thread cannot access it directly without throwing an exception.

Use
button1.Invoke(delegate {button1.Left++; });



另外,最好将这些代码包装在



Additionally it is a good idea to wrap such code in

if (button1.InvokeRequired)
   {
   ...
   }

中作为检查.


您不能与后台工作线程中的UI元素进行交互.您可以发送将在UI线程上触发并在此处更改UI的进度事件.有一种方法可以关闭此功能,但是最好还是使用它.
You cannot interact with UI elements in your background worker thread. You can send progress events which will fire on the UI thread and change your UI there. There is a way to turn this off, but you''re better off working with it.


这篇关于跨线错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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