如何在线程后向表单添加新面板? [英] How to add new panel to a form after threading?

查看:60
本文介绍了如何在线程后向表单添加新面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用线程方法生成sql命令

以防止我的主应用程序冻结

但是当我尝试添加一个新面板后成功连接到服务器

但我触发了一个错误:

跨线程操作无效..



i阅读几篇文章来自谷歌很多人说这不是使用线程添加任何用户控件的最佳方式



所以我尝试了另一种方法

第一个中止我的线程,你可以在openConnection,LoadConnection.Abort();推荐。

我中止后我尝试了这种方法

 if(loadConnection.IsAlive){} 
else {

if(connectionStatus ==connectionSucceed){}
else if(connectionStatus ==connectionFail){
var panel = new Panel(){new size(100,100),location (0,0);}
}
}





但这次没有任何反应。



是否有任何方法可以在运行sql命令时将任何用户控件添加到主窗体而不冻结UI?



我尝试过:



使用System.Threading; 
使用MySql.Data.MySqlClient;

namespace databaseCommand {
public static class mainCommand {
private static Thread loadConnection = new Thread(new ThreadStart(openConnection));
private static MySqlConnection con;
private static string connectionStatus;

public static void startConnection(){
loadConnection.Start();
}

private static void openConnection(){
con = new MySqlConnection(server = localhost; user = root; database = bookbardatabase; port = 3306; password =) ;
try {
con.Open();
// connectionStatus =connectionSucceed;
// conStatus =连接到服务器;
}
catch(Exception ex){
// connectionStatus =connectionFail;
// conStatus = ex.ToString();
var panel = new Panel(){new size(100,100),location(0,0);}
}
//loadConnection.Abort();
}
}
}

解决方案

你不能做任何UI的东西,包括做一个新的面板并将其添加到窗体,从UI(启动)线程以外的任何其他线程。



你的方法做了不止一件事,这很糟糕。您的代码正在进行数据库工作,然后您也尝试将UI内容投入到混合中。别。此代码应返回从数据库中检索的任何内容或异常。这就是全部,没有别的。



由调用代码决定如何处理结果,显示它,显示错误信息等等。

i am using threading method in generating the sql command
to prevent my main application getting freeze
but when i try to add a new panel after sucessfully connected with the server
but i triggered an error :
cross-thread operation not valid..

i read several article from google a lot of people said that it is not a best way to add any user control with using threading

so i tried another method
1st abort my thread as u can see at openConnection, LoadConnection.Abort(); is commend.
after i abort it i tried this method

if (loadConnection.IsAlive) {  }
            else{
                
                if (connectionStatus == "connectionSucceed") { }
                else if (connectionStatus == "connectionFail") {
                    var panel = new Panel(){ new size(100,100), location (0,0);}
                }
            }



but this time nothing happen.

is there any way to add any user control to the main form while running the sql command without freezing the UI??

What I have tried:

using System.Threading;
using MySql.Data.MySqlClient;

namespace databaseCommand{
    public static class mainCommand {
        private static Thread loadConnection = new Thread(new ThreadStart(openConnection));
        private static MySqlConnection con;
        private static string connectionStatus;

        public static void startConnection() {
            loadConnection.Start();
        }

        private static void openConnection() {
            con = new MySqlConnection("server=localhost;user=root;database=bookbardatabase;port=3306;password=");
            try{
                con.Open();
                //connectionStatus = "connectionSucceed";
                //conStatus = "Connected to server";
            }
            catch (Exception ex){
                //connectionStatus = "connectionFail";
                //conStatus = ex.ToString();
                var panel = new Panel(){ new size(100,100), location (0,0);}
            }
            //loadConnection.Abort();
        }
    }
}

解决方案

You cant do any UI stuff, including making a new Panel and adding it to the Form, from any other thread other the the UI (startup) thread.

You method is doing more than one thing and that's bad. Your code is doing database work and then you're trying to throw UI stuff into the mix too. Don't. This code should either return whatever your retrieving from the database or an exception. That's all, nothing else.

It's up to the calling code to figure out what to do with the result, either display it, show an error message, or whatever.


这篇关于如何在线程后向表单添加新面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