Groups | Blog | Home
all groups > dotnet interop > february 2006 >

dotnet interop : Program shuts down sporadicaly



Exardious NO[at]SPAM gmail.com
2/13/2006 1:10:47 AM
Hi

I hope someone is able to help me towards a solution here.
I've developed a program that flashes the ROMs of smartphones. It runs
with multithreads. I have 16 simultaenious threads of the same class,
collected in an array of that class.

The flashing process takes about 20 mins, regardless of the number of
smartphones connected.

PROBLEM:
More often than not, the program shuts down completely at any random
point in the flashing process. Theres no error, and no trace of the
program afterwards (In the process list). It just vanishes. I've tried
to debug it, by running the program through VS. But that doesnt bring
me any closer to a solution. When the program shuts down, theres no
error or exception. It just states "The program xxxx.exe has been shut
down. Exitcode(0)" or something similar. My mainforms Close-event and
Dispose function aren't being called.

The CPU isn't really stressed. With 16 smartphones being flashed at
once, the CPU load topped at 22%.
The program uses up about 170 megs of memory when running. (due to ROM
files)

I have checked for memory leaks, and cant identify any.

For those who like to draw the program is something similar to this:

I have a form, which is the main thread. The form has an array of 16
workerthreads. Every workerthread, has an object of an USBInterface
class (which i wrote myself), and another thread, which basically just
reads from the devices constantly.

All the USB communication, is handled through an activesync driver and
the use of
ReadFile
Writefile
GetOverlappedResult
Open
etc

[quoted text, click to view]

Can anyone maybe help me out? At least point out to me how i can
monitor what causes the error.

Thank in advance.
Exardious NO[at]SPAM gmail.com
2/13/2006 6:18:32 AM
After glancing through some of the topics in this newsgroup i maybe
found out what could be causing my problem.

When calling GetOverlappedResult in Kernel32, i use an OVERLAPPED
structure. What can happen, is that the Garbage Collector could move
around the memory in which the OVERLAPPED structure resides, and
thereby crashing my program.

Now i've tried to Marshal my structure so that it won't happen, but now
my program is way too slow. What normaly takes about 5 secs. couldn't
be completed in 20 mins.

If you have some questions/comments to the code, or missing something
to put it all together, just ask for it, and i'll provide it.

Here is some code for you:

' This is the OverLapped structure used by the calls to the Windows
API.
<StructLayout(LayoutKind.Sequential, Pack:=1)> Public Structure
OVERLAPPED
Public Internal As Integer
Public InternalHigh As Integer
Public Offset As Integer
Public OffsetHigh As Integer
Public hEvent As Integer
End Structure

<DllImport("kernel32.dll")> Private Shared Function WriteFile( _
ByVal hFile As Integer, ByVal Buffer As Byte(), _
ByVal nNumberOfBytesToWrite As Integer, _
ByRef lpNumberOfBytesWritten As Integer, _
ByRef lpOverlapped As IntPtr) As Boolean
End Function

<DllImport("kernel32.dll")> Private Shared Function
GetOverlappedResult( _
ByVal hFile As Integer, ByRef lpOverlapped As IntPtr, _
ByRef lpNumberOfBytesTransferred As Integer, _
ByVal bWait As Boolean) As Boolean
End Function


***WRITEFILE FUNCTION****

Public Function WriteFile(ByVal hFile As Integer, ByVal Buffer As
Byte(), ByVal nNumberOfBytesToWrite As Integer, ByRef
lpNumberOfBytesWritten As Integer) As Boolean
Dim ReturnValue As Boolean = False
Dim lastErr As Integer
Dim ObjectResult As Integer

Try
If Not WriteFile(hFile, Buffer, nNumberOfBytesToWrite,
lpNumberOfBytesWritten, OverlappedWptr) Then

If GetLastError() = ERROR_IO_PENDING Then
' Write is pending

If WaitForSingleObject(muOverlappedW.hEvent, 30000)
= WAIT_OBJECT_0 Then
' Object signaled,operation completed
If GetOverlappedResult(hFile, OverlappedWptr, _
lpNumberOfBytesWritten, False) Then
FlushFileBuffers(hFile)
ReturnValue = True
End If
End If
End If
Else
FlushFileBuffers(hFile)
ReturnValue = True
End If
Catch err As Exception
If isDebugMode Then Debug.WriteLine(hFile.ToString & ": " &
"FATAL ERROR IN WRITEFILE: " & err.Message)
ReturnValue = False
Catch ioErr As IOTimeoutException
If isDebugMode Then Debug.WriteLine(hFile.ToString & ": " &
"FATAL ERROR IN WRITEFILE: " & ioErr.Message)
ReturnValue = False
Catch cioErr As CIOChannelException
If isDebugMode Then Debug.WriteLine(hFile.ToString & ": " &
"FATAL ERROR IN WRITEFILE: " & cioErr.Message)
ReturnValue = False
End Try

FlushFileBuffers(hFile)
WriteFile = ReturnValue
End Function


*** MY CONSTRUCTOR ***

Sub New()


With muOverlappedE
.Internal = 0
.InternalHigh = 0
.Offset = 0
.Offset = 0
.hEvent = CreateEvent(Nothing, 0, 0, Nothing)
End With

With muOverlappedW
.Internal = 0
.InternalHigh = 0
.Offset = 0
.Offset = 0
.hEvent = CreateEvent(Nothing, 0, 0, Nothing)
End With

OverlappedEptr =
Marshal.AllocHGlobal(Marshal.SizeOf(muOverlappedE))
Marshal.StructureToPtr(muOverlappedE, OverlappedEptr, True)

OverlappedWptr =
Marshal.AllocHGlobal(Marshal.SizeOf(muOverlappedW))
Marshal.StructureToPtr(muOverlappedW, OverlappedWptr, True)

End Sub
Phil Wilson
2/14/2006 5:04:13 PM
If you wanted to pin something in C# to stop GC moving it while it's in use
outside the managed environment, you'd probably use the "fixed" keyword. I
don't know if there's a VB equivalent.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrffixed.asp
--
Phil Wilson [MVP Windows Installer]
----
[quoted text, click to view]

AddThis Social Bookmark Button