Application qui freeze quand elle envoit un login au pop3

warenbe Messages postés 145 Date d'inscription dimanche 2 décembre 2001 Statut Membre Dernière intervention 10 septembre 2009 - 2 août 2005 à 21:25
warenbe Messages postés 145 Date d'inscription dimanche 2 décembre 2001 Statut Membre Dernière intervention 10 septembre 2009 - 3 août 2005 à 14:39
bonsoir
j'ai fais un petit bout de code qui me permet de verifier si j'ai des mails ou pas
ce n'est pas terminé mais l'application freeze quand j'essaye d'envoyer le login
voici le code :


<HR>

using System;


using System.Drawing;


using System.Collections;


using System.ComponentModel;


using System.Windows.Forms;


using System.Data;


using System.Net.Sockets;


using System.Net;


using System.IO;


using System.Text;


namespace popverif


{



/// <summary>



/// Summary description for Form1.



/// </summary>



public
class Form1 : System.Windows.Forms.Form


{



private System.Windows.Forms.GroupBox groupBox1;



private System.Windows.Forms.GroupBox groupBox2;



private System.Windows.Forms.GroupBox groupBox3;



private System.Windows.Forms.GroupBox groupBox4;



private System.Windows.Forms.GroupBox groupBox5;



private System.Windows.Forms.Button bconnect;



private System.Windows.Forms.Button boptions;



private System.Windows.Forms.Button bquitter;



private System.Windows.Forms.TextBox tbserveur;



private System.Windows.Forms.TextBox tblogin;



private System.Windows.Forms.TextBox tbpassword;



private System.Windows.Forms.TextBox tbstatut;



private System.Windows.Forms.TextBox tbport;



/// <summary>



/// Required designer variable.



/// </summary>



private System.ComponentModel.Container components =
null;



public Form1()


{



//



// Required for Windows Form Designer support



//


InitializeComponent();



//on change les textes des textboxs


tbserveur.Text = "entrez ici le serveur pop (pop.serveur.com)";


tblogin.Text = "entrez ici le login";


tbpassword.Text = "mot de passe";


tbport.Text = "110";



//on gere quand l'utilisateur clique sur quitter


bquitter.Click +=
new EventHandler(bquitter_Click);



//on gere quand l'utilisateur clique sur connecter


bconnect.Click +=
new EventHandler(bconnect_Click);


}



//pour quitter



void bquitter_Click(
object sender, System.EventArgs e)


{


Application.Exit();


}



//pour se connecter au serveur



void bconnect_Click(
object sender, System.EventArgs e)


{



//on commence par recuperer les variables serveur port etc



string serveur = tbserveur.Text;



string login = tblogin.Text;



string password = tbpassword.Text;



int port =
int.Parse(tbport.Text);



//on met a jour la zone de statut


tbstatut.Text = "connection à " +serveur +" sur le port " +port;



//on etablit une connection au serveur


TcpClient client =
new TcpClient(serveur, port);


tbstatut.Text = "connection etablie";



//on fait un stream


NetworkStream nstream = client.GetStream();



//on attend la reponse



//si le client s'est bien connecté on reçoit +ok



string reponse = rep(client,nstream);



if (!reponse.StartsWith("+OK"))


{


MessageBox.Show("erreur de connection");


Application.Exit();


}



//on envoi l'identification



string user = "USER "+login;


envoimessage(nstream, user);



//on ecoute la reponse...


reponse = rep(client,nstream);



if (!reponse.StartsWith("+OK"))


{


MessageBox.Show("erreur!");


Application.Exit();


}



//on envoi le mot de passe



string pass = "PASS "+password;


envoimessage(nstream, pass);



//on ecoute la reponse...


reponse = rep(client,nstream);



if (!reponse.StartsWith("+OK"))


{


MessageBox.Show("erreur!");


Application.Exit();


}



//on envoi la commande STAT qui indique si on a des messages


envoimessage(nstream, "STAT");



//on ecoute la reponse...


reponse = rep(client,nstream);



if (!reponse.StartsWith("+OK"))


{


MessageBox.Show("erreur!");


Application.Exit();


}



//fermeture de la connection


client.Close();


}



string rep(TcpClient client, NetworkStream nstream)


{



byte[] bytes =
new
byte[client.ReceiveBufferSize];


nstream.Read(bytes, 0, (
int) client.ReceiveBufferSize);



string reponseserver = Encoding.ASCII.GetString(bytes);


tbstatut.Text = reponseserver;



return reponseserver;


}



void envoimessage(NetworkStream nstream,
string mes)


{


Byte[] message = Encoding.ASCII.GetBytes(mes);


nstream.Write(message, 0, message.Length);



return;


}



/// <summary>



/// Clean up any resources being used.



/// </summary>



protected
override
void Dispose(
bool disposing )


{



if( disposing )


{



if (components !=
null)


{


components.Dispose();


}


}



base.Dispose( disposing );


}


#region Windows Form Designer generated code



/// <summary>



/// Required method for Designer support - do not modify



/// the contents of this method with the code editor.



/// </summary>



private
void InitializeComponent()


{



this.groupBox1 =
new System.Windows.Forms.GroupBox();



this.groupBox2 =
new System.Windows.Forms.GroupBox();



this.groupBox3 =
new System.Windows.Forms.GroupBox();



this.groupBox4 =
new System.Windows.Forms.GroupBox();



this.groupBox5 =
new System.Windows.Forms.GroupBox();



this.bconnect =
new System.Windows.Forms.Button();



this.boptions =
new System.Windows.Forms.Button();



this.bquitter =
new System.Windows.Forms.Button();



this.tbserveur =
new System.Windows.Forms.TextBox();



this.tblogin =
new System.Windows.Forms.TextBox();



this.tbpassword =
new System.Windows.Forms.TextBox();



this.tbstatut =
new System.Windows.Forms.TextBox();



this.tbport =
new System.Windows.Forms.TextBox();



this.groupBox1.SuspendLayout();



this.groupBox2.SuspendLayout();



this.groupBox3.SuspendLayout();



this.groupBox4.SuspendLayout();



this.groupBox5.SuspendLayout();



this.SuspendLayout();



//



// groupBox1



//



this.groupBox1.Controls.Add(
this.tbserveur);



this.groupBox1.Location =
new System.Drawing.Point(8, 8);



this.groupBox1.Name = "groupBox1";



this.groupBox1.Size =
new System.Drawing.Size(288, 48);



this.groupBox1.TabIndex = 0;



this.groupBox1.TabStop =
false;



this.groupBox1.Text = "serveur";



//



// groupBox2



//



this.groupBox2.Controls.Add(
this.tblogin);



this.groupBox2.Location =
new System.Drawing.Point(8, 64);



this.groupBox2.Name = "groupBox2";



this.groupBox2.Size =
new System.Drawing.Size(288, 48);



this.groupBox2.TabIndex = 1;



this.groupBox2.TabStop =
false;



this.groupBox2.Text = "login";



//



// groupBox3



//



this.groupBox3.Controls.Add(
this.tbpassword);



this.groupBox3.Location =
new System.Drawing.Point(8, 120);



this.groupBox3.Name = "groupBox3";



this.groupBox3.Size =
new System.Drawing.Size(288, 48);



this.groupBox3.TabIndex = 2;



this.groupBox3.TabStop =
false;



this.groupBox3.Text = "password";



//



// groupBox4



//



this.groupBox4.Controls.Add(
this.tbport);



this.groupBox4.Location =
new System.Drawing.Point(304, 8);



this.groupBox4.Name = "groupBox4";



this.groupBox4.Size =
new System.Drawing.Size(104, 48);



this.groupBox4.TabIndex = 3;



this.groupBox4.TabStop =
false;



this.groupBox4.Text = "port";



//



// groupBox5



//



this.groupBox5.Controls.Add(
this.tbstatut);



this.groupBox5.Location =
new System.Drawing.Point(8, 176);



this.groupBox5.Name = "groupBox5";



this.groupBox5.Size =
new System.Drawing.Size(400, 48);



this.groupBox5.TabIndex = 4;



this.groupBox5.TabStop =
false;



this.groupBox5.Text = "statut";



//



// bconnect



//



this.bconnect.Location =
new System.Drawing.Point(304, 64);



this.bconnect.Name = "bconnect";



this.bconnect.Size =
new System.Drawing.Size(96, 24);



this.bconnect.TabIndex = 5;



this.bconnect.Text = "connection";



//



// boptions



//



this.boptions.Location =
new System.Drawing.Point(304, 96);



this.boptions.Name = "boptions";



this.boptions.Size =
new System.Drawing.Size(96, 24);



this.boptions.TabIndex = 6;



this.boptions.Text = "options";



//



// bquitter



//



this.bquitter.Location =
new System.Drawing.Point(304, 136);



this.bquitter.Name = "bquitter";



this.bquitter.Size =
new System.Drawing.Size(96, 24);



this.bquitter.TabIndex = 7;



this.bquitter.Text = "quitter";



//



// tbserveur



//



this.tbserveur.BackColor = System.Drawing.Color.Azure;



this.tbserveur.Location =
new System.Drawing.Point(8, 16);



this.tbserveur.Name = "tbserveur";



this.tbserveur.Size =
new System.Drawing.Size(264, 20);



this.tbserveur.TabIndex = 0;



this.tbserveur.Text = "textBox1";



//



// tblogin



//



this.tblogin.BackColor = System.Drawing.Color.Azure;



this.tblogin.Location =
new System.Drawing.Point(8, 16);



this.tblogin.Name = "tblogin";



this.tblogin.Size =
new System.Drawing.Size(264, 20);



this.tblogin.TabIndex = 1;



this.tblogin.Text = "textBox2";



//



// tbpassword



//



this.tbpassword.BackColor = System.Drawing.Color.Azure;



this.tbpassword.Location =
new System.Drawing.Point(8, 16);



this.tbpassword.Name = "tbpassword";



this.tbpassword.PasswordChar = '*';



this.tbpassword.Size =
new System.Drawing.Size(264, 20);



this.tbpassword.TabIndex = 2;



this.tbpassword.Text = "textBox3";



//



// tbstatut



//



this.tbstatut.BackColor = System.Drawing.Color.Azure;



this.tbstatut.Enabled =
false;



this.tbstatut.Location =
new System.Drawing.Point(8, 16);



this.tbstatut.Name = "tbstatut";



this.tbstatut.Size =
new System.Drawing.Size(384, 20);



this.tbstatut.TabIndex = 3;



this.tbstatut.Text = "textBox4";



//



// tbport



//



this.tbport.BackColor = System.Drawing.Color.Azure;



this.tbport.Location =
new System.Drawing.Point(8, 16);



this.tbport.Name = "tbport";



this.tbport.Size =
new System.Drawing.Size(88, 20);



this.tbport.TabIndex = 0;



this.tbport.Text = "textBox5";



//



// Form1



//



this.AutoScaleBaseSize =
new System.Drawing.Size(5, 13);



this.BackColor = System.Drawing.Color.LightSkyBlue;



this.ClientSize =
new System.Drawing.Size(416, 230);



this.Controls.Add(
this.bquitter);



this.Controls.Add(
this.boptions);



this.Controls.Add(
this.bconnect);



this.Controls.Add(
this.groupBox5);



this.Controls.Add(
this.groupBox4);



this.Controls.Add(
this.groupBox2);



this.Controls.Add(
this.groupBox1);



this.Controls.Add(
this.groupBox3);



this.Name = "Form1";



this.Text = "Form1";



this.groupBox1.ResumeLayout(
false);



this.groupBox2.ResumeLayout(
false);



this.groupBox3.ResumeLayout(
false);



this.groupBox4.ResumeLayout(
false);



this.groupBox5.ResumeLayout(
false);



this.ResumeLayout(
false);


}


#endregion



/// <summary>



/// The main entry point for the application.



/// </summary>


[STAThread]



static
void Main()


{


Application.Run(
new Form1());


}


}


}


<HR>

l'application freeze lors de mon appel à la fonction envoimessage(nstream, user);

vous avez une idée du pourquoi? :o

la société de consommation porte mal son nom car un con fait rarement une sommation avant de dire une connerie en société

2 réponses

sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
3 août 2005 à 06:14
le problème c'est tu effectues ce long traitement dans le thread principal.
Je te conseille de créer un nouveau thread qui fera ça en arrière plan, ca permettra à ton application de continuer à "vivre" normalement.


<HR>
Sébastien FERRAND

Blog : http://blogs.developpeur.org/sebmafate
0
warenbe Messages postés 145 Date d'inscription dimanche 2 décembre 2001 Statut Membre Dernière intervention 10 septembre 2009
3 août 2005 à 14:39
ok j'y connais rien en thread mais je vais essayer de m'y mettre
quelque chose me dit que je vais revenir poser des questions

sinon le fait que ça freeze dans le thread principale ça veut bien dire que quelque chsoe plante dans ma fonction qui envoi un message non? peut etre que je passe mal les parametres...
une idée?

la société de consommation porte mal son nom car un con fait rarement une sommation avant de dire une connerie en société
0
Rejoignez-nous