views:

547

answers:

3

my aim is that in the function "Dummy" i can change the controls like labels etc of the form from which the thread is initiating..how to do it..please don't suggest completely different strategies or making a worker class etc...modify this if you can

        Thread pt= new Thread(new ParameterizedThreadStart(Dummy2));


        private void button1_Click(object sender, EventArgs e)
        {                    
            pt = new Thread(new ParameterizedThreadStart(Dummy2));
            pt.IsBackground = true;
            pt.Start( this );
        }


        public static void Dummy(........)
        {
           /*                
           what i want to do here is to access the controls on my form form where the
           tread was initiated and change them directly
           */ 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (t.IsAlive)
                label1.Text = "Running";
            else
                label1.Text = "Dead";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            pt.Abort();
        }


    }
}

what i plan is that i could do this in the "Dummy" function

Dummy( object p)
{
  p.label1.Text = " New Text " ;
}
+3  A: 

You can't do this. You can only access a UI control on the same thread that created it.

See the System.Windows.Forms.Control.Invoke Method and the Control.InvokeRequired property.

John Saunders
Can you give me an example how to access a UI control from some other thread that "did not created it"
Junaid Saeed
Can you click the links I provided?
John Saunders
+2  A: 

Can use something like this:

private void UpdateText(string text)
{
    // Check for cross thread violation, and deal with it if necessary
    if (InvokeRequired)
    {
        Invoke(new Action<string>(UpdateText), new[] {text});
        return;
    }

    // What the update of the UI
    label.Text = text;
}

public static void Dummy(........)
{
   UpdateText("New text");
}
Svish
Who are you calling Invoke and InvokeRequired on here?
Marc
The form that has the label.
Svish
+4  A: 

You could do this, supposing you're passing an instance of the form to the thread method using the t.Start(...) method:

private void Form_Shown(object sender)
{
    Thread t = new Thread(new ParameterizedThreadStart(Dummy));
    t.Start(this);
}

....


private static void Dummy(object state)
{
    MyForm f = (MyForm)state;

    f.Invoke((MethodInvoker)delegate()
    {
        f.label1.Text = " New Text ";
    });
}

EDIT
Added thread start code for clarity.

Thorsten Dittmar
now if i want to do this that this thread does what it is supposed to do and after every "X" seconds it wakes again and again
Junaid Saeed
how much memory this "Invoke" is going to take, lets say if the parent form has a lot of UI controls n all
Junaid Saeed
For first comment: Add timer to form. Fire every X seconds in case the thread is not already active and create new thread (threads can't be reused) OR loop in your thread method and after doing what you need to do: sleep for X seconds (you must quit the thread before closing the form!!)
Thorsten Dittmar
For second comment: I actually have NO idea and for me it hasn't ever been relevant, as you have no other choice anyway. This is the only way to do it.
Thorsten Dittmar
isn't there a way that i could ask the thread to go sleep for X seconds after that time it wakes again and does what it is supposed to do, i thought of timer earlier but just want to know if the thread does it by itself
Junaid Saeed
As I said above, that's the second option: Thread.Sleep(X * 1000) whithin a while-loop within the thread method.
Thorsten Dittmar
and when the thread has executed all its statements does it aborts, sleeps, suspend or what, the state of the thread, you must pardon my questions, i am new to threds
Junaid Saeed
ok Sleep got it with a loop, i was fantasizing sleep without a lopp :)
Junaid Saeed