Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. I

R

Roberto Rasto

I have a problem.

I'm using an ocx (Pivo.EmailValidator) that have a method: validate (with
the e-mail for parameter).
I'd like to create several thread to validate several e-mail at the same
time.
I need to create another class because the validate method needs parameters
and I can't find any way to pass parameter in the addressof statement when I
create the Thread.

The problem is that the class description say:

Thread Safety
Public static (Shared in Visual Basic) members of this type are safe for
multithreaded operations. Instance members are not guaranteed to be
thread-safe.

What does mean?
Can I use this code or I have some problem because I use instanced object?
Thanks a lot.

Roberto

(Emails is an array of email to validate)

.......
Dim Valid as Validator
For i = 1 To UBound(t)
Valid = New Validator
Valid.Email = Emails(i)
Valid.ValidationLevel = 4
t(i) = New Thread(AddressOf objValid.Validate)
t(i).Start()
Next i
.........


Public Class Validator
Public Email as String
Public EmailIsOK as Integer
Public ValidationLevel as Integer

Public Sub Validate()
EmailIsOK = EMailValidator.Validate(EMail, ValidationLevel)
End Sub

End Class





Sorry, but I can't understand if I can instance several objects of this
class and start a method, or I have join the thread with a shared object of
the class
 
T

Tom Shelton

I have a problem.

I'm using an ocx (Pivo.EmailValidator) that have a method: validate (with
the e-mail for parameter).
I'd like to create several thread to validate several e-mail at the same
time.
I need to create another class because the validate method needs parameters
and I can't find any way to pass parameter in the addressof statement when I
create the Thread.

Assuming your using VB2005 or latter... Then you first change your
thread method to look like:

Sub ThreadSub (ByVal paramObject As Object)
End Sub

Then, when you create the thread object:
Dim t As Thread = new Thread (AddressOf ThreadSub)

Then, you pass the argument via the Thread.Start method:

t.Start (emails)

Now, if you need multiple arguments, you simply create a class to
contain them and then pass that in.


If your not using VB2005 or latter, then the common method of passing
arguments was to create a class that hosted a thread. Something like

class worker
private i as integer
private name as string

public sub new(byval i as integer, byval name as string)
me.i = i
me.name = name
end sub

public sub Process()
' start thread here
Dim t as thread = new thread(addressof workersub)
t.start()
end sub

private sub WorkerSub ()
' do stuff with i and name
end sub
end class

Obviously, if you need to return results, or be notified of the
completion of the thread, etc, there is more work to be done here. This
method insn't really that bad an idea even in VB2005

The question you have to ask yourself is do you really need threading
here? And if you do, do you have to do it explicitly? Can you make use
of the ThreadPool? Using the ThreadPool is usually a better idea if you
need to do short running type processes....
The problem is that the class description say:

Thread Safety
Public static (Shared in Visual Basic) members of this type are safe for
multithreaded operations. Instance members are not guaranteed to be
thread-safe.

It means that only shared methods are thread safe. Instance methods may
require work on your part (see synclock or the system.threading.monitor
class) to guarentee thread saftey. This totally depends on your use.
 
A

Armin Zingler

Roberto Rasto said:
I have a problem.

I'm using an ocx (Pivo.EmailValidator) that have a method: validate
(with the e-mail for parameter).

I believe that it is not an ocx. The manufacturer's homepage states it's
an assembly. Also, I don't think there are /static/ and control specific
members for imported OCXes.
I'd like to create several thread to validate several e-mail at the
same time.
I need to create another class because the validate method needs
parameters and I can't find any way to pass parameter in the
addressof statement when I create the Thread.

The problem is that the class description say:


That's the description for the EmailValidator class?

Thread Safety
Public static (Shared in Visual Basic) members of this type are safe
for multithreaded operations. Instance members are not guaranteed to
be thread-safe.

What does mean?
Can I use this code or I have some problem because I use instanced
object? Thanks a lot.

Roberto

(Emails is an array of email to validate)

......
Dim Valid as Validator
For i = 1 To UBound(t)
Valid = New Validator
Valid.Email = Emails(i)
Valid.ValidationLevel = 4
t(i) = New Thread(AddressOf objValid.Validate)
t(i).Start()
Next i
........


Public Class Validator
Public Email as String
Public EmailIsOK as Integer
Public ValidationLevel as Integer

Public Sub Validate()
EmailIsOK = EMailValidator.Validate(EMail, ValidationLevel)
End Sub

End Class





Sorry, but I can't understand if I can instance several objects of
this class and start a method, or I have join the thread with a
shared object of the class


According to the documentation, the code should work because Validate is
a static method, and it is safe for multithreading. You don't have to
add any synchronization code.


Armin
 

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