-2

My code is as :

main()
{
    label1.Visible=true;

    /* code that takes about 1-2 minutes to respond */

    label1.Visible=false;
}

Now i am unable to display & hide that label.

How do I display and hide the label?

jww
  • 97,681
  • 90
  • 411
  • 885
  • If you want to change a labels state while code is executing then you should consider multithreading. – Ryanas Sep 21 '14 at 15:46
  • The underlying cause is explained [here](http://stackoverflow.com/a/952964/60761). The `await` answer below is a better solution. – H H Sep 21 '14 at 15:53
  • I think you need to call `Invalidate` and then `Update`. See [Control.Invalidate Method](http://msdn.microsoft.com/en-us/library/598t492a%28v=vs.110%29.aspx). – jww Sep 21 '14 at 16:08
  • Just put `this.Refresh();` after hide/shod instuction – Mehdi Khademloo Sep 21 '14 at 16:11

1 Answers1

2

It's because all work is done in one thread which is your UI thread. Try performing the hard work in another thread asynchronously:

async void YourMethod()
{
    label1.Visible=true;
    await Task.Run(() => /* do the work */); 
    label1.Visible=false;
}

See Asynchronous Programming with Async and Await for more details.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • Note that YourMethod should be an eventhandler (`async void YourMethod(object sender, SomeEventArgs e)`) or you'll only have shifted the problem elsewhere. – H H Sep 21 '14 at 16:50