Threads help

A

Armando Rocha

Hi,

I want a know if is possible launch a separeted thread that run with a
interval of time like a Timer object (Timer.Tick).

my goal is put to buttons (Start and Stop), when user click Start i want
launch a thread that do something with a interval of 30 seconds at least,
but user can work normaly, but when user click Stop i want also stop thread.


--
Armando Rocha
Mobile Developer

http://www.ifthensoftware.com
PORTUGAL
 
P

Paul G. Tobey [eMVP]

Seems like we've had this discussion in this group before. Have you checked
the archives?

The pattern goes something like this:

-----

// This event will be set when you want the thread to exit.
protected AutoResetEvent thev = new AutoResetEvent( false );

startbutton_click()
{
th = new Thread( new ThreadStart( this.MyThread ) );
th.Start();
}

protected virtual void AcqThread()
{
while (thev.WaitOne(time, false) == false)
{
}

// The event was set. Time to exit the thread...
}

stopbutton_click()
{
thev.Set();

th.Join(waittime);
}
 
P

Paul G. Tobey [eMVP]

That's what happens when you copy/paste bits and pieces, but not the whole
program... Substitute the pieces below in your thinking:

-----

protected virtual void MyThread() // Helps to set the name right!
{
while (thev.WaitOne(time, false) == false)
{
// Do whatever processing you need here. This code will be executed
every 'time'
// milliseconds. So, if you want it to happen every 30s, set time
to 30000.
}

// The event was set. Time to exit the thread...
}

----

Note that you should *always* indicate what version of .NET CF you're
talking about. The code has to be written a *lot* differently for .NET CF
1.0 and 3.5...

Paul T.


"Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT
com> wrote in message news:[email protected]...
 
P

Paul G. Tobey [eMVP]

I don't mean you and I, personally. The archives of the group contain a
wealth of information, if you don't treat the group as a chat and, instead,
look to the archives *before* asking a question.

Paul T.

Armando Rocha said:
hi,

No, i never put this discussionin this group.

Thanks for the help Paul.

--
Armando Rocha
Mobile Developer

http://www.ifthensoftware.com
PORTUGAL
"Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT
com> escreveu na mensagem news:[email protected]...
 
A

Armando Rocha

you are right, bui i try find information for this issues but i cant find,
only after this i put my question to group.

--
Armando Rocha
Mobile Developer

http://www.ifthensoftware.com
PORTUGAL
"Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT
com> escreveu na mensagem news:[email protected]...
I don't mean you and I, personally. The archives of the group contain a
wealth of information, if you don't treat the group as a chat and, instead,
look to the archives *before* asking a question.

Paul T.

Armando Rocha said:
hi,

No, i never put this discussionin this group.

Thanks for the help Paul.

--
Armando Rocha
Mobile Developer

http://www.ifthensoftware.com
PORTUGAL
"Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam
DOT com> escreveu na mensagem
Seems like we've had this discussion in this group before. Have you
checked the archives?

The pattern goes something like this:

-----

// This event will be set when you want the thread to exit.
protected AutoResetEvent thev = new AutoResetEvent( false );

startbutton_click()
{
th = new Thread( new ThreadStart( this.MyThread ) );
th.Start();
}

protected virtual void AcqThread()
{
while (thev.WaitOne(time, false) == false)
{
}

// The event was set. Time to exit the thread...
}

stopbutton_click()
{
thev.Set();

th.Join(waittime);
}

-----

Paul T.

Hi,

I want a know if is possible launch a separeted thread that run with a
interval of time like a Timer object (Timer.Tick).

my goal is put to buttons (Start and Stop), when user click Start i
want launch a thread that do something with a interval of 30 seconds at
least, but user can work normaly, but when user click Stop i want also
stop thread.


--
Armando Rocha
Mobile Developer

http://www.ifthensoftware.com
PORTUGAL
 
P

Paul G. Tobey [eMVP]

What did you search on?! "thread periodic" gives great results...

Paul T.

Armando Rocha said:
you are right, bui i try find information for this issues but i cant find,
only after this i put my question to group.

--
Armando Rocha
Mobile Developer

http://www.ifthensoftware.com
PORTUGAL
"Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT
com> escreveu na mensagem news:[email protected]...
I don't mean you and I, personally. The archives of the group contain a
wealth of information, if you don't treat the group as a chat and,
instead, look to the archives *before* asking a question.

Paul T.

Armando Rocha said:
hi,

No, i never put this discussionin this group.

Thanks for the help Paul.

--
Armando Rocha
Mobile Developer

http://www.ifthensoftware.com
PORTUGAL
"Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam
DOT com> escreveu na mensagem
Seems like we've had this discussion in this group before. Have you
checked the archives?

The pattern goes something like this:

-----

// This event will be set when you want the thread to exit.
protected AutoResetEvent thev = new AutoResetEvent( false );

startbutton_click()
{
th = new Thread( new ThreadStart( this.MyThread ) );
th.Start();
}

protected virtual void AcqThread()
{
while (thev.WaitOne(time, false) == false)
{
}

// The event was set. Time to exit the thread...
}

stopbutton_click()
{
thev.Set();

th.Join(waittime);
}

-----

Paul T.

Hi,

I want a know if is possible launch a separeted thread that run with a
interval of time like a Timer object (Timer.Tick).

my goal is put to buttons (Start and Stop), when user click Start i
want launch a thread that do something with a interval of 30 seconds
at least, but user can work normaly, but when user click Stop i want
also stop thread.


--
Armando Rocha
Mobile Developer

http://www.ifthensoftware.com
PORTUGAL
 
S

Simon Hart [MVP]

Why can't you use the Threading.Timer class? this will execute on a separate
thread which essentially adds the request to the thread pool for you
automagically.
 
S

Scott Gifford

Armando Rocha said:
I want a know if is possible launch a separeted thread that run with a
interval of time like a Timer object (Timer.Tick).

my goal is put to buttons (Start and Stop), when user click Start i
want launch a thread that do something with a interval of 30 seconds
at least, but user can work normaly, but when user click Stop i want
also stop thread.

I would simply create a timer for this, and have the timer event
create a background thread and go off and do its work. Then the
Start/Stop button would just Enable/Disable the timer.

Depending on exactly what you need to do, it may not work for you, but
it works for my situation.

----Scott.
 
B

Bjørn Brox

Armando Rocha skrev:
Hi,

I want a know if is possible launch a separeted thread that run with a
interval of time like a Timer object (Timer.Tick).

my goal is put to buttons (Start and Stop), when user click Start i want
launch a thread that do something with a interval of 30 seconds at
least, but user can work normaly, but when user click Stop i want also
stop thread.
Here are some good tutorials:

http://msdn.microsoft.com/en-us/library/ms951109.aspx
http://www.yoda.arachsys.com/csharp/threads/
 

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