I have buggy code and I don’t know where or what is the fault.
I am writing an application for a customer. During the splash screen the app checks if Mysql is running to be able to connect to it later. If mysql is on, the app continues booting. If mysql is not running I start it and re-check again.
Initially, I was finding the mysql pid, but for some reason I can’t get it without using unmanaged code.
So I found another way in xampp source code and I decided to use it.
Running the app under vstudio debugger stepping or stopping in a breakpoint works perfectly, but if run without stepping, the app boots mysql server but can´t detect it and eventually the application stop (I don’t want the app continue loading if database fails).
This behavior has led me to think that one or more variables are disposed at runtime before I can use it again.
I don’t know how solve this issue and need a hand.
EDIT: The issue was mysql was not started yet running the app without interruption
As Jan & SWEKO said. Adding some delay works perfectly
The splash screen Load code.
// App booting tasks
void Load()
{
// License validation ok
Boolean licensecheck = BootChecks.LicenseCheck();
if (! licensecheck)
{
MessageBox.Show("License Error Conta ct Technical Support","Error",MessageBoxButtons.OK,MessageBoxIcon.Stop);
Application.Exit();
}
bar.Width += 10; // feed progress bar ( it's a custom drawn label )
this.Refresh(); // update form to paint it
Application.DoEvents(); // process all pooled messages
// Mysql check
Boolean dbcheck = BootChecks.DbCheck();
// THE ISSUE IT'S HERE RUNNING WITHOUT STEPPING , ALWAYS FIRES THE ERROR
if ( ! dbcheck )
{
MessageBox.Show("Can't init Mysql. Contact Technical Support", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}
bar.Width += 10;
this.Refresh();
Application.DoEvents();
// more stuff
}
Database check code
public static bool DbCheck()
{
Boolean norun = MysqlController.CheckMysqlIsRunning(Constants.dbpidfile);
// check if running
if ( norun ) return true;
// if not running, I start it
MysqlController.StartMysql(Constants.dbexe,Constants.dbopts);
Boolean rerun = MysqlController.CheckMysqlIsRunning(Constants.dbpidfile);
// if really running all it's ok
if ( rerun ) return true;
// really a fault
return false;
}
Real mysql check
comment: this way was found on xampp package. To know if mysql it’s running find mysql pid file, and open a named event. if right definitively mysql it’s running.
public static Boolean CheckMysqlIsRunning(String pathtopidfile)
{
try
{
Int32 pid;
using ( StreamReader sr = new StreamReader(pathtopidfile) )
{
pid = int.Parse(sr.ReadToEnd());
}
IntPtr chk = OpenEvent(SyncObjectAccess.EVENT_MODIFY_STATE, false,
String.Format("MySQLShutdown{0}", pid.ToString()));
if ( chk != null ) return true;
return false;
}
catch (Exception) { return false;}
}
public static void StartMysql(String path, String aargs)
{
// All ok. Simply spawn a process
}
You have to code some kind of loop. You start the mysql process and when you check immediately after is, the process is still not there and you return false.
Try something like this: