Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > General Coding > Coding Tutorials
You last visited: Today at 12:33

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Tutorial[C#]: Accessing form properties from different thread

Discussion on Tutorial[C#]: Accessing form properties from different thread within the Coding Tutorials forum part of the General Coding category.

Reply
 
Old   #1
 
InvincibleNoOB's Avatar
 
elite*gold: 20
Join Date: Mar 2007
Posts: 4,277
Received Thanks: 2,990
Tutorial[C#]: Accessing form properties from different thread

Hello,

I originally wrote this tutorial for Stealthex ,but I will post it here aswell.

The purpose of the tutorial is to teach you how to access form members from threads other than the main thread.

I hope you like it.

//
In the beggining of my C# development I encounted much problems when I wanted to access the properties on my Form.
Same goes for Delphi,but I don't have a solution for Delphi.

This is the C# way.
To access a property on your form,you can create a method or class contructor:

Code:
public string TextValue
        {
            set
            {
                    this.Memo.Text += value + "\n";
            }
        }
The contructor is namedTextValue,the form is,lets say - Form1.

To access that type of functions,you have to assign a value to them,in our case a string.
So to call that function you should use:
Code:
Form1.TextValue = "Test Text";
However,you may ask,"I cannot access the constructor in Form1 itself,it's missing".
This is because you didn't create a instance of Form1,you can also use this
Code:
        public static void addtxt(string txt)
        {
            var form = Form.ActiveForm as Form1;
            if(form != null)
                form.TextValue = txt;
        }
See,it works!

End of part I


Part II

In part 1 we discussed how to use form properties from different classes without making the properties's modifiers public.

However,an exception will be thrown if you try to access that thread from another thread(lets say you're making a TCP client with async callback).

To prevent that,we've got to use Invoke.

This is how our constructor would look like if we use Invoke

Code:
        public string TextValue
        {
            set
            {
                if (this.Memo.InvokeRequired)
                { //if invoke IS required
                    this.Invoke((MethodInvoker)delegate
                    {
                        this.Memo.Text += value + "\n";
                    });
                }
                else //if invoke is not required
                {
                    this.Memo.Text += value + "\n";
                }
            }
        }
Invoke is most likely always used and to be honest,that else statement is just useless,but it's good to stay there for a better view from intellectual point of view.

Now comes the handy part.(aka part 3 - the last one)
You may now wonder "So I have to create those two functions with so much code for every property on my form?".
Of course you don't have to.That's why there are "actions" since C# v2.0 went out. (:

This is the latest function I use,however it requires to change form properties's modifiers from private to Internal(protected internal is recommended).

And here comes the ultra-handy code that I use in my clientless:
//Note i made the class "partial" ,so it dont have to mess the form.cs file with it.

Code:
namespace Invincible_s_Clientless
{
    public partial class FormMain
    {
        int Ticks = 0;

        EndianBitConverter converter = EndianBitConverter.Little;

        [DllImport("kernel32")]
        public extern static int LoadLibrary(string librayName);

        public static void PerformActionOnMainForm(Action<FormMain> action)
        {
            var form = Form.ActiveForm as FormMain;
            if (form != null)
                form.PerformAction(action);
        }

        public void PerformAction(Action<FormMain> action)
        {
            if (InvokeRequired)
                Invoke(action, this);
            else
                action(this);
        }
    }
}

usage:

Code:
form1.PerformActionOnMainForm(form => form.memo.text += "My newest invoke method :)\n");
can also be used with multiple lines.Example I use this to add the servers names in the combobox of my clientless
Code:
                    FormMain.PerformActionOnMainForm(form =>
                        {
                            form.comboServers.Items.Clear();
                            for (ii = 0; ii < num; ii++)
                            {
                                form.comboServers.Items.Insert(ii, Names[ii]);
                            };
                        });
End of my lesson.
InvincibleNoOB is offline  
Thanks
3 Users
Old 08/02/2009, 15:05   #2
 
elite*gold: 0
Join Date: Aug 2009
Posts: 5
Received Thanks: 1
tnx
titolook4 is offline  
Old 08/05/2009, 01:20   #3
 
.1337's Avatar
 
elite*gold: 25
Join Date: Dec 2008
Posts: 1,070
Received Thanks: 514
i think c++ will be hard for me since i started autoit from about a week
so if u can make a tutorials for easy sucess at autoit i'll be very thank full.
.1337 is offline  
Old 01/03/2010, 17:57   #4
 
elite*gold: 0
Join Date: Sep 2008
Posts: 1,683
Received Thanks: 506
Quote:
Originally Posted by .1337 View Post
i think c++ will be hard for me since i started autoit from about a week
so if u can make a tutorials for easy sucess at autoit i'll be very thank full.
Seriously, what does that have to do with this thread?
C# and C++ are two different things.
Basser is offline  
Reply


Similar Threads Similar Threads
[Small tutorial] fixing the -300 error form the Dshop
08/24/2009 - Dekaron Private Server - 12 Replies
this error occurs when you manually input your data into your databse and as a result you enter the next user_no after the two entries already there which so happens to be 3. This is the problem simply avoid user_no's that begin with 3 and try to make them all begin with 6, it really doesnt matter what they are you could have 787230348 or 86349447. Simply change the number from 3 to any other number u can think of. And your problems solved.
[Resource] Complete Item Index with Properties
06/12/2008 - Dekaron Exploits, Hacks, Bots, Tools & Macros - 8 Replies
Here is the entire Item Index List for both in game items and store items, complete with all property settings. I have included the offset at the bottom of the list for those who wish to change the values and see what happens. note: I've managed to change the stack rate, but it resets after reloging back on, so if you want 1000 health potions in one slot, it will redistribute after server logon. If you find a better way of doing it, please share. Sample: item index Name Rank Type...
[Info] Item properties list
06/09/2008 - General Gaming Releases - 0 Replies
I had a look around and couldnt see this posted by anyone else, my apologies if its a repost. I dont take credit for compiling this list, I merely stumbled across it on the AOC forums and thought it was great and people here may find it useful. It can make finding what you're looking for on the auction house a whoooole lot easier. Armor Item Properties (including Shields) Algid +% Cold Invuln Baneful + Unholy Damage
Accessing storage
05/13/2008 - RF Online - 5 Replies
Question! Is there a way to access your storage bank from anywhere?
Accessing Servers
10/27/2007 - Conquer Online 2 - 2 Replies
I think i've found a new way to get into TQ's servers using FTP but I'm having trouble with the username and password. I think I need to use an email address for the password. Ive been trying for a couple of days but i've only been using brutus and haven't had much luck. If anyone can offer some assistance or advice PM me



All times are GMT +2. The time now is 12:33.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.