Re bonjour,
J'ai dans ma form un bouton pour se connecter a une BD Postgres.
Dans l'évènement click du bouton, je fais appel a un nouveau thread qui lui meme fait appel à une méthode.
Le problème c'est que ça bloque ma form le temps que la connexion se fasse.
Pouvez-vous me dire ce que je fais de mal?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 private void nBtnConnect_Click(object sender, System.EventArgs e) { nWaitingBar.Properties.WaitSize = 12; nWaitingBar.Properties.Step = 5; nWaitingBar.Properties.Text = "Connecting DB"; nWaitingBar.Visible = true; nWaitingBar.BeginWait(); Thread mythread = new Thread(new ThreadStart(this.Connection)); mythread.IsBackground = true; mythread.Start(); } #region connect private void Connection() { log("Connecting to PostgreSQL..."); // Setup the logging NpgsqlEventLog.Level = LogLevel.Normal; NpgsqlEventLog.LogName = "Log.log"; NpgsqlEventLog.EchoMessages = true; if (cnDB != null) { if (cnDB.State != ConnectionState.Closed) { log("Error: Already connected!"); log("Finished connecting!\r\n"); return; } } // Check the data if (txtHostname.Text == "") { log("Error: No hostname was specified!"); log("Finished connecting!\r\n"); return; } if (txtPort.Text == "") { log("Error: No port was specified!"); log("Finished connecting!\r\n"); return; } if (txtUsername.Text == "") { log("Error: No username was specified!"); log("Finished connecting!\r\n"); return; } // Setup a connection string string szConnect = "DATABASE=" + txtDatabase.Text + ";SERVER=" + txtHostname.Text + ";PORT=" + int.Parse(txtPort.Text) + ";UID=" + txtUsername.Text + ";PWD=" + txtPassword.Text + ";"; log("Connection String: " + szConnect); // Attempt to open a connection cnDB = new NpgsqlConnection(szConnect); try { cnDB.Open(); } catch(Exception ex) { log("Error: " + ex.Message + "\r\n" + "StackTrace: \r\n" + ex.StackTrace); log("Finished connecting!\r\n"); return; } // Get the PostgreSQL version number as proof try { NpgsqlCommand cmdVer = new NpgsqlCommand("SELECT version()", cnDB); Object ObjVer = cmdVer.ExecuteScalar(); log(ObjVer.ToString()); } catch(Exception ex) { log("Error: " + ex.Message + "\r\n" + "StackTrace: \r\n" + ex.StackTrace); log("Finished connecting!\r\n"); return; } log("Finished connecting!\r\n"); sbEtat.Text= "Connecté"; nWaitingBar.EndWait(); nWaitingBar.Visible = false;
Partager