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?Monday, September 9, 2013
ISynchronizeInvoke implementation example
Put the following code for classes you want to implement ISynchronizeInvoke should do the job
Subscribe to:
Post Comments (Atom)
 
Are you looking to make cash from your websites by running popup ads?
ReplyDeleteIf so, did you know about Propeller Ads?