Click here to Skip to main content
15,498,797 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends, once again i triggered this message,
can anyone of u teach me how to settle down this problem..

i try to start a thread to get the information from task manager
so i can get the information of which process is currently activate, suspended or close

previously i triggered the same error message by try to create new usercontrol with thread which are wrong

in this case how do i change my label's text ??
cause when i try to change my text of label inside the thread it will trigger this.

What I have tried:

<pre>private void detector() {
            while (true) {
                var mycafeclientPro = Process.GetProcessesByName("notepad");
                if (mycafeclientPro.Length > 0) {
                    foreach (Process p in mycafeclientPro) {
                        if (p.Responding) {
                            //statusPanel.BackColor = Color.FromArgb(190, 197, 209);
                            statusLabel.Text = "Application activated";
                            //statusLabel.ForeColor = Color.FromArgb(255, 255, 255);
                            //statusLabel.Location = new Point((statusPanel.Width - statusLabel.Width) / 2, (statusPanel.Height - statusLabel.Height) / 2);
                        else {

                else {
Updated 12-Dec-17 21:11pm

You cannot access UI elements (controls, forms) at all, except on the thread on which they were created: the UI thread. If you try, you will get a cross thread exception.
So this code:
statusLabel.Text = "Application activated";
cannot be executed on your "new" thread.

To solve this, either
1) Invoke your control: Control.Invoke Method (Delegate) (System.Windows.Forms)[^]
2) Use the BackgroundWorker class (BackgroundWorker Class (System.ComponentModel)[^] and do the control updates in the ProgressChanged event handler.
Share this answer
delegate void  SetTextOnControl (Control controlToChange, string message);

			public void SetText(Control controlToChange, string message)
				if (controlToChange.InvokeRequired)
					SetTextOnControl DDD = new SetTextOnControl(SetText);
					controlToChange.Invoke(DDD, controlToChange, message);
					controlToChange.Text = message;

Than call this SetTexst from any thread.
Share this answer

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

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