用thread(工作线程)更新主界面的progressBar主界面设置progressBar的函数:
public void SetProgress(int min,int max,int val)
{
if(this.InvokeRequired)
{
this.Invoke(new SetProgressHandler(SetProgress),new object[]{min,max,val});
}
else
{
this.progressBar.Minimum = min;
this.progressBar.Maximum = max;
this.progressBar.Value = val;
}
}问题是,thread退出之前还会调用一次SetProgress()
然后this.Invoke(new SetProgressHandler(SetProgress),new object[]{min,max,val});
就会报错“堆栈溢出”。跟踪了一下,发现那时候thread已经退出了。
应该怎么解决呢

解决方案 »

  1.   

    而且很奇怪。工作线程thread里
    try{}
    catch{}
    SetProgress();只有当捕捉到错误了才会出现堆栈溢出。否则就没问题
      

  2.   

    You should check if the main thread is gone and only update if the main thread is still running.  You can record the main thread's handle in a member variable: Thread mainThread = Thread.CurrentThread.  And in the worker thread, check for mainThread.IsAlive before calling this.Invoke.
      

  3.   

    晕。好像是因为 SetProgress()在不停的自己调自己,最后溢出了
      

  4.   

    this.InvokeRequired 一直是true,为什么为什么为什么。郁闷了
      

  5.   

    Knight94(愚翁) 是说再写个方法?SetProgress()里不判断InvokeRequired而直接用invoke调那个方法?我明天试试看。不过还是觉得这样写比较帅