线程间操作无效: 从不是创建控件“pbarc”的线程访问它 解决办法
线程间操作无效: 从不是创建控件“pbarc”的线程访问它 解决方法
public partial class Form5 : Form
{
public Thread PBarThread;
private delegate void MyTestDelegate(object obj);
private MyTestDelegate myTest;
/// <summary>
/// dev进度条
/// </summary>
private DevExpress.XtraEditors.ProgressBarControl pBarc;
public Form5()
{
InitializeComponent();
}
private void Form5_Load(object sender, EventArgs e)
{
pBarc.Properties.Minimum = 0;//设置进度条最小值
pBarc.Properties.Maximum = 10;//设置进度条最大值
pBarc.Properties.Step = 1;//设置进度条步长
pBarc.Properties.ProgressViewStyle = DevExpress.XtraEditors.Controls.ProgressViewStyle.Broken;//设置进度条样式
pBarc.Position = 0;//设置进度条开始位置
}
private void button1_Click(object sender, EventArgs e)
{
PBarThread = new Thread(new ParameterizedThreadStart(fun));
PBarThread.Start();
}
/// <summary>
/// 线程执行的事件 在子线程中为代理赋值,并执行代理 防止线程跨界访问错误
/// </summary>
/// <param name="obj"></param>
private void fun(object obj)
{
if (this.InvokeRequired)
{
myTest += fun;
this.BeginInvoke(myTest, new object[] { obj });
}
else
{
pBarc.PerformStep();//走进度条
}
}
}