xenadmin/XenAdmin/Dialogs/MessageBoxTest.cs
Gabor Apati-Nagy 92f0499911 CA-149867: Invoke on MainWindow instead of various controls
We observed that some threads can reach deadlock-ish state after they have Invoked into a control's UI thread. When it happens they are all in a waiting for join or in sleep state for very long time, although there should not be any deadlock situations.
It seems this has something to do with multiple parent controls and with which control we invoked on. This should not make a difference, because we have got one UI thread (for MainWindow) they should wait for, but we have seen it does.

The solution that fixed this issue was to invoke on the MainWindow instead of various controls (see a4fe507adf ).

This changeset is changing all our Invokes to invoke into MainWindow
instead of a control itself. (MainWindow's UI thread is the only UI thread
all Control is using in XenCenter)
This changeset should be in place until we have found the root cause or the exact reason for the above.
2015-04-08 15:16:10 +01:00

128 lines
4.8 KiB
C#

/* Copyright (c) Citrix Systems Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace XenAdmin.Dialogs
{
public partial class MessageBoxTest : XenDialogBase
{
private readonly System.Threading.Timer timer;
private readonly List<TextBox> argBoxes = new List<TextBox>();
private ThreeButtonDialog tbd = null;
public MessageBoxTest()
{
timer = new System.Threading.Timer(refreshTick, null, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
InitializeComponent();
argBoxes.Add(Parameter1Box);
argBoxes.Add(Parameter2Box);
argBoxes.Add(Parameter3Box);
Button1Box.Text = "OK";
Button2Box.Text = "Cancel";
textBox1_TextChanged(null, null);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
timer.Change(500, System.Threading.Timeout.Infinite);
Parameter1Box.Enabled = FormatBox.Text.Contains("{0}");
Parameter2Box.Enabled = FormatBox.Text.Contains("{1}");
Parameter3Box.Enabled = FormatBox.Text.Contains("{2}");
}
private void refreshTick(object state)
{
timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
Program.Invoke(Program.MainWindow, refreshTick_);
}
private void refreshTick_()
{
Point loc = tbd == null ? new Point(100, 100) : tbd.Location;
List<string> args = new List<string>();
foreach (TextBox tb in argBoxes)
{
if (tb.Enabled)
args.Add(tb.Text);
}
string msg;
try
{
msg = string.Format(FormatBox.Text, args.ToArray());
}
catch (FormatException)
{
msg = FormatBox.Text;
}
bool two = Button3Box.Text == "";
ThreeButtonDialog d;
if (two)
{
d = new ThreeButtonDialog(
new ThreeButtonDialog.Details(SystemIcons.Information, msg),
new ThreeButtonDialog.TBDButton(Button1Box.Text, DialogResult.OK, ThreeButtonDialog.ButtonType.ACCEPT),
new ThreeButtonDialog.TBDButton(Button2Box.Text, DialogResult.OK, ThreeButtonDialog.ButtonType.CANCEL));
}
else
{
d = new ThreeButtonDialog(
new ThreeButtonDialog.Details(SystemIcons.Information, msg),
new ThreeButtonDialog.TBDButton(Button1Box.Text, DialogResult.OK, ThreeButtonDialog.ButtonType.NONE),
new ThreeButtonDialog.TBDButton(Button2Box.Text, DialogResult.OK, ThreeButtonDialog.ButtonType.ACCEPT),
new ThreeButtonDialog.TBDButton(Button3Box.Text, DialogResult.OK, ThreeButtonDialog.ButtonType.CANCEL));
}
d.Show();
d.Location = loc;
Focus();
BringToFront();
if (tbd != null)
tbd.Close();
tbd = d;
}
}
}