Skip to content

Commit 1ae2b0a

Browse files
committed
Fixed: control Invoke issue.
1 parent 9f10c4a commit 1ae2b0a

1 file changed

Lines changed: 48 additions & 38 deletions

File tree

TemplateCodeGeneratorPlugin/Control/XrmMockGeneratorPluginControl.cs

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@ public partial class PluginControl : PluginControlBase, IStatusBarMessenger
8585

8686
private ToolStripButton buttonOptions;
8787
private Panel panelLinks;
88-
private LinkLabel labelLinkSeparator;
8988
private LinkLabel linkQuickGuide;
9089
private Panel panelToast;
9190
private Label labelToast;
9291
private ToolStripButton buttonHistory;
92+
private ToolStripButton buttonDefaultT4;
93+
private ToolStripLabel labelYagasoft;
94+
private Label labelSeparator1;
9395

9496
// load T4 Generator required assemblies
9597
private RefreshMode dummy1 = RefreshMode.KeepChanges;
@@ -104,8 +106,6 @@ public partial class PluginControl : PluginControlBase, IStatusBarMessenger
104106
private readonly WorkerHelper workerHelper;
105107
private readonly ConnectionManager connectionManager;
106108
private readonly FileHelper fileHelper;
107-
private ToolStripButton buttonDefaultT4;
108-
private ToolStripLabel labelYagasoft;
109109
private readonly UiHelper uiHelper;
110110

111111
#region Base tool implementation
@@ -117,8 +117,8 @@ public PluginControl()
117117
ShowReleaseNotes();
118118

119119
workerHelper = new WorkerHelper(
120-
(s, work, callback) => Invoke(new Action(() => RunAsync(s, work, callback))),
121-
(s, work, callback) => Invoke(new Action(() => RunAsync(s, work, callback))));
120+
(s, work, callback) => InvokeSafe(() => RunAsync(s, work, callback)),
121+
(s, work, callback) => InvokeSafe(() => RunAsync(s, work, callback)));
122122
connectionManager = new ConnectionManager(() => Service);
123123
fileHelper = new FileHelper(pluginSettings,
124124
(filetype, savedFileGroup) =>
@@ -147,7 +147,19 @@ public PluginControl()
147147
uiHelper.ShowToast($"{filetype} saved.");
148148
});
149149
templateViewModel = new TemplateViewModel();
150-
uiHelper = new UiHelper(panelToast, labelToast, action => Invoke(action));
150+
uiHelper = new UiHelper(panelToast, labelToast, InvokeSafe);
151+
}
152+
153+
private void InvokeSafe(Action action)
154+
{
155+
if (IsHandleCreated)
156+
{
157+
Invoke(action);
158+
}
159+
else
160+
{
161+
action();
162+
}
151163
}
152164

