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

Threading logical problem??

P: n/a
What I want to implement is to restart certain thread every 24 hours...

sample code:

Dim T() As Thread
Dim L() As Lib1.Class1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim i As Integer = 0
For i = 0 To 2

ReDim Preserve T(i)
ReDim Preserve L(i)

L(i) = New Lib1.Class1
L(i).m1 = i

T(i) = New Thread(AddressOf L(i).StartTimer) '<--- Class1
contains a timer to do the required task.
T(i).Start()

Trace.WriteLine(T(i).ThreadState.ToString) '<---- Unstarted ??
Next

End Sub



Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"James A Taber" <ic*******@hotmail.com> schrieb
What I want to implement is to restart certain thread every 24
hours...

sample code:

[...]
Trace.WriteLine(T(i).ThreadState.ToString) '<----
Unstarted ??
As long as *this* thread runs, the other one might still be unstarted, but
the OS will start it when it is it's turn.
Next

End Sub


Maybe I did not understand the problem, yet. Isn't starting a
System.Timers.Timer sufficient?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2

P: n/a
Hi Armin,

Thanks for you reply,

The timer works fine... what I am experiencing is that somtimes some of the
thread seams to stop working ... thats why I want to restart all the threads
in the application after a certain time....

-James A Taber


"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
"James A Taber" <ic*******@hotmail.com> schrieb
What I want to implement is to restart certain thread every 24
hours...

sample code:

[...]
Trace.WriteLine(T(i).ThreadState.ToString) '<----
Unstarted ??


As long as *this* thread runs, the other one might still be unstarted, but
the OS will start it when it is it's turn.
Next

End Sub


Maybe I did not understand the problem, yet. Isn't starting a
System.Timers.Timer sufficient?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3

P: n/a
James,
In addition to Armin's comments, have you tried using Thread.Sleep(0) to
give the second thread a chance to start?

Something like:
T(i) = New Thread(AddressOf L(i).StartTimer) '<--- Class1
contains a timer to do the required task.
T(i).Start()
Thread.Sleep(0)

Trace.WriteLine(T(i).ThreadState.ToString) '<---- Unstarted ??
Its not that your second thread did not start as much as your first thread
is not sharing the CPU. ;-)

Also in addition to restarting threads, you may want to consider restarting
AppDomains also.

Hope this helps
Jay

"James A Taber" <ic*******@hotmail.com> wrote in message
news:e3**************@TK2MSFTNGP12.phx.gbl... What I want to implement is to restart certain thread every 24 hours...

sample code:

Dim T() As Thread
Dim L() As Lib1.Class1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim i As Integer = 0
For i = 0 To 2

ReDim Preserve T(i)
ReDim Preserve L(i)

L(i) = New Lib1.Class1
L(i).m1 = i

T(i) = New Thread(AddressOf L(i).StartTimer) '<--- Class1
contains a timer to do the required task.
T(i).Start()

Trace.WriteLine(T(i).ThreadState.ToString) '<---- Unstarted ??
Next

End Sub


Nov 20 '05 #4

P: n/a
Hi Jay,

I have tried use Sleep(3000) but it didnt make any difference ... is it
possible that the timer in Class1 one is running in its own thread?

hm... restart Appdomain ? how can I do that ?

is this true --> Restart Appdomain = Initialize Application

James A Taber
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:es**************@TK2MSFTNGP10.phx.gbl...
James,
In addition to Armin's comments, have you tried using Thread.Sleep(0) to
give the second thread a chance to start?

Something like:
T(i) = New Thread(AddressOf L(i).StartTimer) '<--- Class1
contains a timer to do the required task.
T(i).Start()
Thread.Sleep(0)

Trace.WriteLine(T(i).ThreadState.ToString) '<---- Unstarted ??
Its not that your second thread did not start as much as your first thread
is not sharing the CPU. ;-)

Also in addition to restarting threads, you may want to consider restarting AppDomains also.

Hope this helps
Jay

"James A Taber" <ic*******@hotmail.com> wrote in message
news:e3**************@TK2MSFTNGP12.phx.gbl...
What I want to implement is to restart certain thread every 24 hours...

sample code:

Dim T() As Thread
Dim L() As Lib1.Class1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim i As Integer = 0
For i = 0 To 2

ReDim Preserve T(i)
ReDim Preserve L(i)

L(i) = New Lib1.Class1
L(i).m1 = i

T(i) = New Thread(AddressOf L(i).StartTimer) '<--- Class1
contains a timer to do the required task.
T(i).Start()

Trace.WriteLine(T(i).ThreadState.ToString) '<---- Unstarted

?? Next

End Sub



Nov 20 '05 #5

