1

我有错误的代码,我不知道哪里或什么是错误的。

我正在为客户编写应用程序。在启动屏幕期间,应用程序会检查 Mysql 是否正在运行以便以后能够连接到它。如果 mysql 已打开,应用程序将继续启动。如果 mysql 没有运行,我启动它并重新检查。

最初,我正在寻找 mysql pid,但由于某种原因,如果不使用非托管代码,我就无法获得它。

所以我在xampp源代码中找到了另一种方式,我决定使用它。

在 vstudio 调试器下运行应用程序步进或在断点处停止运行完美,但如果不步进运行,应用程序会启动 mysql 服务器但无法检测到它并最终应用程序停止(如果数据库失败,我不希望应用程序继续加载)。

这种行为使我认为一个或多个变量在我可以再次使用之前在运行时被释放。

我不知道如何解决这个问题,需要帮助。

编辑:问题是 mysql 还没有启动,还没有中断运行应用程序正如 Jan & SWEKO 所说。添加一些延迟效果很好

初始屏幕加载代码。

// 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

}

数据库校验码

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;

}

真正的mysql检查

评论:这种方式是在 xampp 包上找到的。要知道 mysql 是否正在运行,请查找 mysql pid 文件,并打开一个命名事件。如果绝对正确,mysql它正在运行。

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
}
4

3 回答 3

3

你必须编写某种循环。您启动 mysql 进程,当您立即检查时,该进程仍然不存在并且您返回 false。

尝试这样的事情:

// if not running, I start it
MysqlController.StartMysql(Constants.dbexe,Constants.dbopts);
// wait 10 seconds max.
int timeout = 10;
bool mySqlIsRunning = false;

while(!MysqlController.CheckMysqlIsRunning(Constants.dbpidfile)) {
    // wait a while
    Thread.Sleep(1000);
    if (timeout-- == 0) {
        // timeout error
        // show message to user ...
    }
}

// here mysql is running or your timeout is expired.
于 2012-11-09T12:34:38.820 回答
1

我认为问题可能是您在运行检查时生成了一个未完成的后台进程。

当你在调试时,MySQL 有足够的时间在你从它开始的时间里启动

MysqlController.StartMysql(Constants.dbexe,Constants.dbopts);

Boolean rerun = MysqlController.CheckMysqlIsRunning(Constants.dbpidfile);

但是,当您真正运行应用程序时,情况可能并非如此。作为一个简单的测试,尝试添加一个简单Thread.Sleep(2000)的等待几秒钟,然后再次运行检查。

于 2012-11-09T12:36:11.690 回答
0

恕我直言,它的错误形式是有一个方法说它会做一个检查,然后也做一个初始化。将检查的代码分成两部分。然后在检查后在主代码中进行初始化。通过将它们作为属性保存在表单上来保留对数据库的引用。

于 2012-11-09T12:35:27.677 回答