By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,933 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,933 IT Pros & Developers. It's quick & easy.

Problem with threads...

P: n/a
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[i]);
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.
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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[i]); 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]

Nov 16 '05 #2

P: n/a
Nurchi BECHED <nu****@telus.net> wrote:
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/t...shutdown.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

P: n/a
Richard Blewett [DevelopMentor] <ri******@develop.com> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

P: n/a
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 Blewett [DevelopMentor] wrote:
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]

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.