v3ks


Can anyone PLEASE explain this behavior

I am providing code below... but this is what is happening!

I am setting an application, and I have a timer that fires every 100 msec. My application creates a new thread and it generates a random number, assigns it to a variable, sleeps 70msec then dies. I am not disabling/enabling the timer every time the event occurs.... So, I decided to change the timer's interval to 30msec, and I expected either the application to crash or my PC to get stuck... but my application is running, and it hasn't crashed yet. WHY I can't find anything logical in this (but that could be due to my limited knowledge of UI programming, as well as threading).

Please see code for more details.Can anyone please help me understand what is actually happening and why is my application still running

The code is:

Code Snippet

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;


namespace TestApp
{
public partial class Form1 : Form
{
Thread t1 = null;
string str1;
Random r = new Random();
//
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}

public void Test1000()
{
str1 = r.Next().ToString();// SP1.ReadLine();
Thread.Sleep(70);
}

// timer interval was set to 30 in the Form Designer
private void timer1_Tick(object sender, EventArgs e)
{
try
{
t1 = new Thread(new ThreadStart(Test1000));
t1.Start();

while (t1.IsAlive)
Thread.Sleep(10); // keep checking if the thread is still alive.

label1.Text = str1; //update the random number generated in the thread.
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
//
Thread.Sleep(100);

GC.Collect();
}
}

private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
if (t1 != null)
t1.Abort();

//
label1.Text = "";
this.Close();
}
}
}





Re: Windows Forms General why is my application running????????

Christopher Payne

I'm not an expert at threads, but I have an interest so I'll take a shot.

The Thread.Sleep docs say that the thread will be suspended for the specified time. It does not explicitly say this, but I believe that the thread will not be pumping events while it is suspended. So even though your event handler is not doing much work while it waits for your thread to finish its work, I don't think you're getting additional tick events while you're waiting.

You would probably get the results you were expecting if you replaced the Thread.Sleep(10) in your Tick event with Application.DoEvents. That will cause the tick event to be re-entrant, and you'll end up creating tons and tons of mostly idle threads.

I'm also a little bit concerned that you're updating your str1 variable from one thread and reading it from another without any attempt at synchronizing them. It seems like you might want to lock it while you update it, or perhaps while you read it, or there would be some extremely slight chance that you could be reading the value while you're writing it. Could be an opportunity for something bad to happen once every other blue moon. Perhaps a real thread expert will chime in and tell me if I'm nuts, overly paranoid, or technically correct.... Smile





Re: Windows Forms General why is my application running????????

nobugz

Christopher is correct, a timer's Tick event doesn't run until your app goes idle and starts processing Windows messages again. Tick events don't back up, they just get delayed. There is implicit synchronization in the code as it waits for the thread to stop running. Of course, you'd never do this in a real app.





Re: Windows Forms General why is my application running????????

v3ks

Thanks! That makes sense......
Of course, I will note make an application based on the settings I was asking about, but I was curious about the fact that it was working!


Thanks again Christopher and noBugz

v3ks