public int ProgramRegion(FileStream hardwareFileStream, MemoryRegion region, Action <int, int> progress)
        {
            uint currentPacketLength;
            uint noTransferredBytes = region.Size;
            uint currentAddress     = region.Address;

            while (noTransferredBytes > 0)
            {
                currentPacketLength = _bytesPerPacket < noTransferredBytes ? _bytesPerPacket : noTransferredBytes;

                byte[] data = new byte[currentPacketLength];

                for (uint i = 0, j = currentAddress - region.Address; i < currentPacketLength; i++, j++)
                {
                    data[i] = region.Data[j];
                }
                hardwareFileStream.Write(ProgramDeviceReq.getCommandPacket(currentAddress, data), 0, 65);

                progress((int)(region.Size - noTransferredBytes), (int)region.Size);

                noTransferredBytes -= currentPacketLength;
                currentAddress     += currentPacketLength;
            }
            hardwareFileStream.Write(ProgramCompleteReq.getCommandPacket(), 0, 65);


            return(0);
        }
        public int ProgramRegion(string path, MemoryRegion region, Action <int, int> progress)
        {
            SafeFileHandle asyncWriteHandle = CreateFile(path, Win32HardwareIOSupport.GENERIC_WRITE,
                                                         Win32HardwareIOSupport.FILE_SHARE_WRITE | Win32HardwareIOSupport.FILE_SHARE_READ, IntPtr.Zero, Win32HardwareIOSupport.OPEN_EXISTING, 0, IntPtr.Zero);

            if (asyncWriteHandle.IsInvalid)
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }
            int  noWriteBytes   = (int)region.Size;
            uint currentAddress = region.Address;
            int  currentPacketLength;
            uint trBytes;


            while (noWriteBytes > 0)
            {
                currentPacketLength = (int)_bytesPerPacket < noWriteBytes ? (int)_bytesPerPacket : noWriteBytes;
                byte[] data = new byte[currentPacketLength];
                for (uint i = 0, j = currentAddress - region.Address; i < currentPacketLength; i++, j++)
                {
                    data[i] = region.Data[j];
                }
                if (!WriteFile(asyncWriteHandle, ProgramDeviceReq.getCommandPacket(currentAddress, data),
                               65, out trBytes, IntPtr.Zero))
                {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }
                noWriteBytes   -= currentPacketLength;
                currentAddress += (uint)currentPacketLength;
                progress((int)(region.Size - noWriteBytes), (int)region.Size);
            }



            if (!WriteFile(asyncWriteHandle, ProgramCompleteReq.getCommandPacket(),
                           65, out trBytes, IntPtr.Zero))
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }


            asyncWriteHandle.Close();

            return(0);
        }