Click here to Skip to main content
11,643,493 members (80,191 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0
How to enable and disable a button inside of a Thread

i am created a long process inside of a thread, at the same time i want to disable process start button


using this code some errors found
_________________________________________________________________
private void process()
{
btnCreate.Enabled = false; chkGroup.Enabled = false;
//long process
}



______________________On btn Click_________________________________
Thread create = new Thread(process);
create.IsBackground = true;
create.Start();
Posted 20-Jun-13 6:21am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

private void process()
{
btnCreate.Invoke((MethodInvoker) delegate { btnCreate.Enabled = false; chkGroup.Enabled = false; });
//long process
}
 
Thread create = new Thread(new ThreadStart(process));
create.IsBackground = true;
create.Start();

You have to pass a delegate to the Thread object, which is of type ThreadStart. Then for updating the UI you have to update it on the UI thread, using the Control.Invoke method.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 20-Jun-13 16:47pm
   
My 5, but: I would not explain the thread start, as there are different methods of getting a thread, so you better don't create an impression that this method is related to the problem, besides, OP probably knows it already. Also, the method if invocation is not the only one. Just good to know...

Actually, it's better to pass parameters (such as btnCreate or chkGroup) to Invoke method, to avoid unnecessary closure effect (which however can be well used, but not everyone understands its implications).

—SA
SatheeshK787 at 21-Jun-13 1:31am
   
Thanks !

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

  Print Answers RSS
0 OriginalGriff 485
1 jyo.net 480
2 DamithSL 328
3 Sergey Alexandrovich Kryukov 270
4 CPallini 222
0 DamithSL 541
1 OriginalGriff 495
2 Mika Wendelius 469
3 Sergey Alexandrovich Kryukov 330
4 Afzaal Ahmad Zeeshan 324


Advertise | Privacy | Mobile
Web03 | 2.8.150731.1 | Last Updated 20 Jun 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100