实在是搞不定了,我需要在button1事件中执行数据查询,同时需要循环显示进度条,并且当数据查询完成之后停止进度条并隐藏;
由于这个需要考虑多线程与委托的方法,自己弄了很久,但始终达不到效果(现在程序运行只看到进度条循环显示,就是数据没有查询出来),请大家帮帮忙:private void button1_Click(object sender, EventArgs e)
{
try
{
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
proBarThread = new Thread(new ThreadStart(SetToolStripProgressBar));
proBarThread.Start();
//开启进度条线程
reportThread = new Thread(new ThreadStart(DataSetTabPage));
reportThread.IsBackground = true;//后台线程
reportThread.Start();
//开启数据查询线程
timer1.Enabled = true;
}
catch (Exception ex)
{
this.Cursor = System.Windows.Forms.Cursors.Default;
MessageBox.Show(ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}private void SetToolStripProgressBar()
{
// 判断是否需要Invoke,多线程时需要
if (this.InvokeRequired)
{
// 通过委托调用写主线程控件的程序,传递参数放在object数组中
this.Invoke(new ThreadMethod(SetToolStripProgressBarDelegate), new object[] { });
}
else
{
SetToolStripProgressBarDelegate();
}
}private void DataSetTabPage()
{
// 判断是否需要Invoke,多线程时需要
if (this.InvokeRequired)
{
// 通过委托调用写主线程控件的程序,传递参数放在object数组中
this.Invoke(new ThreadMethod(DataSetTabPageDelegate), new object[] { });
}
else
{
DataSetTabPageDelegate();
}
}
/// <summary>
/// 执行数据查询相关动作
/// </summary>
private void DataSetTabPageDelegate()
{
try
{
……//这里有很多代码,有涉及到对UI界面控件的很多操作;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
{
/// <summary>
/// 设置进度条显示效果
/// </summary>
private void SetToolStripProgressBarDelegate()
{
while (toolStripProgressBar1.Value < 100)
{
if (toolStripProgressBar1.Value == 99)
{
toolStripProgressBar1.Value = 0;
}
Thread.Sleep(50);//执行延时
toolStripProgressBar1.Value++;
}
}
//判断线程结束后的动作
private void timer1_Tick(object sender, EventArgs e)
{
if (!reportThread.IsAlive) //线程结束
{
reportThread.Abort();
reportThread.Join();
reportThread = null;
//
proBarThread.Abort();
proBarThread.Join();
proBarThread = null;
//
toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
Thread.Sleep(500);
toolStripProgressBar1.Visible = false;
//
this.Cursor = System.Windows.Forms.Cursors.Default;
timer1.Enabled = false;
}
}
由于这个需要考虑多线程与委托的方法,自己弄了很久,但始终达不到效果(现在程序运行只看到进度条循环显示,就是数据没有查询出来),请大家帮帮忙:private void button1_Click(object sender, EventArgs e)
{
try
{
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
proBarThread = new Thread(new ThreadStart(SetToolStripProgressBar));
proBarThread.Start();
//开启进度条线程
reportThread = new Thread(new ThreadStart(DataSetTabPage));
reportThread.IsBackground = true;//后台线程
reportThread.Start();
//开启数据查询线程
timer1.Enabled = true;
}
catch (Exception ex)
{
this.Cursor = System.Windows.Forms.Cursors.Default;
MessageBox.Show(ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}private void SetToolStripProgressBar()
{
// 判断是否需要Invoke,多线程时需要
if (this.InvokeRequired)
{
// 通过委托调用写主线程控件的程序,传递参数放在object数组中
this.Invoke(new ThreadMethod(SetToolStripProgressBarDelegate), new object[] { });
}
else
{
SetToolStripProgressBarDelegate();
}
}private void DataSetTabPage()
{
// 判断是否需要Invoke,多线程时需要
if (this.InvokeRequired)
{
// 通过委托调用写主线程控件的程序,传递参数放在object数组中
this.Invoke(new ThreadMethod(DataSetTabPageDelegate), new object[] { });
}
else
{
DataSetTabPageDelegate();
}
}
/// <summary>
/// 执行数据查询相关动作
/// </summary>
private void DataSetTabPageDelegate()
{
try
{
……//这里有很多代码,有涉及到对UI界面控件的很多操作;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
{
/// <summary>
/// 设置进度条显示效果
/// </summary>
private void SetToolStripProgressBarDelegate()
{
while (toolStripProgressBar1.Value < 100)
{
if (toolStripProgressBar1.Value == 99)
{
toolStripProgressBar1.Value = 0;
}
Thread.Sleep(50);//执行延时
toolStripProgressBar1.Value++;
}
}
//判断线程结束后的动作
private void timer1_Tick(object sender, EventArgs e)
{
if (!reportThread.IsAlive) //线程结束
{
reportThread.Abort();
reportThread.Join();
reportThread = null;
//
proBarThread.Abort();
proBarThread.Join();
proBarThread = null;
//
toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
Thread.Sleep(500);
toolStripProgressBar1.Visible = false;
//
this.Cursor = System.Windows.Forms.Cursors.Default;
timer1.Enabled = false;
}
}
但是,在调试时运行会报错:
线程间操作无效: 从不是创建控件“tabControl1”的线程访问它。
线程间操作无效: 从不是创建控件“”的线程访问它。网上有说用这个方法:Control.CheckForIllegalCrossThreadCalls = false;但据说是不安全的,
可以用委托的方法实现,我上面也用了委托,但感觉用法不对,只能执行查询数据或显示进度条的其中一种方法,所以请大家帮忙修正一下。
private void SetToolStripProgressBar()
{
// 判断是否需要Invoke,多线程时需要
if (this.InvokeRequired)
{
// 通过委托调用写主线程控件的程序,传递参数放在object数组中
this.Invoke(new Action(SetToolStripProgressBar)); //Action 是Framework提供的
}
else
{
while (toolStripProgressBar1.Value < 100)
{
if (toolStripProgressBar1.Value == 99)
{
toolStripProgressBar1.Value = 0;
}
Thread.Sleep(50);//执行延时
toolStripProgressBar1.Value++;
}
}
}
这个地方 都要 invoke
建议你 从京沪线程跑进度条 次线程 跑数据 这样只需要开辟一个线程就好了
或者访问以下网址
http://www.cnblogs.com/rockniu/archive/2009/09/13/1565746.html
proBarThread.Start();
/// <summary>
/// 设置进度条显示效果
/// </summary>
private void SetToolStripProgressBarDelegate()
{
if (this.InvokeRequired)
{
ThreadMethod MyThread = new ThreadMethod(SetToolStripProgressBar);
this.Invoke(MyThread);
}
else
{
while (toolStripProgressBar1.Value < 100)
{
if (toolStripProgressBar1.Value == 99)
{
toolStripProgressBar1.Value = 0;
}
Thread.Sleep(50);//没什么意思,单纯的执行延时
toolStripProgressBar1.Value++;
}
}
}
proBarThread = new Thread(new ThreadStart(SetToolStripProgressBar));
proBarThread.Start();
调用()
{
SetTSPBDelegate delegate = new SetTSPBDelegate(SetToolStripProgressBarDelegate);
proBarThread = new Thread(new ThreadStart(proBarThread));
proBarThread.Start();
}delegate void SetTSPBDelegate();
/// <summary>
/// 设置进度条显示效果
/// </summary>
private void SetToolStripProgressBarDelegate()
{
if (this.InvokeRequired)
{
ThreadMethod MyThread = new ThreadMethod(SetToolStripProgressBar);
this.Invoke(MyThread);
}
else
{
while (toolStripProgressBar1.Value < 100)
{
if (toolStripProgressBar1.Value == 99)
{
toolStripProgressBar1.Value = 0;
}
Thread.Sleep(50);//没什么意思,单纯的执行延时
toolStripProgressBar1.Value++;
}
}
}
private void SetToolStripProgressBar()
{
while (toolStripProgressBar1.Value < 100)
{
if (toolStripProgressBar1.Value == 99)
{
toolStripProgressBar1.Value = 0;
}
Thread.Sleep(50);//没什么意思,单纯的执行延时
toolStripProgressBar1.Value++;
}
}
调用()
{
Thread proBarThread = new Thread(new ThreadStart(SetToolStripProgressBarDelegate));
proBarThread.Start();
}delegate void SetTSPBDelegate();
/// <summary>
/// 设置进度条显示效果
/// </summary>
private void SetToolStripProgressBarDelegate()
{
if (this.InvokeRequired)
{
SetTSPBDelegate Delegate = newSetTSPBDelegate(SetToolStripProgressBar);
this.Invoke(Delegate );
}
}
private void SetToolStripProgressBar()
{
while (toolStripProgressBar1.Value < 100)
{
if (toolStripProgressBar1.Value == 99)
{
toolStripProgressBar1.Value = 0;
}
Thread.Sleep(50);//没什么意思,单纯的执行延时
toolStripProgressBar1.Value++;
}
}