• 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

    • I see you subscribe to the Linux/macOS approach to winning over converts...
    • As we get closer to October 2025 and the end of Windows 10 support, Windows 11 will pick up its pace and soon become the most popular desktop operating system worldwide. That's an assertion. It's also quite possible that the only growth will come from attrition as people and companies buy new machines that only come with Win11. It's also possible that we'll get a literal repeat of Win XP and Win 7, where a large number of users just waited until Microsoft gave in and fixed the core problems that the consumers were complaining about in Win Vista and Win 8 when they released Win 7 and Win 10.
    • It's significant growth for Linux considering the market share, so it could have had that effect I described.
    • Microsoft and Crowdstrike announce partnership on threat actor naming by Pradeep Viswanathan Whenever a cyberattack is discovered, companies disclose it to the public and assign it a unique name based on their internal procedures. Unfortunately, this leads to inconsistencies, as each company has its own naming conventions. As a result, the same threat actor behind a cyberattack may end up with multiple names, causing delays and confusion in response efforts. For example, a threat actor that Microsoft refers to as Midnight Blizzard might be known as Cozy Bear, APT29, or UNC2452 by other security vendors. To address this issue, Microsoft and CrowdStrike are teaming up. These companies will align their individual threat actor taxonomies to help security professionals respond to cyberattacks with greater clarity and confidence. It’s important to note that Microsoft and CrowdStrike are not attempting to create a single naming standard. Instead, they are releasing a mapping that lists common threat actors tracked by both companies, matched according to their respective taxonomies. The mapping also includes corresponding aliases from each group’s naming system. You can view the joint threat actor mapping by Microsoft and CrowdStrike here. Although this threat actor taxonomy mapping is a joint effort between Microsoft and CrowdStrike, Google/Mandiant and Palo Alto Networks' Unit 42 are expected to contribute to this initiative in the future. Vasu Jakkal, Corporate Vice President of Microsoft Security, wrote the following about this collaboration with CrowdStrike: As more organizations join this initiative, the collective defense against cyber threats will undoubtedly be improved.
  • Recent Achievements

    • First Post
      chriskinney317 earned a badge
      First Post
    • Week One Done
      Nullun earned a badge
      Week One Done
    • First Post
      sultangris earned a badge
      First Post
    • Reacting Well
      sultangris earned a badge
      Reacting Well
    • First Post
      ClarkB earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      172
    2. 2
      ATLien_0
      125
    3. 3
      snowy owl
      122
    4. 4
      Xenon
      116
    5. 5
      +Edouard
      93
  • Tell a friend

    Love Neowin? Tell a friend!