3

Can someone explain this to me. I Have code like this, but I don't know what the meaning.

void ConnectionManager_ConnectionFailed(object sender, EventArgs e)
{
    BeginInvoke((MethodInvoker)delegate()
    {
        if (cbAutoConnect.Checked)
            Connect();
        else
            State = ConnectState.NotFound;
    });
}

My question:

  1. Is this method an EventHandler?

  2. What the purpose of this code is?

    BeginInvoke((MethodInvoker)delegate() {

  3. What happened to the "State" when execution condition "Else"?

Note:

Connect is a Method.

State is an enum describe by this code

public ConnectState State
        {
            get
            {
                return _State;
            }

            {
                if (_State == value)
                    return;

                _State = value;

                switch (value)
                {
                    case ConnectState.Connected:
                        DoingSomeThing;
                        break;

                    case ConnectState.Connecting:
                        DoingSomeThing;
                        break;

                    case ConnectState.NotFound:
                        DoingSomeThing;
                        break;
                }

                if (StateChanged != null)
                    StateChanged(this, new EventArgs<ConnectState>(value));
            }
        }

Another Hint

The enum initiation

public enum ConnectState { Connected, Connecting, NotFound }
ConnectState _State = ConnectState.NotFound;

I do not know what this exactly, but I think this is a custom EventHandler declaration for "ConnectState" object/class.

public EventHandler<EventArgs<ConnectState>> StateChanged;
user3332360
  • 105
  • 8
  • 1
    +1 for nice written question. Wish every new member will do so. – Micha Feb 21 '14 at 07:02
  • 1
    Its a eventhandler. For more details please check this link http://stackoverflow.com/questions/1167771/methodinvoker-vs-action-for-control-begininvoke – prashant Feb 21 '14 at 07:04
  • @prashanth so `delegate()` is an pointer of method and `MethodInvoker` represents a delegate. Below @user3036342 said the `connect()` will repeat ass long the cbAutoConnect is chacked. Why this happen? I did not find a code for looping. IF anything to do with this code: `BeginInvoke` is like `Thread.Start()`? – user3332360 Feb 21 '14 at 07:35

3 Answers3

2

Is this method an EventHandler?

Yes it is.

What the purpose of this code is?

You are defining a anonymous method with delegate { ... } then casting it to MethodInvoker delegate and passing it to Control.BeginInvoke method which is executing this anonymous method asynchronously on the thread that your control was created on.

What happened to the "State" when execution condition "Else"?

If cbAutoConnect.Checked is true Connect method is calling, otherwise State's value is changed to ConnectState.NotFound, that's it.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • whether the `Connect ();` would be repeated during `cbAutoConnect` is checked, as a result of code `BeginInvoke ();` similar to `Thread.start()`, or caused by something else? – user3332360 Feb 21 '14 at 08:05
1

It's an eventhanlder like you said. If the connection fails, and auto connect is checked, it tries to reconnect again, if not, it gives you a "NotFound" error by using the ConnectionState

I'd suggest asking your questions to the programmer who wrote this instead

user3036342
  • 1,023
  • 7
  • 15
  • I could not contact the programmer who wrote the code. so I tried to read the code for development purposes. Adds some minor features. I still wonder, why the Connect (); continues to repetition. I did not find a code for looping. if anything to do with this code: `BeginInvoke((MethodInvoker)delegate() {` – user3332360 Feb 21 '14 at 07:11
-2

1 probably yes,also you can find who has called it to make sure the guess. 2 asyn excute the anonymous Delegate(method)
3 er~~ this member's set method is very strange.

look this : case ConnectState.NotFound: DoingSomeThing; after setting value , it is not over, if the state is changed , go on DoingSomeThing.