Threading logical problem??

  • Thread starter Thread starter James A Taber
  • Start date Start date
J

James A Taber

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
 
James A Taber said:
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?
 
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 said:
James A Taber said:
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
 
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
 
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
 
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,
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
 
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
 
Back
Top