153165
private void LoadPluginSettings()
@@ -274,6 +286,7 @@ private void InitializeComponent()
274286
this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
275287
this.buttonClearCache = new System.Windows.Forms.ToolStripButton();
276288
this.toolStripSeparator5 = new System.Windows.Forms.ToolStripSeparator();
289+
this.labelYagasoft = new System.Windows.Forms.ToolStripLabel();
277290
this.linkDownVs = new System.Windows.Forms.LinkLabel();
278291
this.buttonCancel = new System.Windows.Forms.Button();
279292
this.tableLayoutMainPanel = new System.Windows.Forms.TableLayoutPanel();
@@ -283,12 +296,11 @@ private void InitializeComponent()
283296
this.labelNamespace = new System.Windows.Forms.Label();
284297
this.textBoxNamespace = new System.Windows.Forms.TextBox();
285298
this.panelLinks = new System.Windows.Forms.Panel();
286-
this.labelLinkSeparator = new System.Windows.Forms.LinkLabel();
287299
this.linkQuickGuide = new System.Windows.Forms.LinkLabel();
288300
this.panelHost = new System.Windows.Forms.Panel();
289301
this.panelToast = new System.Windows.Forms.Panel();
290302
this.labelToast = new System.Windows.Forms.Label();
291-
this.labelYagasoft = new System.Windows.Forms.ToolStripLabel();
303+
this.labelSeparator1 = new System.Windows.Forms.Label();
292304
this.toolBar.SuspendLayout();
293305
this.tableLayoutMainPanel.SuspendLayout();
294306
this.tableLayoutPanel2.SuspendLayout();
@@ -460,6 +472,19 @@ private void InitializeComponent()
460472
this.toolStripSeparator5.Name = "toolStripSeparator5";
461473
this.toolStripSeparator5.Size = new System.Drawing.Size(6, 25);
462474
//
475+
// labelYagasoft
476+
//
477+
this.labelYagasoft.Font = new System.Drawing.Font("Verdana", 8F, System.Drawing.FontStyle.Bold);
478+
this.labelYagasoft.ForeColor = System.Drawing.Color.DarkViolet;
479+
this.labelYagasoft.IsLink = true;
480+
this.labelYagasoft.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
481+
this.labelYagasoft.LinkColor = System.Drawing.Color.DarkViolet;
482+
this.labelYagasoft.Name = "labelYagasoft";
483+
this.labelYagasoft.Size = new System.Drawing.Size(95, 22);
484+
this.labelYagasoft.Text = "Yagasoft.com";
485+
this.labelYagasoft.VisitedLinkColor = System.Drawing.Color.DarkBlue;
486+
this.labelYagasoft.Click += new System.EventHandler(this.labelYagasoft_Click);
487+
//
463488
// linkDownVs
464489
//
465490
this.linkDownVs.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
@@ -567,7 +592,7 @@ private void InitializeComponent()
567592
//
568593
// panelLinks
569594
//
570-
this.panelLinks.Controls.Add(this.labelLinkSeparator);
595+
this.panelLinks.Controls.Add(this.labelSeparator1);
571596
this.panelLinks.Controls.Add(this.linkQuickGuide);
572597
this.panelLinks.Controls.Add(this.linkDownVs);
573598
this.panelLinks.Dock = System.Windows.Forms.DockStyle.Fill;
@@ -576,19 +601,6 @@ private void InitializeComponent()
576601
this.panelLinks.Size = new System.Drawing.Size(214, 20);
577602
this.panelLinks.TabIndex = 2;
578603
//
579-
// labelLinkSeparator
580-
//
581-
this.labelLinkSeparator.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
582-
| System.Windows.Forms.AnchorStyles.Right)));
583-
this.labelLinkSeparator.AutoSize = true;
584-
this.labelLinkSeparator.Location = new System.Drawing.Point(75, 3);
585-
this.labelLinkSeparator.Name = "labelLinkSeparator";
586-
this.labelLinkSeparator.Size = new System.Drawing.Size(9, 13);
587-
this.labelLinkSeparator.TabIndex = 22;
588-
this.labelLinkSeparator.TabStop = true;
589-
this.labelLinkSeparator.Text = "|";
590-
this.labelLinkSeparator.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
591-
//
592604
// linkQuickGuide
593605
//
594606
this.linkQuickGuide.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
@@ -639,18 +651,16 @@ private void InitializeComponent()
639651
this.labelToast.Text = "<toast>";
640652
this.labelToast.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
641653
//
642-
// labelYagasoft
654+
// labelSeparator1
643655
//
644-
this.labelYagasoft.Font = new System.Drawing.Font("Verdana", 8F, System.Drawing.FontStyle.Bold);
645-
this.labelYagasoft.ForeColor = System.Drawing.Color.DarkViolet;
646-
this.labelYagasoft.IsLink = true;
647-
this.labelYagasoft.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
648-
this.labelYagasoft.LinkColor = System.Drawing.Color.DarkViolet;
649-
this.labelYagasoft.Name = "labelYagasoft";
650-
this.labelYagasoft.Size = new System.Drawing.Size(95, 22);
651-
this.labelYagasoft.Text = "Yagasoft.com";
652-
this.labelYagasoft.VisitedLinkColor = System.Drawing.Color.DarkBlue;
653-
this.labelYagasoft.Click += new System.EventHandler(this.labelYagasoft_Click);
656+
this.labelSeparator1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
657+
| System.Windows.Forms.AnchorStyles.Right)));
658+
this.labelSeparator1.AutoSize = true;
659+
this.labelSeparator1.Location = new System.Drawing.Point(75, 3);
660+
this.labelSeparator1.Name = "labelSeparator1";
661+
this.labelSeparator1.Size = new System.Drawing.Size(9, 13);
662+
this.labelSeparator1.TabIndex = 0;
663+
this.labelSeparator1.Text = "|";
654664
//
655665
// PluginControl
656666
//
@@ -1183,7 +1193,7 @@ private void GenerateCode()
11831193
}
11841194
finally
11851195
{
1186-
Invoke(new Action(() => buttonCancel.Hide()));
1196+
InvokeSafe(() => buttonCancel.Hide());
11871197
EnableTool();
11881198
}
11891199
},
@@ -1222,22 +1232,22 @@ private void GenerateCode()
12221232

12231233
private void EnableTool()
12241234
{
1225-
Invoke(new Action(
1235+
InvokeSafe(
12261236
() =>
12271237
{
12281238
tableLayoutMainPanel.Enabled = true;
12291239
toolBar.Enabled = true;
1230-
}));
1240+
});
12311241
}
12321242

12331243
private void DisableTool()
12341244
{
1235-
Invoke(new Action(
1245+
InvokeSafe(
12361246
() =>
12371247
{
12381248
toolBar.Enabled = false;
12391249
tableLayoutMainPanel.Enabled = false;
1240-
}));
1250+
});
12411251
}
12421252

12431253
private void UpdateFilePathsUi()

0 commit comments

Comments
 (0)