Click here to Skip to main content
13,053,994 members (78,609 online)
Rate this:
Please Sign up or sign in to vote.
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;
Posted 20-Jun-13 6:21am

1 solution

Rate this: bad
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;

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.
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).

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 20 Jun 2013
Copyright © CodeProject, 1999-2017
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