Example #1
0
        private bool Execute(string workPath)
        {
            using (source = new CancellationTokenSource())
            {
                using (progressDialog = new UI.ProgressDialog(source))
                {
                    var timeout = plugin.Timeout == 0 ? Plugin.MaxTimeout : plugin.Timeout;

                    progressDialog.SetMaximum(timeout);
                    progressDialog.SetMessage(string.Format(
                                                  Resx.Plugin_Running, plugin.Command, plugin.Arguments, workPath));

                    Process process = null;

                    try
                    {
                        // process should run in an STA thread otherwise it will conflict with
                        // the OneNote MTA thread environment
                        var thread = new Thread(() =>
                        {
                            process = StartPlugin(workPath);
                        });

                        thread.SetApartmentState(ApartmentState.STA);
                        thread.IsBackground = true;
                        thread.Start();

                        progressDialog.StartTimer();
                        var result = progressDialog.ShowDialog(owner);

                        if (result == DialogResult.Cancel)
                        {
                            logger.WriteLine("clicked cancel");
                            process.Kill();
                            return(false);
                        }
                    }
                    catch (Exception exc)
                    {
                        logger.WriteLine("error running Execute(string)", exc);
                    }
                    finally
                    {
                        if (process != null)
                        {
                            process.Dispose();
                            process = null;
                        }
                    }
                }
            }

            return(true);
        }
Example #2
0
        // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
        // Our trusty little worker

        private bool RunBackgroundTask(string path, Action action)
        {
            using (var source = new CancellationTokenSource())
            {
                using (progressDialog = new UI.ProgressDialog(source))
                {
                    progressDialog.SetMaximum(MaxWait);
                    progressDialog.SetMessage($"Importing {path}...");

                    try
                    {
                        // process should run in an STA thread otherwise it will conflict with
                        // the OneNote MTA thread environment
                        var thread = new Thread(() =>
                        {
                            action();
                        });

                        thread.SetApartmentState(ApartmentState.STA);
                        thread.IsBackground = true;
                        thread.Start();

                        progressDialog.StartTimer();
                        var result = progressDialog.ShowDialog(owner);

                        if (result == DialogResult.Cancel)
                        {
                            logger.WriteLine("clicked cancel");
                            thread.Abort();
                            return(false);
                        }
                    }
                    catch (Exception exc)
                    {
                        logger.WriteLine("error importing", exc);
                    }
                }
            }

            return(true);
        }