P: n/a
James,
is it
possible that the timer in Class1 one is running in its own thread? Hard to say as you don't say what type of timer is in Class1. Remember there
are 3 distinct Timer classes in .NET.

Hope this helps
Jay

"James A Taber" <ic*******@hotmail.com> wrote in message
news:us**************@tk2msftngp13.phx.gbl... Hi Jay,

I have tried use Sleep(3000) but it didnt make any difference ... is it
possible that the timer in Class1 one is running in its own thread?

hm... restart Appdomain ? how can I do that ?

is this true --> Restart Appdomain = Initialize Application

James A Taber
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:es**************@TK2MSFTNGP10.phx.gbl...
James,
In addition to Armin's comments, have you tried using Thread.Sleep(0) to
give the second thread a chance to start?

Something like:
T(i) = New Thread(AddressOf L(i).StartTimer) '<--- Class1
contains a timer to do the required task.
T(i).Start()
Thread.Sleep(0)

Trace.WriteLine(T(i).ThreadState.ToString) '<---- Unstarted ??

Its not that your second thread did not start as much as your first thread
is not sharing the CPU. ;-)

Also in addition to restarting threads, you may want to consider restarting
AppDomains also.

Hope this helps
Jay

"James A Taber" <ic*******@hotmail.com> wrote in message
news:e3**************@TK2MSFTNGP12.phx.gbl...
What I want to implement is to restart certain thread every 24

hours...
sample code:

Dim T() As Thread
Dim L() As Lib1.Class1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim i As Integer = 0
For i = 0 To 2

ReDim Preserve T(i)
ReDim Preserve L(i)

L(i) = New Lib1.Class1
L(i).m1 = i

T(i) = New Thread(AddressOf L(i).StartTimer) '<--- Class1
contains a timer to do the required task.
T(i).Start()

Trace.WriteLine(T(i).ThreadState.ToString) '<----

Unstarted ?? Next

End Sub




Nov 20 '05 #6

P: n/a
James,
Yes restarting an AppDomain is effectively the same as re-initializing your
app, however you can control what parts of your app are running in which
AppDomain.

I don't have a clean example, to "restart" an AppDomain, you need to create
a new AppDomain and start execution there, letting the current one finish. I
would start by looking at the System.AppDomain class and its Load & Unload
methods...

Hope this helps
Jay

"James A Taber" <ic*******@hotmail.com> wrote in message
news:us**************@tk2msftngp13.phx.gbl...
Hi Jay,

I have tried use Sleep(3000) but it didnt make any difference ... is it
possible that the timer in Class1 one is running in its own thread?

hm... restart Appdomain ? how can I do that ?

is this true --> Restart Appdomain = Initialize Application

James A Taber
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:es**************@TK2MSFTNGP10.phx.gbl...
James,
In addition to Armin's comments, have you tried using Thread.Sleep(0) to
give the second thread a chance to start?

Something like:
T(i) = New Thread(AddressOf L(i).StartTimer) '<--- Class1
contains a timer to do the required task.
T(i).Start()
Thread.Sleep(0)

Trace.WriteLine(T(i).ThreadState.ToString) '<---- Unstarted ??

Its not that your second thread did not start as much as your first thread
is not sharing the CPU. ;-)

Also in addition to restarting threads, you may want to consider restarting
AppDomains also.

Hope this helps
Jay

"James A Taber" <ic*******@hotmail.com> wrote in message
news:e3**************@TK2MSFTNGP12.phx.gbl...
What I want to implement is to restart certain thread every 24

hours...
sample code:

Dim T() As Thread
Dim L() As Lib1.Class1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim i As Integer = 0
For i = 0 To 2

ReDim Preserve T(i)
ReDim Preserve L(i)

L(i) = New Lib1.Class1
L(i).m1 = i

T(i) = New Thread(AddressOf L(i).StartTimer) '<--- Class1
contains a timer to do the required task.
T(i).Start()

Trace.WriteLine(T(i).ThreadState.ToString) '<----

Unstarted ?? Next

End Sub




Nov 20 '05 #7

P: n/a
Thanks for helping me out Jay...

This is the Timer iam using

System.Timers.Timer

I will look more into the Appdomain "feature" later tonigh...

Thanks again
-James A Taber
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uN**************@TK2MSFTNGP10.phx.gbl...
James,
is it
possible that the timer in Class1 one is running in its own thread? Hard to say as you don't say what type of timer is in Class1. Remember

there are 3 distinct Timer classes in .NET.

Hope this helps
Jay

"James A Taber" <ic*******@hotmail.com> wrote in message
news:us**************@tk2msftngp13.phx.gbl...
Hi Jay,

