shaidirendai 2014-10-04 03:05
浏览 965

提示“Label1不是从创建它的线程访问”,怎么修改,请问?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace CallBackEx
{
public partial class Form1 : Form
{
public delegate void ExampleCallback(int lineCount,Label lb);
public Form1()
{
InitializeComponent();
}
public void CurrentNumber(int tempNumber, Label lb)
{
lb.Text = tempNumber.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
ThreadWithData twd = new ThreadWithData(1, 100, this.label1, new ExampleCallback(CurrentNumber));
Thread td = new Thread(new ThreadStart(twd.RunMethod));
td.Start();
}

    private void button2_Click(object sender, EventArgs e)
    {
        ThreadWithData twd = new ThreadWithData(2, 200, this.label2, new ExampleCallback(CurrentNumber));
        Thread td = new Thread(new ThreadStart(twd.RunMethod));
        td.Start();
    }
    public class ThreadWithData
    {
        private int start=0;
        private int end=0;
        private ExampleCallback callback;
        private Label lb;

        public ThreadWithData(int start,int end,Label lb,ExampleCallback callback)
        {
            this.start = start;
            this.end = end;
            this.callback = callback;
            this.lb = lb;
        }

        public void RunMethod()
        {

            for (int i = start; i < end; i++)
            {
                Thread.Sleep(500);
                if (callback != null)
                {
                    callback(i,lb);
                }
            }
        }

    }
}

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 路易威登官网 里边的参数逆向
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
    • ¥50 需求一个up主付费课程
    • ¥20 模型在y分布之外的数据上预测能力不好如何解决
    • ¥15 processing提取音乐节奏
    • ¥15 gg加速器加速游戏时,提示不是x86架构