C#调用其他线程的控键做为控键的父控键
using System;using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication1
{
class Class1
{
private Panel l;
Label la;
public Class1(Panel a)
{
this.l = a;
}
public void add()
{
int x = 0, y = 25;
la = new Label();
Random rand = new Random();
int r = rand.Next(97, 123);
x = rand.Next(0, 100);
la.Text = ((char)r).ToString();
this.l.Controls.Add(la);//我想用在这个线程内使用主线程的panel控键,但是报错,说这个线程不能调用其他线程的控键作为该线程控键的父控键,那因该怎么改就对了???
la.Location = new Point(x, y);
while(true)
{
la.Location = new Point(x,y);
y++;
Thread.Sleep(500);
}
}
}
}
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 WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Class1 cc = new Class1(this.panel1);
new Thread(new ThreadStart(cc.add)).Start();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
}
}
} --------------------编程问答-------------------- CrossThread Exception
看这个
http://blog.csdn.net/LeoMaya/archive/2006/12/26/1463695.aspx
补充:.NET技术 , C#