示例#1
0
 /// <summary>
 /// Adds a control (Windows <see cref="System.Windows.Forms.UserControl"/>) to the taskpane
 /// that has been exposed to COM and has a given ProgId
 /// </summary>
 /// <typeparam name="T">The type of UserControl being created</typeparam>
 /// <param name="progId">The [ProgId()] attribute value adorned on the UserControl class</param>
 /// <param name="licenseKey">The license key (for specific SolidWorks add-in types)</param>
 /// <returns></returns>
 public async Task <T> AddControlAsync <T>(string progId, string licenseKey)
 {
     // Wrap any error creating the taskpane in a SolidDna exception
     return(SolidDnaErrors.Wrap <T>(() =>
     {
         // Attempt to create the taskpane
         return (T)BaseObject.AddControl(progId, licenseKey);
     },
                                    SolidDnaErrorTypeCode.SolidWorksTaskpane,
                                    SolidDnaErrorCode.SolidWorksTaskpaneAddControlError,
                                    await Localization.GetStringAsync("ErrorSolidWorksTaskpaneAddControlError")));
 }
        /// <summary>
        /// Attempts to create
        /// </summary>
        /// <param name="iconPath">An absolute path to an icon to use for the taskpane (ideally 37x37px)</param>
        /// <param name="toolTip">The title text to show at the top of the taskpane</param>
        public async Task <Taskpane> CreateTaskpaneAsync(string iconPath, string toolTip)
        {
            // Wrap any error creating the taskpane in a SolidDna exception
            return(SolidDnaErrors.Wrap <Taskpane>(() =>
            {
                // Attempt to create the taskpane
                var comTaskpane = mBaseObject.CreateTaskpaneView2(iconPath, toolTip);

                // If we fail, return null
                if (comTaskpane == null)
                {
                    return null;
                }

                // If we succeed, create SolidDna object
                return new Taskpane(comTaskpane);
            },
                                                  SolidDnaErrorTypeCode.SolidWorksTaskpane,
                                                  SolidDnaErrorCode.SolidWorksTaskpaneCreateError,
                                                  await Localization.GetStringAsync("ErrorSolidWorksTaskpaneCreateError")));
        }
        /// <summary>
        /// Runs the specified action on the UI thread
        ///
        /// IMPORTANT: Make sure the caller (doing the await RunOnUIThreadAwait)
        /// does not await anything inside the action that can be locked by the
        /// parent scope (the scope of the await itself) otherwise a deadlock
        /// could occur
        ///
        /// NOTE: Only possible right now if the application is making use of TaskpaneIntegration as the
        /// UI dispatcher is via the Taskpane control on the UI thread
        ///
        /// TODO: Find a better way to access an invoker/dispatcher without needing a Taskpane
        /// </summary>
        /// <param name="action">The action to run</param>
        public static async Task RunOnUIThreadAsync(Action action)
        {
            // If we are on the UI thread already, just run the action
            if (mInvoker == null)
            {
                throw new ArgumentNullException(await Localization.GetStringAsync("ErrorThreadInvokerNullError"));
            }

            // If we are already on the UI thread, just run
            if (!mInvoker.InvokeRequired)
            {
                action();
            }
            else
            {
                // We are not on the UI thread, so it is safe to
                // await a task that will run on the UI thread
                // as it will not deadlock this thread
                var tcs = new TaskCompletionSource <bool>();

                // Invoke action
                mInvoker.Invoke((MethodInvoker) delegate
                {
                    try
                    {
                        action();
                    }
                    finally
                    {
                        tcs.TrySetResult(true);
                    }
                });

                // Wait for completion
                await tcs.Task;
            }
        }