コード例 #1
0
        public Result AddService(ServiceModel service)
        {
            if (string.IsNullOrEmpty(service.Name))
            {
                return(Result.Fail($"The property 'Name' is required."));
            }
            if (string.IsNullOrEmpty(service.DisplayName))
            {
                return(Result.Fail($"The property 'DisplayName' is required."));
            }
            if (string.IsNullOrEmpty(service.Filename))
            {
                return(Result.Fail($"The property 'Filename' is required."));
            }

            var tabPages = tabControl.TabPages.Cast <TabPage>();

            if (tabPages.Any(x => x.Name == service.Name))
            {
                return(Result.Fail($"There is already a service registered with the name '{service.Name}'"));
            }

            var serviceExecutionModel = new ServiceExecutionModel(service);

            var tab = CreateTab(
                service.Name,
                service.DisplayName,
                serviceExecutionModel.Logger,
                serviceExecutionModel
                );

            ModifyUI(() =>
            {
                tabControl.TabPages.Add(tab);
            });

            serviceExecutionModel.Start();

            try
            {
                OnServiceAdded?.Invoke(this, new EventArgs <ServiceModel>(service));
            }
            catch (Exception ex)
            {
                mainLogger.Error(ex.ToString());
            }
            return(Result.Successful());
        }
コード例 #2
0
        private TabPage CreateTab(string name, string displayName, Logger logger, ServiceExecutionModel serviceExecutionModel = null)
        {
            var tabPage = new TabPage(displayName)
            {
                Name = name,
                Tag  = serviceExecutionModel
            };

            var richTextBox = new RichTextBox()
            {
                Font       = new Font("Lucida Console", 10),
                ForeColor  = Color.LightGray,
                BackColor  = Color.Black,
                Dock       = DockStyle.Fill,
                ReadOnly   = true,
                WordWrap   = false,
                ScrollBars = RichTextBoxScrollBars.Both,
                DetectUrls = true
            };

            richTextBox.LinkClicked += (s, e) => { Process.Start(e.LinkText); };

            logger.OnMessage += (s, e) =>
            {
                if (richTextBox.IsDisposed)
                {
                    return;
                }
                ModifyUI(() => WriteMessageToRichTextBox(richTextBox,
                                                         e.Message,
                                                         e.Level
                                                         ));
            };

            tabPage.Controls.Add(richTextBox);
            return(tabPage);
        }