Exemplo n.º 1
0
        protected void UpdateValue(object value)
        {
            if (_targetObject == null || !_targetObject.IsAlive || _targetProperty == null || !_targetProperty.IsAlive)
            {
                return;
            }

            DependencyObject obj   = _targetObject.Target as DependencyObject;
            object           tProp = _targetProperty.Target;

            if (tProp is DependencyProperty)
            {
                DependencyProperty prop         = tProp as DependencyProperty;
                Action             updateAction = () => obj.SetValue(prop, value);

                // Check whether the target object can be accessed from the
                // current thread, and use Dispatcher.Invoke if it can't
                if (obj.CheckAccess())
                {
                    updateAction();
                }
                else
                {
                    obj.Dispatcher.Invoke(updateAction);
                }
            }
            else if (tProp is PropertyInfo)
            {
                PropertyInfo prop = tProp as PropertyInfo;
                prop.SetValue(_targetObject, value, null);
            }
        }
Exemplo n.º 2
0
        public void GetResponse_ThreadPool()
        {
            WebClient wc = new WebClient();

            if (new Uri(wc.BaseAddress).Scheme != "http")
            {
                EnqueueTestComplete();
                return;
            }

            DependencyObject TestPanel           = this.TestPanel;
            Thread           main_thread         = Thread.CurrentThread;
            bool             get_response_called = false;

            /* Check that the GetResponse callback is executed on a thread-pool thread */

            HttpWebRequest request = (HttpWebRequest)GetWebRequest(WebClientTest.TimecodeLongWmv);

            Enqueue(() =>
            {
                request.BeginGetResponse(delegate(IAsyncResult ar)
                {
                    Assert.IsFalse(TestPanel.CheckAccess());
                    Assert.AreNotEqual(main_thread.ManagedThreadId, Thread.CurrentThread.ManagedThreadId, "Different thread ids in BeginGetResponse");
                    get_response_called = true;
                    try {
                        request.EndGetResponse(ar);
                    } catch (Exception ex) {
                        Console.WriteLine(ex);
                    }
                }, request);
            });
            EnqueueConditional(() => get_response_called);
            EnqueueTestComplete();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Attempts to "safely" set the specified <see cref="DependencyProperty"/> value as <typeparamref name="T"/>
        /// </summary>
        /// <typeparam name="T">The type of value to retrieve</typeparam>
        /// <param name="obj">The object to retrieve the value from</param>
        /// <param name="property">The property to retrieve</param>
        /// <param name="milliTimeout">The timeout (in milliseconds) of the dispatcher operation (if Dispatcher Use is neccesary)</param>
        /// <returns></returns>
        public static T GetValueSafe <T>(this DependencyObject obj, DependencyProperty property, double milliTimeout = 50d)
        {
            object val  = null;
            bool   done = false;

            if (!obj.CheckAccess())
            {
                obj.Dispatcher.Invoke(new Action <DependencyObject, DependencyProperty>((d, dp) =>
                {
                    val  = d.GetValue(dp);
                    done = true;
                }), TimeSpan.FromMilliseconds(milliTimeout), obj, property);
            }
            else
            {
                return((T)obj.GetValue(property));
            }

            while (!done)
            {
                Thread.Sleep(0);
            }

            return((T)val);
        }
Exemplo n.º 4
0
        public void GetRequestStream_ThreadPool()
        {
            WebClient wc = new WebClient();

            if (new Uri(wc.BaseAddress).Scheme != "http")
            {
                EnqueueTestComplete();
                return;
            }

            DependencyObject TestPanel                 = this.TestPanel;
            Thread           main_thread               = Thread.CurrentThread;
            List <Stream>    streams_to_close          = new List <Stream> ();
            bool             get_request_stream_called = false;

            /* Check that the GetRequestStream callback is executed on a thread-pool thread */

            HttpWebRequest request = (HttpWebRequest)GetWebRequest(WebClientTest.TimecodeLongWmv);

            request.Method = "POST";
            Enqueue(() =>
            {
                request.BeginGetRequestStream(delegate(IAsyncResult ar)
                {
                    Assert.IsFalse(TestPanel.CheckAccess());
                    Assert.AreNotEqual(main_thread.ManagedThreadId, Thread.CurrentThread.ManagedThreadId, "Different thread ids in BeginGetRequestStream");
                    streams_to_close.Add(request.EndGetRequestStream(ar));
                    get_request_stream_called = true;
                }, request);
            });
            EnqueueConditional(() => get_request_stream_called);
            Enqueue(() => { streams_to_close.ForEach((v) => v.Close()); });
            EnqueueTestComplete();
        }
 protected void UpdateValue(object value)
 {
     foreach (object target in _targetList)
     {
         // if (_target != null)
         {
             if (_targetProperty is DependencyProperty)
             {
                 DependencyObject   obj          = target as DependencyObject;
                 DependencyProperty prop         = _targetProperty as DependencyProperty;
                 Action             updateAction = () => obj.SetValue(prop, value);
                 if (obj.CheckAccess())
                 {
                     updateAction();
                 }
                 else
                 {
                     obj.Dispatcher.Invoke(updateAction);
                 }
             }
             else
             {
                 PropertyInfo prop = target as PropertyInfo;
                 prop.SetValue(target, value, null);
             }
         }
     }
 }
Exemplo n.º 6
0
        private void TryRegisterRegion()
        {
            DependencyObject targetElement = this.HostControl;

            if (targetElement.CheckAccess())
            {
                IRegionManager regionManager = this.FindRegionManager(targetElement);

                IRegionManager attachedRegionManager = this.GetAttachedRegionManager();

                if (regionManager != attachedRegionManager)
                {
                    if (attachedRegionManager != null)
                    {
                        this.attachedRegionManagerWeakReference = null;
                        attachedRegionManager.Regions.Remove(this.Region.Name);
                    }

                    if (regionManager != null)
                    {
                        this.attachedRegionManagerWeakReference = new WeakReference(regionManager);
                        regionManager.Regions.Add(this.Region);
                    }
                }
            }
        }
        /// <summary>
        ///     Gets the value synchronize.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">The object.</param>
        /// <param name="property">The property.</param>
        /// <returns></returns>
        public static T GetValueSync <T>(this DependencyObject obj, DependencyProperty property)
        {
            if (obj.CheckAccess())
            {
                return((T)obj.GetValue(property));
            }

            return((T)obj.Dispatcher.Invoke(() => obj.GetValue(property)));
        }
        /// <summary>
        /// Gets the parent in the visual or logical tree.
        /// </summary>
        /// <param name="depObj">The dependency object.</param>
        /// <param name="isVisualTree">True for visual tree, false for logical tree.</param>
        /// <returns>The parent, if available.</returns>
        public static DependencyObject GetParent(this DependencyObject depObj, bool isVisualTree)
        {
            if (depObj.CheckAccess())
            {
                return(GetParentInternal(depObj, isVisualTree));
            }

            return((DependencyObject)depObj.Dispatcher.Invoke(new Func <DependencyObject>(() => GetParentInternal(depObj, isVisualTree))));
        }
Exemplo n.º 9
0
 /// <summary>
 /// Sets the value thread-safe.
 /// </summary>
 /// <param name="obj">The object.</param>
 /// <param name="property">The property.</param>
 /// <param name="value">The value.</param>
 /// <typeparam name="T">The type of the value.</typeparam>
 public static void SetValueSync <T>(this DependencyObject obj, DependencyProperty property, T value)
 {
     if (obj.CheckAccess())
     {
         obj.SetValue(property, value);
     }
     else
     {
         obj.Dispatcher.Invoke(new Action(() => obj.SetValue(property, value)));
     }
 }
Exemplo n.º 10
0
 /// <summary>
 /// Attempts to "safely" set the specified <see cref="DependencyProperty"/> value to the specified value
 /// </summary>
 /// <param name="obj">The object to set the value on</param>
 /// <param name="property">The property to set</param>
 /// <param name="value">The value to set</param>
 public static void SetValueSafe(this DependencyObject obj, DependencyProperty property, object value)
 {
     if (!obj.CheckAccess())
     {
         obj.Dispatcher.Invoke(new Action <DependencyObject, DependencyProperty, object>((d, p, o) => d.SetValueSafe(p, o)), obj, property, value);
     }
     else
     {
         obj.SetValue(property, value);
     }
 }
Exemplo n.º 11
0
 public static void InvokeIfNeeded(this DependencyObject ctl, Action action)
 {
     if (ctl.CheckAccess())
     {
         action();
     }
     else
     {
         ctl.Dispatcher.BeginInvoke(action);
     }
 }
Exemplo n.º 12
0
        void RegisterRegion()
        {
            DependencyObject targetElement = HostControl;

            if (targetElement.CheckAccess())
            {
                TabbedGroup tg = targetElement as TabbedGroup;

                if (tg != null && RegionManager != null)
                {
                    RegionManager.Regions.Add(Region);
                }
            }
        }
        /// <summary>
        /// Gets the value thread-safe.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <param name="property">The property.</param>
        /// <typeparam name="T">The type of the value.</typeparam>
        /// <returns>The value.</returns>
        public static T GetValueSync <T>(this DependencyObject obj, DependencyProperty property)
        {
#if SILVERLIGHT
            return((T)obj.GetValue(property));
#else
            if (obj.CheckAccess())
            {
                return((T)obj.GetValue(property));
            }
            else
            {
                return((T)obj.Dispatcher.Invoke(new Func <object>(() => obj.GetValue(property))));
            }
#endif
        }
Exemplo n.º 14
0
        /// <summary>
        /// ReleaseOnChannel
        /// </summary>
        void DUCE.IResource.ReleaseOnChannel(DUCE.Channel channel)
        {
            // reconsider the need for this lock
            using (CompositionEngineLock.Acquire())
            {
                Debug.Assert(_duceResource.IsOnChannel(channel));

                //release from this channel
                _duceResource.ReleaseOnChannel(channel);

                if (!_duceResource.IsOnAnyChannel)
                {
                    // If this was the last reference on the channel then clear up our state.
                    // Again, we assume here that if the target DependencyObject is animated that
                    // it will be associated with a Dispatcher and that this animation resource
                    // will also be associated with that Dispatcher's channel.

                    DependencyObject d = (DependencyObject)_dependencyObject.Target;

                    // DependencyObject shouldn't have been garbage collected before we've
                    // released all of its property animation resources.
                    Debug.Assert(d != null);

                    // The target DependencyObject should be associated with a Dispatcher.
                    Debug.Assert(d.Dispatcher != null);

                    // Make sure the target belongs to this thread
                    Debug.Assert(d.CheckAccess());

                    // If we're invalid, that means we've added our _updateResourceHandler to the
                    // MediaContext's ResourcesUpdated event. Since we've been entirely released
                    // from the channel we can cancel this update by removing the handler.
                    if (!_isValid)
                    {
                        MediaContext mediaContext = MediaContext.From(d.Dispatcher);
                        mediaContext.ResourcesUpdated -= _updateResourceHandler;
                        _isValid = true;
                    }

                    _updateResourceHandler = null;
                }
            }
        }
        private void TryCreateRegion()
        {
            DependencyObject targetElement = this.TargetElement;

            if (targetElement == null)
            {
                this.Detach();
                return;
            }
            if (targetElement.CheckAccess())
            {
                this.Detach();
                if (!this.regionCreated)
                {
                    string regionName = this.RegionManagerAccessor.GetRegionName(targetElement);
                    CreateRegion(targetElement, regionName);
                    this.regionCreated = true;
                }
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Gets the value of a <see cref="DependencyProperty"/> from a specified
        /// <see cref="DependencyObject"/> on the UI thread, marshalling if ncessary.
        /// </summary>
        /// <param name="obj">The target <see cref="DependencyObject"/>.</param>
        /// <param name="dp">The desired <see cref="DependencyProperty"/>.</param>
        /// <returns>The value of the <see cref="DependencyProperty"/> on the <see cref="DependencyObject"/>.</returns>
        public static object SafeGetValue(this DependencyObject obj, DependencyProperty dp)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            if (dp == null)
            {
                throw new ArgumentNullException("dp");
            }

            if (obj.CheckAccess())
            {
                return(obj.GetValue(dp));
            }

            var self = new Func <DependencyObject, DependencyProperty, object>(SafeGetValue);

            return(Dispatcher.Invoke(self, obj, dp));
        }
Exemplo n.º 17
0
        /// <summary>
        /// Sets the value of a read-only <see cref="DependencyProperty"/> on a specified
        /// <see cref="DependencyObject"/> on the UI thread, marshalling if ncessary.
        /// </summary>
        /// <param name="obj">The target <see cref="DependencyObject"/>.</param>
        /// <param name="key">The desired <see cref="DependencyPropertyKey"/>.</param>
        /// <param name="value">The value to set.</param>
        public static void SafeSetValue(this DependencyObject obj, DependencyPropertyKey key, object value)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (obj.CheckAccess())
            {
                obj.SetValue(key, value);
            }
            else
            {
                var self = new Action <DependencyObject, DependencyPropertyKey, object>(SafeSetValue);
                Dispatcher.Invoke(self, obj, key, value);
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// update language value
        /// </summary>
        /// <param name="value">new value</param>
        public void UpdateValue(object value)
        {
            if (Target != null)
            {
                if (Property is DependencyProperty)
                {
                    DependencyObject   obj  = Target as DependencyObject;
                    DependencyProperty prop = Property as DependencyProperty;

                    void updateAction() => obj.SetValue(prop, GetValue(prop.Name, value));

                    // Check whether the target object can be accessed from the
                    // current thread, and use Dispatcher.Invoke if it can't
                    if (obj == null)
                    {
                        return;
                    }
                    if (obj.CheckAccess())
                    {
                        updateAction();
                    }
                    else
                    {
                        obj.Dispatcher.Invoke(updateAction);
                    }
                }
                else // _targetProperty is PropertyInfo
                {
                    if (Property is PropertyInfo prop)
                    {
                        prop.SetValue(Target, value, null);
                    }
                    else if (Property is MethodInfo method)
                    {
                        method.Invoke(Target, new object[] { Target, value });
                    }
                }
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// AddRefOnChannel
        /// </summary>
        DUCE.ResourceHandle DUCE.IResource.AddRefOnChannel(DUCE.Channel channel)
        {
            // reconsider the need for this lock
            using (CompositionEngineLock.Acquire())
            {
#if DEBUG
                // We assume that a multi-channel resource can only be multi-channel
                // if it is Frozen and does not have animated properties. In this case we know
                // the target resource has at least one animated property so we expect that this
                // independently animated property resource will only be added to the channel
                // associated with the MediaContext associated with the target object's Dispatcher.

                DependencyObject d = (DependencyObject)_dependencyObject.Target;

                // I'm not sure how our target animated DependencyObject would get garbage
                // collected before we call AddRefOnChannel on one of its animated property
                // resources, but if it happens it will be a bad thing.
                Debug.Assert(d != null);

                // Any animated DependencyObject must be associated with a Dispatcher because the
                // AnimationClocks doing the animating must be associated with a Dispatcher.
                Debug.Assert(d.Dispatcher != null);

                // Make sure the target belongs to this thread
                Debug.Assert(d.CheckAccess());
#endif

                if (_duceResource.CreateOrAddRefOnChannel(this, channel, ResourceType))
                {
                    _updateResourceHandler = new MediaContext.ResourcesUpdatedHandler(UpdateResource);

                    UpdateResourceCore(channel);
                }

                return(_duceResource.GetHandle(channel));
            }
        }
Exemplo n.º 20
0
        private void TryRegisterRegion()
        {
            if (visualizingRegion.InnerRegion == null)
            {
                visualizingRegion.InnerRegion = this.Region;

                // Add the AutoPopulateRegionBehavior. This region should be autopopulated, not the inner region, because
                // if you register a viewmodel to a region, it should still be visualized.
                this.autoPopulateRegionBehavior.Region = this.visualizingRegion;
                this.autoPopulateRegionBehavior.Attach();
            }

            DependencyObject targetElement = this.HostControl;

            if (targetElement.CheckAccess())
            {
                IRegionManager regionManager = this.FindRegionManager(targetElement);

                IRegionManager attachedRegionManager = this.GetAttachedRegionManager();

                if (regionManager != attachedRegionManager)
                {
                    if (attachedRegionManager != null)
                    {
                        this.attachedRegionManagerWeakReference = null;
                        attachedRegionManager.Regions.Remove(this.Region.Name);
                    }

                    if (regionManager != null)
                    {
                        this.attachedRegionManagerWeakReference = new WeakReference(regionManager);

                        regionManager.Regions.Add(this.visualizingRegion);
                    }
                }
            }
        }
 /// <summary>
 ///     Gets the parent in the visual or logical tree.
 /// </summary>
 /// <param name="depObj">The dependency object.</param>
 /// <param name="isVisualTree">True for visual tree, false for logical tree.</param>
 /// <returns>The parent, if available.</returns>
 public static DependencyObject GetParent(this DependencyObject depObj, bool isVisualTree)
 {
     return(depObj.CheckAccess()
         ? GetParentInternal(depObj, isVisualTree)
         : depObj.Dispatcher.Invoke(() => GetParentInternal(depObj, isVisualTree)));
 }