Problem with threads...

N

Nurchi BECHED

Hello, my dearest respected brother, All!

I am writing an application which uses printer port to play with LEDs for
now
and something else later...
I created a button which makes LEDs "dance"...

What I need to do is, make LEDs 'Dance' as I click the button.
Stop them when I click it again.
Resume, if I click it again.
Stop when I click again.
etc...

Here is the code:
//DanceLEDThread Declaration
System.Threading.Thread DanceLEDThread=null;
//Constructor code:
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

this.DanceLEDThread=new System.Threading.Thread(
new System.Threading.ThreadStart(this.Dance_LEDs));
this.DanceLEDThread.Priority=
System.Threading.ThreadPriority.BelowNormal;
}

//This makes LEDs "dance"
//Random numbers will be used in the future...
private void Dance_LEDs()
{
int[] x=new int[]
{
24, 36, 66, 129,
0,
129, 195, 231, 255
};
while(true)
{
for (int i=0; i<x.Length; i++)
{
//InpOut32.Output() is a wrapper
//method for InpOut32 library (DLL)
//and it works fine
//this.BaseAddress is the address of the port
//x is a local variable for this method
InpOut32.Output(this.BaseAddress, x);
System.Threading.Thread.Sleep(250);
}
}
}

//bDanceLeds button click method
private void bDanceLeds_Click(object sender, System.EventArgs e)
{
if (!this.Dancing)
{
if (this.DanceLEDThread.ThreadState==
System.Threading.ThreadState.Unstarted)
this.DanceLEDThread.Start();
else if (this.DanceLEDThread.ThreadState==
System.Threading.ThreadState.Suspended)
this.DanceLEDThread.Resume();
this.bDanceLeds.Text="Stop";

//Here is a weird thing:
//The thread is "Running" the first time I click the button
//and the LEDs dance
//The second time it is "WaitSleepJoin" (see 'else' below)
//and the LEDs stop dancing
//The third time it is "WaitSleepJoin, Suspended"
//And after that, it doesn't change
//and the LEDs are still not dancing
MessageBox.Show(
this.DanceLEDThread.ThreadState.ToString());
}
else
{
//Here is a weird thing:
//The thread state turns out to be "WaitSleepJoin"
//I assume, it is because of Sleep being invoked
//within the DanceLEDs method...
MessageBox.Show(
this.DanceLEDThread.ThreadState.ToString());
this.DanceLEDThread.Suspend();
this.bDanceLeds.Text="Dance Leds";
}
//this.Dancing is a bool variable
//to check whether 'Dancing' or not
this.Dancing=!this.Dancing;
}

In addition to all, the thread keeps running even
after I exit the application.
Well, this should be because I am not terminating the thread.
I tried to use Abort() in the Closing() method of the form, but it
throws an exception.

So, what would be a good way of doing what I need to do?

With best regards, Nurchi BECHED.
 
R

Richard Blewett [DevelopMentor]

Personally I?d use a timer (System.Windows.Forms.Timer)?to make the LEDs dance. Using thread for this type of animation is problematic as the thread cannot update the UI directly it has to request that the UI thread does it via Control.BeginInvoke

Regard

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/
Hello, my dearest respected brother, All!

I am writing an application which uses printer port to play with LEDs for now and something else later...
I created a button which makes LEDs "dance"...

What I need to do is, make LEDs 'Dance' as I click the button.
Stop them when I click it again.
Resume, if I click it again.
Stop when I click again.
etc...

Here is the code:
//DanceLEDThread Declaration
System.Threading.Thread DanceLEDThread=null; //Constructor code:
public Form1()
{
//
// Required for Windows Form Designer support // InitializeComponent();

this.DanceLEDThread=new System.Threading.Thread( new System.Threading.ThreadStart(this.Dance_LEDs));
this.DanceLEDThread.Priority=
System.Threading.ThreadPriority.BelowNormal;
}

//This makes LEDs "dance"
//Random numbers will be used in the future...
private void Dance_LEDs()
{
int[] x=new int[]
{
24, 36, 66, 129,
0,
129, 195, 231, 255
};
while(true)
{
for (int i=0; i<x.Length; i++)
{
//InpOut32.Output() is a wrapper
//method for InpOut32 library (DLL)
//and it works fine
//this.BaseAddress is the address of the port //x is a local variable for this method InpOut32.Output(this.BaseAddress, x); System.Threading.Thread.Sleep(250);
}
}
}

//bDanceLeds button click method
private void bDanceLeds_Click(object sender, System.EventArgs e) { if (!this.Dancing) { if (this.DanceLEDThread.ThreadState==
System.Threading.ThreadState.Unstarted)
this.DanceLEDThread.Start();
else if (this.DanceLEDThread.ThreadState==
System.Threading.ThreadState.Suspended)
this.DanceLEDThread.Resume();
this.bDanceLeds.Text="Stop";

//Here is a weird thing:
//The thread is "Running" the first time I click the button //and the LEDs dance //The second time it is "WaitSleepJoin" (see 'else' below) //and the LEDs stop dancing //The third time it is "WaitSleepJoin, Suspended"
//And after that, it doesn't change
//and the LEDs are still not dancing
MessageBox.Show(
this.DanceLEDThread.ThreadState.ToString());
}
else
{
//Here is a weird thing:
//The thread state turns out to be "WaitSleepJoin"
//I assume, it is because of Sleep being invoked //within the DanceLEDs method...
MessageBox.Show(
this.DanceLEDThread.ThreadState.ToString());
this.DanceLEDThread.Suspend();
this.bDanceLeds.Text="Dance Leds";
}
//this.Dancing is a bool variable
//to check whether 'Dancing' or not
this.Dancing=!this.Dancing;
}

In addition to all, the thread keeps running even after I exit the application.
Well, this should be because I am not terminating the thread.
I tried to use Abort() in the Closing() method of the form, but it throws an exception.

So, what would be a good way of doing what I need to do?

With best regards, Nurchi BECHED.


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004

[microsoft.public.dotnet.languages.csharp]
 
J

Jon Skeet [C# MVP]

Nurchi BECHED said:
Hello, my dearest respected brother, All!

I am writing an application which uses printer port to play with LEDs for
now and something else later...
I created a button which makes LEDs "dance"...

What I need to do is, make LEDs 'Dance' as I click the button.
Stop them when I click it again.
Resume, if I click it again.
Stop when I click again.
etc...

Rather than using ThreadState, keep track of the state yourself.

For your other questions:

1) The secondary thread could be a background thread, which will make
the process end when the main thread has terminated

2) The secondary thread could poll a flag set by the main thread to say
that it should stop

See
http://www.pobox.com/~skeet/csharp/threads/shutdown.shtml
 
J

Jon Skeet [C# MVP]

Richard Blewett said:
Personally I?d use a timer (System.Windows.Forms.Timer)?to make the
LEDs dance. Using thread for this type of animation is problematic as
the thread cannot update the UI directly it has to request that the
UI thread does it via Control.BeginInvoke

The secondary thread doesn't need to update the UI as far as I can see
- it's updating the printer port.
 
R

Richard Blewett [DevelopMentor]

Ahh my mistake, I thought the "LEDs" were on the screen - I missed that they were real LEDs

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

?
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/
Richard said:
Personally I?d use a timer (System.Windows.Forms.Timer)?to make the
LEDs dance. Using thread for this type of animation is problematic as
the thread cannot update the UI directly it has to request that the UI
thread does it via Control.BeginInvoke

The secondary thread doesn't need to update the UI as far as I can see
- it's updating the printer port.

--
Jon Skeet -
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004

[microsoft.public.dotnet.languages.csharp]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top