Added code to improve how the ROEditor responds when a 2nd instance is opened.

This commit is contained in:
Rich 2015-02-27 03:51:53 +00:00
parent 17a72a4dd7
commit f73ab7c3a7

View File

@ -299,6 +299,7 @@ using ctlXMLEditLib;
using RODBInterface;
using ROFields;
using Org.Mentalis.Files;
using System.Runtime.InteropServices;
//using IniFileIO;
@ -431,7 +432,6 @@ namespace ROEditor
duplicate_active = false;
if (specificro != null) DoSpecificRO(specificro);
}
private void DoSpecificRO(string specificro)
{
// if a specific RO was passed in. Load the xml tree from it to the root & then
@ -1458,9 +1458,28 @@ namespace ROEditor
}
#endregion
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// The main entry point for the application.
/// </summary>
static void BringWindowToFront()
{
Process currentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
Process process = null;
for (int i = 0; i < processes.Length; i++)
{
if (processes[i].SessionId != currentProcess.SessionId)
{
process = processes[i];
break;
}
}
if (process == null) return;
SetForegroundWindow(process.MainWindowHandle);
}
[STAThread]
static void Main(string[] args)
{
@ -1528,6 +1547,15 @@ namespace ROEditor
try
{
fiown = new FileInfo("RoEditor.own");
fsown = fiown.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
TextReader tr = new StreamReader(fsown);
string who = tr.ReadToEnd();
tr.Close();
if (who.Contains(Environment.UserName.ToUpper()))
{
BringWindowToFront();
return;
}
}
catch(Exception e)
{
@ -1536,11 +1564,34 @@ namespace ROEditor
// Open the file just specified. Open it so that no-one else can use it
try
{
fsown = fiown.Open( FileMode.Create, FileAccess.ReadWrite, FileShare.None );
fsown = fiown.Open( FileMode.Create, FileAccess.ReadWrite, FileShare.Read );
TextWriter tw = new StreamWriter(fsown);
tw.WriteLine("Current User: {0}, Date and Time Started: {1}", Environment.UserName.ToUpper(), DateTime.Now.ToString("MM/dd/yyyy @ hh:mm"));
tw.Flush();
}
catch (IOException)
catch (IOException ex)
{
MessageBox.Show("Another user is executing the RoEditor");
fsown = fiown.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
TextReader tr = new StreamReader(fsown);
string who = tr.ReadToEnd();
tr.Close();
if(who.Contains(Environment.UserName.ToUpper()))
{
BringWindowToFront();
//Process[] p = Process.GetProcessesByName("ROEditor");
//Process cp = Process.GetCurrentProcess();
//for (int i = 0; i < p.Length; i++)
//{
// if (p[i].SessionId != cp.SessionId)
// {
// SetForegroundWindow(p[i].MainWindowHandle);
// break;
// }
//}
}
else
MessageBox.Show(who, "Another user is executing the RoEditor");
return;
}