Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have file copy program in which start button cause to start the thread and cancel button causes cancel operation.
C#
 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??
Posted
Updated 14-May-15 21:38pm
v2
Comments
Sergey Alexandrovich Kryukov 15-May-15 2:51am    
Did you use the debugger? The problem looks simple enough.
Comparison with button text is absolutely dirty.
bCancelFlag declaration is not shown. This cannot be your real code. You need to paste exact code.
So, please use "Improve question".
—SA
Sarita S 15-May-15 3:51am    
yes I used debugger.Everything is going fine.Only App is get hanged on cancel to start operation
Harshad Kathiriya 15-May-15 5:26am    
It may be cause an issue related to Start and Stop Thread.
As per your code, you have started the thread but not stopped it.

revise your code.
Sarita S 15-May-15 5:41am    
After coming out of while thread get stopped. or on Cancel button click It get out of while loop causes to end thread.

1 solution

You missed and else:
C#
if(btnText.equals("Start"))
 {
   // ...
 }
 else if (btnText.equals("cancel"))
 {
   // ...
 }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-May-15 3:19am    
It would not fix anything, isn't it obvious? The whole "technique" is quite dirty.
—SA
CPallini 15-May-15 3:32am    
I don't comment about the technique. Anyway, missing the else is just a programming error.
Sarita S 15-May-15 3:38am    
Program dont have else part.
CPallini 15-May-15 3:45am    
It should have. Suppose button text is "Start", then the first if-block is executed and button text becomes "cancel", this causes (in your code) immediate (unintended, I hope) execution of the second if-block.
Sergey Alexandrovich Kryukov 15-May-15 8:58am    
Of course. I did not look at assignment to "cancel" in first part. 5ed.
But anyway, this will be too dirty to be correct. :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900