Sai Stuff to Developers

July 29, 2008

Safe Accessing Controls from the Unsafe Child Threads

Filed under: DotNet — tosaik @ 7:55 am
Tags:

In general, whenever you are working with child threads you can’t access the controls from Form class you will get a runtime error. If you want to access these controls there is a solution for this problem, every controls derived from Form class has the property InvokeRequired. This property returns Boolean value i.e.., True or False. When this property returns false means accessing to this control is not safe, when it returns true it means that accessing to this control is safe. So, the condition to access a label control named label1 safely from child thread is shown below:

 

C#

 

if (!label1.InvokeRequired)

{

  someFunction(label1.Text);

}

 

Vb

 

If NOT label1.InvokeRequired Then

 

  someFunction(label1.Text)

 

End If

 

Now, you understand the importance of InvokeRequired property, but the problem is when this property returns true it means accessing particular control is not safe so how can we make a safe call to that particular Control?

 

Here the answer is using Delegates. I think you know the Working and importants of   Delegates if you are not familiar with delegates please refer delegate concept before proceeding for now I can say some information regarding delegates is, it is a special kind of object which stores every kind of information about particular function.

 

First declare the delegate as shown:

 

delegate void delLoadFirst();      

 

Here the name of the delegate is delLoadFirst, now you create a function it should match the definition with delegate definition.For now I will take  LoadFirst() as my function. Now in LoadFirst() function write as shown below:

 

Void LoadFirst()

{

  

if (!label1.InvokeRequired)

{

  someFunction(label1.Text);

}

Else

  {

  delLoadFirst df = new delLoadFirst(LoadFirst);

  df.Invoke();

  }

}

 

As you can absorve from the above code, the if condition will fails when InvokeRequired property of label1 returns true(means its not safe to call a control label1 from here.) and it goes to else condition where we created delegate object by sending function LoadFirst() as its parameters now when you call Invoke method of this delegate object it will create a separate thread (don’t forget delegate always creates safe threads) and runs the code of the function which you sent as a parameter at the time of creating delegate object.Here in this case LoadFirst() is the method executes by the thread created by delegate delLoadFirst.now again the if condition checks and this time InvokeRequired property of label1 returns false that means its safe to access the control label1 from this thread because this thread is created by delegate so it is safe thread.Now the if condition satisfies and the statements in the if block will executes in this case someFunction(label1.Text); is the statement executes afther this if block compleates the safe thread will joins to its parent thread.

 

This is the way we can access any control from unsafe threads.   

 

Have a nice day.

🙂

 

     

Create a free website or blog at WordPress.com.