csharp中的线程问题 [英] Threading issue in csharp

查看:135
本文介绍了csharp中的线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文件复制程序,其中启动按钮启动线程,取消按钮导致取消操作。

I have file copy program in which start button cause to start the thread and cancel button causes cancel operation.

 public class Form
{
private bool bCancelFlag = false;
onStartCopying()
{
  if(btnText.equals("Start"))
  {
      bCancelFlag = false;
      btnText.Text = "cancel";
      Thread th = new Thread(new ThreadStart(startThread));
      th.start();
  }
  if(btnText.equals("cancel"))
  {
    bCancelFlag = true;
    btnText.Text = "Start";
  }
}
startThread()
{
  while(fileLength)
  {
    value = value + 1;
    reportProgress(value);
    if(bCancelflag)
    {
      break;
    }
  }
 reportProgress(int value)
 {
   progressBar1.BeginInvoke((Action)(() =>
   {
        progressBar1.Value = value;
    }));
 }
}
}



当出现取消标签时,我们点击它会导致标签开始并再次点击开始标签原因取消标签和应用程序被绞死。那边没有任何事情发生。

什么是问题。它与线程启动和停止有关吗?


When Cancel label appear , we click on it causes label to start and again click on start label causes cancel label and app get hanged.Nothing happen over there.
what is problem.Is it related to thread start and stop??

推荐答案

您错过了 else

You missed and else:
if(btnText.equals("Start"))
 {
   // ...
 }
 else if (btnText.equals("cancel"))
 {
   // ...
 }


这篇关于csharp中的线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