I have tried use Sleep(3000) but it didnt make any difference ... is it
possible that the timer in Class1 one is running in its own thread?

hm... restart Appdomain ? how can I do that ?

is this true --> Restart Appdomain = Initialize Application

James A Taber
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:es**************@TK2MSFTNGP10.phx.gbl...
James,
In addition to Armin's comments, have you tried using Thread.Sleep(0) to give the second thread a chance to start?

Something like:
> T(i) = New Thread(AddressOf L(i).StartTimer) '<--- Class1 > contains a timer to do the required task.
> T(i).Start()

Thread.Sleep(0)

>
> Trace.WriteLine(T(i).ThreadState.ToString) '<----

Unstarted
??

Its not that your second thread did not start as much as your first

thread is not sharing the CPU. ;-)

Also in addition to restarting threads, you may want to consider

restarting
AppDomains also.

Hope this helps
Jay

"James A Taber" <ic*******@hotmail.com> wrote in message
news:e3**************@TK2MSFTNGP12.phx.gbl...
> What I want to implement is to restart certain thread every 24 hours... >
> sample code:
>
> Dim T() As Thread
> Dim L() As Lib1.Class1
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles Button1.Click
>
> Dim i As Integer = 0
> For i = 0 To 2
>
> ReDim Preserve T(i)
> ReDim Preserve L(i)
>
> L(i) = New Lib1.Class1
> L(i).m1 = i
>
> T(i) = New Thread(AddressOf L(i).StartTimer) '<--- Class1 > contains a timer to do the required task.
> T(i).Start()
>
> Trace.WriteLine(T(i).ThreadState.ToString) '<----

Unstarted
??
> Next
>
> End Sub
>
>
>
>
>
>
>



Nov 20 '05 #8

P: n/a
James,
System.Timers.Timer will raise its event in a new thread (via the
ThreadPool), there is no need to create a thread to explicitly handle it.

For details on the Timers.Timer and the other 2 timers in .NET check out
this recent article:

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

Hope this helps
Jay

"James A Taber" <ic*******@hotmail.com> wrote in message
news:eJ**************@TK2MSFTNGP12.phx.gbl...
Thanks for helping me out Jay...

This is the Timer iam using

System.Timers.Timer

I will look more into the Appdomain "feature" later tonigh...

Thanks again
-James A Taber
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uN**************@TK2MSFTNGP10.phx.gbl...
James,
is it
possible that the timer in Class1 one is running in its own thread? Hard to say as you don't say what type of timer is in Class1. Remember

there
are 3 distinct Timer classes in .NET.

Hope this helps
Jay

"James A Taber" <ic*******@hotmail.com> wrote in message
news:us**************@tk2msftngp13.phx.gbl...
Hi Jay,

I have tried use Sleep(3000) but it didnt make any difference ... is it possible that the timer in Class1 one is running in its own thread?

hm... restart Appdomain ? how can I do that ?

is this true --> Restart Appdomain = Initialize Application

James A Taber
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:es**************@TK2MSFTNGP10.phx.gbl...
> James,
> In addition to Armin's comments, have you tried using
Thread.Sleep(0) to > give the second thread a chance to start?
>
> Something like:
> > T(i) = New Thread(AddressOf L(i).StartTimer) '<--- Class1 > > contains a timer to do the required task.
> > T(i).Start()
>
> Thread.Sleep(0)
>
> >
> > Trace.WriteLine(T(i).ThreadState.ToString) '<----

Unstarted
??
>
> Its not that your second thread did not start as much as your first

thread
> is not sharing the CPU. ;-)
>
> Also in addition to restarting threads, you may want to consider
restarting
> AppDomains also.
>
> Hope this helps
> Jay
>
> "James A Taber" <ic*******@hotmail.com> wrote in message
> news:e3**************@TK2MSFTNGP12.phx.gbl...
> > What I want to implement is to restart certain thread every 24

hours...
> >
> > sample code:
> >
> > Dim T() As Thread
> > Dim L() As Lib1.Class1
> > Private Sub Button1_Click(ByVal sender As System.Object, ByVal
e
As
> > System.EventArgs) Handles Button1.Click
> >
> > Dim i As Integer = 0
> > For i = 0 To 2
> >
> > ReDim Preserve T(i)
> > ReDim Preserve L(i)
> >
> > L(i) = New Lib1.Class1
> > L(i).m1 = i
> >
> > T(i) = New Thread(AddressOf L(i).StartTimer) '<---

Class1 > > contains a timer to do the required task.
> > T(i).Start()
> >
> > Trace.WriteLine(T(i).ThreadState.ToString) '<----

Unstarted
??
> > Next
> >
> > End Sub
> >
> >
> >
> >
> >
> >
> >
>
>



Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.