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.

🙂

 

     

Advertisements

3 Comments »

  1. Thank U…

    Comment by ravi — August 1, 2008 @ 1:11 pm | Reply

  2. best regards to administrators of saimaterial.wordpress.com – you made a beautiful forum!
    I was able made only one blog with the info about a medical pills
    I hope this it is suitable section for my message if there is no that simply remove it and forgive for disturbance
    My first blog – buy orthoevra
    sorry for errors – here is a obscure and I have very small keyboard

    Comment by fundendeala — September 9, 2008 @ 3:43 pm | Reply

  3. Nothing seems to be easier than seeing someone whom you can help but not helping.
    I suggest we start giving it a try. Give love to the ones that need it.
    God will appreciate it.

    Comment by Geagreewets — January 22, 2009 @ 4:04 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: