Click here to Skip to main content
11,584,612 members (64,666 online)
Rate this: bad
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;
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 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).

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 430
1 Sergey Alexandrovich Kryukov 367
2 Dave Kreskowiak 260
3 Richard Deeming 220
4 virusstorm 194
0 OriginalGriff 1,703
1 Sergey Alexandrovich Kryukov 1,267
2 Abhinav S 711
3 Dave Kreskowiak 687
4 Suvendu Shekhar Giri 568

Advertise | Privacy | Mobile
Web03 | 2.8.150707.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