How to access the form controls in the thread
-
Hi everyone, I am new to code project , sorry if i place the question in wrong area. I am struck by a problem while using threads. In my windows project I have MDI form and shows different forms in the MDI form.In one form i am sending mails. i am sending the mails in a separate process using threads to enable the user to do anything in the application while sending the mails.one progress bar will appear in the mdi form status strip, when mail is sending. When user clicks the Send button in the form i make it disable until the mail is sent.Before calling thread i make it disable and after completion of thread's work i make it enable, it works fine. but my problem is, When user opens this form and send mails, if he visits another form and again comes to this form i am checking in the form load whether thread is going on, by check whether progress bar is visible in the mdi form , if it is visible i make the send button disable. I need the send button enable after thread completion, i make it. but it is not working. the send button is still disabled.I don't know why it is happening. if user remains in the same form it is working fine. if he visits another form it is not working. I am using thread invoke method for using the controls. but still the problem is there. Please any one help me Thanks in advance. Rajesh.
-
Hi everyone, I am new to code project , sorry if i place the question in wrong area. I am struck by a problem while using threads. In my windows project I have MDI form and shows different forms in the MDI form.In one form i am sending mails. i am sending the mails in a separate process using threads to enable the user to do anything in the application while sending the mails.one progress bar will appear in the mdi form status strip, when mail is sending. When user clicks the Send button in the form i make it disable until the mail is sent.Before calling thread i make it disable and after completion of thread's work i make it enable, it works fine. but my problem is, When user opens this form and send mails, if he visits another form and again comes to this form i am checking in the form load whether thread is going on, by check whether progress bar is visible in the mdi form , if it is visible i make the send button disable. I need the send button enable after thread completion, i make it. but it is not working. the send button is still disabled.I don't know why it is happening. if user remains in the same form it is working fine. if he visits another form it is not working. I am using thread invoke method for using the controls. but still the problem is there. Please any one help me Thanks in advance. Rajesh.
Hi, threads can't access Controls directly, only the main thread can. You need one of two things: 1. use the Control.InvokeRequired/Control.Invoke pattern (search for InvokeRequired in CP articles) 2. or use a BackgroundWorker instead of a Thread; that one has InvokeRequired built-in into its ReportProgress and WorkCompleted events. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi, threads can't access Controls directly, only the main thread can. You need one of two things: 1. use the Control.InvokeRequired/Control.Invoke pattern (search for InvokeRequired in CP articles) 2. or use a BackgroundWorker instead of a Thread; that one has InvokeRequired built-in into its ReportProgress and WorkCompleted events. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
Thanks for reply, I am using Invoke method, still problem arises. Actually while i start a thread from the form i am able to enable the button but once if i visit another form and again returns to the this form while the thread is running the button is not enabled in the thread. why iam not able to do it. i think u understand ny question .please let me know. waiting for ur reply. Regards, Rajesh.
-
Thanks for reply, I am using Invoke method, still problem arises. Actually while i start a thread from the form i am able to enable the button but once if i visit another form and again returns to the this form while the thread is running the button is not enabled in the thread. why iam not able to do it. i think u understand ny question .please let me know. waiting for ur reply. Regards, Rajesh.
sekhar.k wrote:
i think u understand ny question
apparently not. you did something wrong or didn't tell an essential part of the story. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.