• 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

    • AdGuard is yet another app to block Windows Recall by Taras Buria Windows Recall is one of the most controversial Windows features. After the misfired launch and the scandal that followed, Microsoft implemented plenty of security measures and improvements. Still, some believe that Recall's reputation is tarnished forever. Besides, not everyone is comfortable with using the feature that takes screenshots of everything you do. As such, privacy-focused apps, browsers, and messengers are now offering blocks for Recall. AdGuard is the latest one to join them. AdGuard for Windows 7.21 introduces a new feature that lets you turn off Windows Recall. Although Recall is a strictly opt-in experience and it has several security measures, AdGuard developers believe that it is not enough. They argue that the feature idea itself is unsettling, PINs are easy to crack, and filters sometimes fail to engage. Here is what AdGuard says in the announcement post: If you use AdGuard on your PC, you can find the new "Disable Windows Recall" feature in Settings > Tracking Protection. As of now, Recall is only available on Copilot+ PCs. This month's non-security update expanded Recall to more users, making the feature available in the European Economic Area. And with Intel working on the next-generation desktop processors with improved NPUs, you can expect Recall and other recently introduced AI features to make their way to more users with desktop PCs. As an online publication, Neowin relies on ads for operating costs, and if you use an ad blocker, we'd appreciate being whitelisted. In addition, we have an ad-free subscription for $28 a year, which is another way to show support!
    • I'm sorry for sounding rude. I was just frustrated. I really want to get this right because I don't want to take any risk of potentially contaminating other hardware and risk losing precious files. Thanks again for the help. 
    • I am low-key enjoying the new start menu of 25H2 build. Looks like the management team who shoved the Recommended Section down our throats up till now got fired in recent gommage at MS. Also loving the 6Ghz Hotspot feature. I only enabled these 2 features and I am happy. I keep an eye on MDL forums for 26200.xxxx updates once a week to stay up to date manually. I downloaded the latest build from https://uupdump.net selected Dev Channel tab and downloaded amd64 build. Let the script ripp. Then I used vivetool to enable the above 2 features • 25H2 New Start Menu ViVeTool.exe /enable /id:47205210,49221331,49381526,49402389,49820095,55495322,48433719 • Enable Wifi 6Ghz Hotspot ViVeTool.exe /enable /id:40466470,48433719 Reboot and enjoy. Almost all the above info was provided by Neowin at some point.
    • Indeed, I do think its sad in some ways a corporation can never be content with "performing well across every metric" and having over £59 billion cash on hand.
    • They've been focusing on security and quality? Could have fooled me. Their own paying customers literally just got breached because they failed to push SharePoint updates downstream to on prem servers operating outside of their "365" ecosystem.
  • Recent Achievements

    • One Month Later
      Philsl earned a badge
      One Month Later
    • One Year In
      armandointerior640 earned a badge
      One Year In
    • One Month Later
      armandointerior640 earned a badge
      One Month Later
    • One Month Later
      Itbob513626 earned a badge
      One Month Later
    • Week One Done
      Itbob513626 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      615
    2. 2
      ATLien_0
      236
    3. 3
      Xenon
      156
    4. 4
      +FloatingFatMan
      122
    5. 5
      Michael Scrip
      116
  • Tell a friend

    Love Neowin? Tell a friend!