Click here to Skip to main content
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 7: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 390
1 Jochen Arndt 165
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 20 Jun 2013
Copyright © CodeProject, 1999-2014
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