• 0

[.NET/Sockets] Infinite Loop when showing a form


Question

I am using Asynchronous sockets in a C# application but a minor problem has poped up. When I receive data, i need to show a form and display the data in it. When i do this (using either the Show() method or the Visible=true property) the application crashes. It looks like it the form entered in an infinite loop (the cursor turns to an hour glass) - the rest of the application remains usable, except for that new form.

I have temporarily fixed this by using the ShowDialog() mothod instead of Show() [which for some odd reason doesn't block the program, even though it should]. This made me think that the cause is because the new form is executing in another thread's context, but i know it shouldn't do this so i am not sure 100% :(

Does any know of a solution to this?!

this is the code i use:

string serv_rply = new string(System.Text.Encoding.ASCII.GetChars(cli.Recv()));
MessageBox.Show(serv_rply);
NetProtocolCmd npc = NetProtocolCmd.LoadFromXml(serv_rply);

if (npc.Command == NPCommands.npcLoginStatus) {
/* check the login status, but for now assume success */
	if (npc.isLoginSuccessful()) {
  frmNotify n = new frmNotify("hello", "world", 6000);
  n.Top = 50;
  n.Left = 50;
  n.Height = 150;
  n.Width = 150;
  //n.Show();
  n.ShowDialog();
	}
}

serv_rply is the string that holds the data received (which is xml)

npc is a deserialized object from the data received (the sender serializes the object into XML and sends the xml)

2 answers to this question

Recommended Posts

  • 0

public class frmNotify : System.Windows.Forms.Form
{
 ?private enum notifyState {nRise = 1, nShow, nHide};

 ?private System.Windows.Forms.Label lblMsg;
 ?private System.Windows.Forms.Label lblTItle;
 ?private System.Windows.Forms.Panel panel1;
 ?private System.Timers.Timer viewTimer;
 ?/// <summary>
 ?/// Required designer variable.
 ?/// </summary>
 ?private System.ComponentModel.Container components = null;

 ?private notifyState ns;
 ?public double delay;

 ?public frmNotify(string msg, string title, int delay)
 ?{
 ? ?//
 ? ?// Required for Windows Form Designer support
 ? ?//
 ? ?InitializeComponent();

 ? ?//
 ? ?// TODO: Add any constructor code after InitializeComponent call
 ? ?//
 ? ?this.lblMsg.Text = msg;
 ? ?this.lblTItle.Text = title;
 ? ?this.Text = title;
 ? ?this.delay = delay;
 ?}

/// <summary>
/// Clean up any resources being used.
/// </summary>
 ?protected override void Dispose( bool disposing )
 ?{
 ?< vs.net code >
 ?}

 ?public void ShowForm() {
 ? ?this.Top = Screen.PrimaryScreen.WorkingArea.Height;
 ? ?this.Left = Screen.PrimaryScreen.WorkingArea.Width-this.Width;
 ? ?this.Height = 0;
 ? ?ns = notifyState.nRise;
 ? ?viewTimer.Enabled = true;
 ? ?this.ShowDialog();
 ? ?this.Left = Screen.PrimaryScreen.WorkingArea.Width-this.Width;
 ?}

 ?public static void showNotifyWnd(string msg, string title, int delay) {
 ? ?frmNotify notifyWnd = new frmNotify(msg, title, delay);

 ? ?notifyWnd.ShowForm();
 ?}

 ?#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()
 ?{
 ?<standard vs.net code>
 ?}
 ?#endregion

 ?private void viewTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
 ? ?switch (ns) {
 ? ? ?case notifyState.nRise:
 ? ? ? ?this.Height += 8;
 ? ? ? ?this.Top = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
 ? ? ? ?this.Left = Screen.PrimaryScreen.WorkingArea.Width-this.Width;
 ? ? ? ?if (this.Height >= 121) {
 ? ? ? ? ?ns = notifyState.nShow;
 ? ? ? ?}
 ? ? ? ?break;
 ? ? ?case notifyState.nHide:
 ? ? ? ?this.Height -= 8;
 ? ? ? ?this.Top = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
 ? ? ? ?this.Left = Screen.PrimaryScreen.WorkingArea.Width-this.Width;
 ? ? ? ?if (this.Height <= 0) {
 ? ? ? ? ?viewTimer.Enabled = false;
 ? ? ? ? ?this.Close();
 ? ? ? ?}
 ? ? ? ?break;
 ? ? ?case notifyState.nShow:
 ? ? ? ?delay -= viewTimer.Interval;
 ? ? ? ?if (delay <= 0) {
 ? ? ? ? ? ? ? ?ns = notifyState.nHide;
 ? ? ? ?}
 ? ? ?break;
 ? ?} ?
 ?}	
}

note:

- viewTimer_Elapsed() is a method executing every n seconds (specified in the constructor of the form). This makes the window "rise" and "hide" like the notifications used by MSN and ICQ when some1 logs in

- there is the standard VS.NET generated code

- showNotifyWnd(string msg, string title, int delay) is actually used to construct and show the form

- showForm() displays the form at the bottom-right corner of the screen

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.