views:

121

answers:

2

When I am loading an Assembly dynamically, then calling a method from it, I appear to be getting the method from Assembly executing before the code in the method that is calling it. It does not appear to be executing in a Serial manner as I would expect. Can anyone shine some light on why this might be happening. Below is some code to illustrate what I am seeing, the code from the some.dll assembly calls a method named PerformLookup. For testing I put a similar MessageBox type output with "PerformLookup Time: " as the text. What I end up seeing is:

First: "PerformLookup Time: 40:842"

Second: "initIndex Time: 45:873"

Imports System
Imports System.Data
Imports System.IO
Imports Microsoft.VisualBasic.Strings
Imports System.Reflection

Public Class Class1
    Public Function initIndex(indexTable as System.Collections.Hashtable) As System.Data.DataSet
        Dim writeCode As String
        MessageBox.Show("initIndex Time: " & Date.Now.Second.ToString() & ":" & Date.Now.Millisecond.ToString())
        System.Threading.Thread.Sleep(5000)
        writeCode = RefreshList()
    End Function

    Public Function RefreshList() As String   
        Dim asm As System.Reflection.Assembly
        Dim t As Type()
        Dim ty As Type
        Dim m As MethodInfo()
        Dim mm As MethodInfo
        Dim retString as String
        retString = ""

        Try
            asm = System.Reflection.Assembly.LoadFrom("C:\Program Files\some.dll")
            t = asm.GetTypes()
            ty = asm.GetType(t(28).FullName) 'known class location
            m = ty.GetMethods()
            mm = ty.GetMethod("PerformLookup")
            Dim o as Object
            o = Activator.CreateInstance(ty)
            Dim oo as Object()   
            retString = mm.Invoke(o,Nothing).ToString()
        Catch Ex As Exception       
        End Try

        return retString
    End Function
End Class

I added a flush statement to the end of my write method even though I had a StreamReader.Close() call. Same results. Here is some output from debug statements I put in to try and diagnose this further. In initIndex I have
write(timestamp)
save(file)
write(save success)
write(saved value)
write(timestamp)
write(file create / file modified times)
sleep(5 seconds)
invoke(assembly method)
write(timestamp)

And in the Assembly method i have
write(timestamp)
sleep(5 seconds) //yes, two 5 second sleeps between write and read
read(file)
write(file value)
write(timestamp)
write(file create / file write times)

Here is my output from initIndex logfile
17:732
True
A/P
17:732
5/17/2010 11:59:30 AM / 5/18/2010 7:49:17 AM
22:748

And here is the output from the Assembly class logfile
12:670
CASH
17:685
5/17/2010 11:59:30 AM / 5/18/2010 7:41:20 AM

A: 

Date times are not as precise as you think, you may just be seeing that imperfection.

StingyJack
In the actual code I am trying to read a value from a file. I update the value in the calling method, then read it in the Assembly method. The Assembly method is pulling the previous value from the file. When I look at the edit timestamp on the file via the Assembly class, I see that it has not been updated by the calling method.
pu.griffin
A: 

So you're writing the new time back to the file then re-reading it? Are you flushing the output stream to ensure the data has been written to the file and not just cached for a lazy-write?

Kelly Ethridge
See more output above.
pu.griffin
Do both methods have the file open at the same time?
Kelly Ethridge
I don't think so. Before the method that saves the file exits, there is a StreamReader.Close() call to close the file. Then there is a 5 second sleep after the method returns before the start of the assembly method. And at the start of the assembly method there is another 5 second sleep. Both sleeps are done with System.Threading.Thread.Sleep() calls. I thought this might be a multi-threading issue, but the sleeps did not seem to sync the code up either. Could this be a problem with the context that I am loading the assembly into?
pu.griffin