Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

1 solution

C#
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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 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)



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