• 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.
  • Posts

    • KDE Plasma 6.5 will notify you if your printer's ink is low by David Uzondu This week, the KDE team continued work on the upcoming Plasma 6.5.0 as well as Plasma 6.4's fourth bug fix release, 6.4.4. As usual, both Plasma versions saw several UI tweaks, bug fixes, and performance improvements. The most notable changes are discussed in this article. Let's start with Plasma 6.5. The desktop environment is getting a useful feature that tells you when your printer is low on ink. This works by having the system check the Common Unix Printing System, or CUPS, for marker levels after a print job is created or completed. CUPS stores attributes like marker-levels in its printers.conf file, and once a level is determined to be low, it triggers a marker-supply-low-warning that Plasma will now use to inform you. UI improvements scheduled for 6.5 include disabling key repeat for certain global shortcuts, like toggling Overview, to prevent rapid screen flashing that could be a seizure risk. There is a better "Someone started sharing this screen" notification that now appears only after a connection is fully established. You will also find standard KDE styling with the "Confirm deleting network connection" dialog, and more consistent spacing in the Global Menu widget. As for bug fixes in 6.5, a layout bug that caused visual overflow in the printer setup page has been corrected. An issue that stopped you from using the virtual keyboard in the Application Dashboard search field is fixed, and XDG portal-using apps can now request screencasts of new virtual outputs. Finally, the clipboard configuration window's size and position information has been moved from the state config file to the settings file. Moving on to 6.4.4, the hitboxes for desktop items now correctly match their visual styling. This means no more accidentally selecting an invisible box around a file. And when you mark a notification as low priority, it will now correctly appear in your history if it arrived during Do Not Disturb mode, so it does not just vanish. If you're experiencing a Kwin crash on login, particularly in a QEMU virtual machine, 6.4.4 has a fix for that on the way. Other bug fixes 6.4.4 brings include: A fix for the Global Menu widget's single-button mode for X11 users. The search field in the Wayland version of the Global Menu widget works again. An annoying bug in the Global Shortcuts XDG portal that made apps think they had no shortcuts has been resolved. Plasma Browser Integration's built-in Share feature has been repaired. Plasma 6.4.4 will drop on the 5th of next month. You can find more details on the official KDE Blog.
    • I just remember wondering how she was ever a bridge officer at all with all of her insufferable insecurities. I am sure she's a wonderful person in real life, but the character was poorly written. They even had to dedicate a whole episode to her being forced into taking the responsibility of leading cadets out of a problem of some sort as if to show how she is "growing" into her function. Never bridge officer material and I just rolled my eyes at her scenes and eventually stopped watching the show altogether, but also because of all the lead character's cry/whisper/talking that frustrated me.
    • Didnt know it was coded/decoded so my bad.
    • Why isn't it more widespread then? What about people that like PC gaming and don't want/don't own a console? MMO gaming/professional work with specific software aren't "edge cases".
  • Popular Contributors

    1. 1
      +primortal
      641
    2. 2
      ATLien_0
      241
    3. 3
      Xenon
      168
    4. 4
      neufuse
      149
    5. 5
      +FloatingFatMan
      123
  • Tell a friend

    Love Neowin? Tell a friend!