how to change property of form in an other started thread?
-
Hi all, how can i change one of my forms properties in one other running thread? i mean i have thread1 which form is made in and thread2 which is resetting some of it properties. but doing so, my form goes into 'not responding' state (by operating system) and i have to end the application. what is the solution? msdn couldn't help. thanks :-D ;P
-
Hi all, how can i change one of my forms properties in one other running thread? i mean i have thread1 which form is made in and thread2 which is resetting some of it properties. but doing so, my form goes into 'not responding' state (by operating system) and i have to end the application. what is the solution? msdn couldn't help. thanks :-D ;P
While you are waiting for someone to more completely answer your query, I would suggest that you research (MSDN and GOOGLE) the
InvokeRequired
property. This is a fundamental part of the solution and there are examples aplenty, which should at least get you thinking in the right areas.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
While you are waiting for someone to more completely answer your query, I would suggest that you research (MSDN and GOOGLE) the
InvokeRequired
property. This is a fundamental part of the solution and there are examples aplenty, which should at least get you thinking in the right areas.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
thanks ;) but it is not really going to work. my form again goes to 'not responding'. why? :(
modified on Tuesday, April 21, 2009 3:44 PM
-
Hi all, how can i change one of my forms properties in one other running thread? i mean i have thread1 which form is made in and thread2 which is resetting some of it properties. but doing so, my form goes into 'not responding' state (by operating system) and i have to end the application. what is the solution? msdn couldn't help. thanks :-D ;P
I've constructed a very simple sample using a System.Timers.Timer as it runs on a seperate thread. You should be able to figure it out from this. [Edit] Changed the sample code to show how to call a method with parameters. [/Edit]
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
System.Timers.Timer timer;
public Form1()
{
InitializeComponent();
timer = new System.Timers.Timer();
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Start();
}void timer\_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { /\* System.InvalidOperationException "Cross-thread operation not valid..." will be thrown if you try to alter the property directly from a different thread \*/ if (this.InvokeRequired) { BeginInvoke(new MethodInvoker(delegate() { UpdateText(sender, e); })); } else { UpdateText(sender, e); } } private void UpdateText(object sender, System.Timers.ElapsedEventArgs e) { Text = e.SignalTime.ToString(); } }
}
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)modified on Tuesday, April 21, 2009 4:50 PM