从子线程

时间:2016-07-27 13:50:18

标签: c# multithreading

我现在已经在这个问题上摸不着头脑了,尽管我在寻找解决方案,但我还是不太了解这些实现(已经看过堆栈溢出的几个答案)

我的程序在打开时会加载一个启动页面,在此期间它会检查数据库连接。如果存在连接,则会关闭启动页面并加载主窗体,否则它会提供错误消息,然后完全关闭。

public partial class StartupSplash : Form
{

    ThreadStart th;
    Thread thread;

    public StartupSplash()
    {
        InitializeComponent();

        th = new ThreadStart(DbAvaliable);
        thread = new Thread(th);
        thread.Start();

    }

    private void DbAvaliable()
    {

        Boolean result = false;

        using (var connectiontest = new SqlConnection("myConnString"))
            try
            {
                connectiontest.Open();
                result = true;
            }
            catch (Exception ex)
            {
                result = false;
            }

        if (result)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainWindow());

        }
        else
        {
            MessageBox.Show("Unable to establish database connection. Please check your data connection and try again.");
        }
    }
}

据我所知,由于跨线程问题,我无法简单地调用this.Close()。我已经阅读了一些关于调用方法的内容,但是我不太清楚如何实现上面的结果。

最初我尝试使用表单加载/显示的事件而不是替代线程,但是表单上的图像无法加载,直到消息框显示错误(而不是显示,然后运行连接检查)

2 个答案:

答案 0 :(得分:0)

您是否可以使用db检查结果设置要在Form2上触发的事件?订阅Form1上的活动,并在条件允许时告诉它关闭。

不确定它是否有效,但是类似于:

public Form2 : Form
{
    public delegate void DbCheckHandler(object sender, DbEventArgs e);
    public event DbCheckHandler DbCheckComplete;

    //check the db
    DbCheckComplete(this, new DbEventArgs { ShouldClose = true; });

}

public Form1 : Form
{
    Form2 form2 = new Form2();
    form2.DbCheckComplete += new DbCheckHandler(CheckDbResult);
    form2.Show();

    private void CheckDbResult(object sender, DbEventArgs e)
    {
        if(e.ShouldClose)
        {
            this.Close();
        }

    }
}

答案 1 :(得分:0)

Hans Passantherehere)发布的先前答案的帮助下,我的解决方案如下:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {   
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        new MyApp().Run(args);

    }

class MyApp : WindowsFormsApplicationBase
{
    protected override void OnCreateSplashScreen()
    {
        this.SplashScreen = new StartupSplash();
    }

    protected override void OnCreateMainForm()
    {
        Boolean result = false;
        using (var connectiontest = new SqlConnection("myConnectionString"))
            try
            {
                connectiontest.Open();
                result = true;
            }
            catch (Exception ex)
            {
                result = false;
            }
        // pause not needed while checking for db connection as that takes its own amount of time to complete.


        if (result)
        {
            System.Threading.Thread.Sleep(3000); //pause moved here to give the splash some time on screen if db connection available
            this.MainForm = new MainWindow();
        }
        else
        {
            MessageBox.Show("Unable to connect to the database");

            Environment.Exit(1); // shuts down the program if database connection not avaliable.

        }

    }


}
相关问题