Monday, September 9, 2013

ISynchronizeInvoke implementation example

Put the following code for classes you want to implement ISynchronizeInvoke should do the job
        private readonly object _sync= new object();

        public IAsyncResult BeginInvoke(Delegate method, object[] args)
        {
            var result = new SimpleAsyncResult();

            ThreadPool.QueueUserWorkItem(
                delegate
            {

                result.AsyncWaitHandle = new ManualResetEvent(false);
                try
                {
                    result.AsyncState = Invoke(method, args);
                }
                catch (Exception exception)
                {
                    Debug.WriteLine(exception.Message);
                    Debug.WriteLine(exception.StackTrace);
                    result.Exception = exception;
                }
                result.IsCompleted = true;
            });


            return result;
        }


        public object EndInvoke(IAsyncResult result)
        {
            if (!result.IsCompleted)
            {
                result.AsyncWaitHandle.WaitOne();
            }


            return result.AsyncState;
        }


        public object Invoke(Delegate method, object[] args)
        {
            lock (_sync)
            {
                return method.DynamicInvoke(args);
            }
        }


        public bool InvokeRequired
        {
            get { return true; }
        }
        public class SimpleAsyncResult : IAsyncResult
        {
            private object _state;


            public bool IsCompleted { get; set; }


            public WaitHandle AsyncWaitHandle { get; internal set; }


            public object AsyncState
            {
                get
                {
                    if (Exception != null)
                    {
                        throw Exception;
                    }
                    return _state;
                }
                internal set { _state = value; }
            }

            public bool CompletedSynchronously
            {
                get { return IsCompleted; }
            }


            internal Exception Exception { get; set; }

        }
        
  

Want to read more?

How to put code snippet into blogger like blogspot


// Comment
public class Testing {
public Testing() {
}
 
public void Method() {
/* Another Comment
on multiple lines */
int x = 9;
}
}
Reference here: http://www.craftyfella.com/2010/01/syntax-highlighting-with-blogger-engine.html

[Self Reference] Bug Lists

20 Sep 2013
  1. if…else.. do not consider last redundant assignment
  2. when dividing, denumerator can be zero
8 Sep 2013
  1. Do not initialize class values before using it - Do not initialize the RuntimeAdapter for InitialState (leave empty), when using RuntimeAdapter
  2. == and !=, if(root.Count==1) throw (only allow once) -> Suppose to be !=1
-----
  1. wrong sequence - Clear the ConcreteServices at the wrong place just after populating it, it should be just before populating it
  2. missed item - case.execute() is not implemented
  3. missed item - the switch does exert the event to partialModelState
  4. redundant call - put two calls in start and internal start for simulate()
  5. wrong name - when copy paste forget to change all the names
  6. Not setting global one - declare a lobal Simulator sm, and didn't set to global one
  7. terminate event not sent -  skip is never called as it won't evoluate to that event, correct way to send terminate event is via the Bprocess execute()
  8. Unexpected - parallel.run(…), in … throw exception (a duplicate var can solve the problem)
  9. parallel.invoke bug can't throw outside: http://stackoverflow.com/q/18764007/1497720
----


Previous two days
  1. not initialize v with maximum when doing v=min(v, value)
  2. not initialize v with zero and maximum separately, when v has to do with min and plus
  3. in switch, change the case activity value
  4. does not understand that geteventid, is a state with previous edges
  5. using a the terminal states without initializing it with value


  1. Index out of bound - action0Col[1] == "if", but action0Col may not have index 1 (e.g., for atomic action)
  2. copy paste - result.Add(new ActionPair(actionPair.Value[0], actionPair.Value[1], action0Col[2] == "c"));
  3. not assigning value to instance - not assigning the value to Available of Nonfunctional Attribute, but assign to a variable totAvailability
  4. split value problem(knowledge problem) - [if:0:c] split becomes {"[if","0","c]"}
  5. copy paste -  avgResponseTime += nf.ResponseTime;  should be  avgResponseTime += nf.avgResponseTime;
  6. Concept problem - in AggregateActions firstResponseTime and firstAvgResponseTime treat the same
  7. "a:name:<empty>", string split, then only have two columns, access 2 index out of bound



  1. Value is null, lock{..}{}..this .. is empty!
  2. Queue and trace not initialize when accessed