vb.net - displays at the same time

XK8ER
XK8ER used Ask the Experts™
on
hello,
I am trying to know what would happen if this code is executed at the exact same time..
every single time I execute it I get different milliseconds..
if some how this code were to be executed at the same time all 3 string would have the same GetRandomPassword is that correct?

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim x As Integer
        For x = 0 To 2

            Dim date1 As Date = Date.Now()

            Dim sTest As String = "my name is: bob " & GetRandomPassword(5, 5)
            Dim sTest2() As String

            sTest2 = sTest.Split(":")

            Debug.Print("Milliseconds: " & date1.Millisecond & " - " & sTest2(0) & " - " & sTest2(1))

        Next
    End Sub

Open in new window



Milliseconds: 828 - my name is -  bob 66026
Milliseconds: 831 - my name is -  bob 74215
Milliseconds: 832 - my name is -  bob 77374
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Where is the code for GetRandomPassword()?...

Author

Commented:
GetRandomPassword =  gets a random password
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Lol...but HOW does it generate the "random" password.

The Random() class is often used incorrectly...which can lead to the same "random" value being returned when calls occur in rapid succession.

If used correctly, however, then different values will be returned.

It just depends on how the system was designed, thus my desire to see the code.

Author

Commented:
well the issue here and the question has nothing to do with GetRandomPassword..

what im trying to find out is what would happen if the code is executed at the same time.. would it be the same or not since its not using double array would it be the same result..
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Your original question was:

    "if some how this code were to be executed at the same time all 3 string would have the same GetRandomPassword is that correct?"

Your last post changes that up to:

    "would it be the same or not since its not using double array would it be the same result"

So initially you wanted to know about GetRandomPassword(), and then afterwards the Array.

These can be two very different situations.

For variables declared locally (x, date1, sTest, sTest2), each thread would get their own copies.  So, with respect to the local variables, multiple threads running that code would not interfere or affect each other.

We don't have any information about GetRandomPassword(), however, so we don't know if it is thread-safe or not.  It could be coded to use a new Random() class instance each time it is called which would result in "repeat" values being returned if called in random succession.  It could also contain code that would prevent it from being running concurrently in multiple threads via thread synchronization techniques.  In that case, it would be impossible for the original code that is calling it to run concurrently because they would block until the method is available.

Thus my questions...

When THREADING is involved there is usually no easy, clear cut answer since things quickly become more complicated than you expect!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial