I have following sample code
[STAThread]
static void Main(string[] args)
{
Thread thread = new Thread(() =>
{
using (var mww = new Form1())
{
Application.Run(mww);
}
});
thread.Start();
thread.Join();
thread = new Thread(() =>
{
using (var mww = new Form1())
{
Application.Run(mww);
}
});
thread.Start();
thread.Join();
thread = new Thread(() =>
{
using (var mww = new Form1())
{
Application.Run(mww);
}
});
thread.Start();
thread.Join();
}
Which shows Form1 defined as:
public partial class Form1 : Form
{
private readonly Timer _myTimer = new Timer();
public Form1()
{
InitializeComponent();
_myTimer.Interval = 10000;
_myTimer.Tick += TOnTick;
TopMost = true;
}
private void TOnTick(object sender, EventArgs eventArgs)
{
_myTimer.Stop();
Close();
}
private void Form1_Load(object sender, EventArgs e)
{
_myTimer.Start();
}
}
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (_myTimer != null)
{
_myTimer.Dispose();
_myTimer = null;
}
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
}
This code is very simple extraction of my production code, so please do not tell me it is useless.
If I would put all Application.Runs to one thread everything would be working and all three forms would be TopMost. If I run code as it is the first form is shown as TopMost and second and third does not.
BUT: If I close form by red X Close button on shown form (thus Close method in timer's tick eventhandler is not called), next form will be shown as TopMost. It seems to me that there must be some difference between X Close button on form and Close method called in code. But I cannot figure out what is the difference and how to reach wanted behavior: closing by timer, all windows top most.
Thanks for help