Link to home
Start Free TrialLog in
Avatar of JSMCM
JSMCM

asked on

CreateThread API

Hello,

I am writing an application that needs to read and write to files. The user chooses the file(s) to read from, and the app then reads the files byte by byte, and writes it into another file byte by byte.

This takes quite a long time, so I am trying to use the create thread API to Run this on a different thread.

The user may choose for example 10 files to copy. All of the info for these 10 files, eg, the file paths and names, how many files the user chose etc are stored in public variables. The thread is then called using create thread. Inside the thread, I define local versions of these variable, and copy the info from the public variable into the private variable. I then use the private variables in the thread.
This then frees the public variables for the user to select more files and create another thread if they want to.

The problem is that it crashes alot. I have declared the createthread(....) in the main form, and I have put the routine in a module. The thread is called in the main form using the click event of a button -> ID = createthread(,,addressof RoutineName,  , , ID) - Please note, this is not how the thread looks in my project, I am at a different PC now and don't have the exact code with me. What I did want to find out though is that in some examples, they use the exact same variable, eg ID. In some examples I have seen they use a different variable, eg Thread = createthread(,,addressof RoutineName,  , , ID). Which would be more correct, or doesn't it matter?

The project also craches alot. Sometimes it works, sometimes it give a fatal exception and sometimes it just closes the project and VB's design environment.

Is there any way to overcome this? If you need more details on the code I've used please let em know, Thanks,
John
Avatar of iozturk
iozturk
Flag of Türkiye image

If you are insist on copying byte by byte in your loop consider using doevents.
Multithreading is not supported by VB (If you arenot using VB .NET).

Avatar of y2ksw
y2ksw

Have a look at:

http://www.vbaccelerator.com/codelib/thread/axexethr.zip

There is a complete class for threading and some hints how not to crash.
Just suggestion

Should you read the file in whole and then write to file instead of reading byte by byte.

It should speed up a lot in your app
Avatar of JSMCM

ASKER

Hi guys, thanks, but I do need to read / write byte by byte as I perform calculations on each byte. That is why I need to multithread, sothat the application doesn't stop.

I have decided (please feel free to comment if you think I am right or wrong about this) that VB's multithreading is not very good, but because I need it, I have come up with a bit of a hack solution.

I now have two exe's. The main user program is where the user selects the files to read, and selects where to write the new files. This exe then uses the shell function to open a second Exe and passes all the values to it as command line arguments. This works pretty well because I can open multiple copies of the second exe and they all perform their task in the background, and, if the user closes the main program, the second exe can still perform it's function (it's set to invisible).

The problem I have now is that I need to pass file names to the new exe. Because there may be more than one, I store them in an array.

Does anyone know how to pass an array to an exe you are opening using the shell function?

Thanks
Do the same way as you do but create the second app as ActiveX EXE. You can create a class in your ActiveX EXE and allow to receive file name


In your app

dim app as new activeXEXE

app.filename filename


in your ActiveX EXE,
you can set up timer and enable it when you have to receive filename
In that case, the caller will not block


ASKER CERTIFIED SOLUTION
Avatar of emadat
emadat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is just an explanation to what I've posted in my previous example.

Your origingl problem is that your code is very slow and that's why you need to put it in a background thread which is a very good approach.

But before going there you should optimize your algorithm.

Reading a file byte-by-byte is the worst thing you can do.
You are doing this because you need to do some work on each byte you are reading.

So rather than reading the bytes fro the file; why don't we read the whole file into a Byes array, and loop through the array to do what we need to do in there; and then write the whole modified array into a new file?

This is what I introduced in my example.
It is very fast. I've tried it on a 3 MB file doing some calculations inside the loop; and it took 1.932 seconds to complete.

Here is the "dummy" calcualtions that I'v done:

    For N = 1 To FLength
        If byteArr(N) > 127 Then
            byteArr(N) = byteArr(N) - 127
        Else
            byteArr(N) = byteArr(N) + 127
        End If
    Next N


A last word to say; Visual Basic is not designed with multithreading into account; and you will need to put so much effort to synchronize your thread with your original process.

The code below stores the arguments in an array if you seperate your arguments by space, for example if you call your exe like

yourexe.exe filename1 filename2 ....

or change the code how you want



Function GetCommandLine(Optional MaxArgs)
   'Declare variables.
   Dim C, CmdLine, CmdLnLen, InArg, i, NumArgs
   'See if MaxArgs was provided.
   If IsMissing(MaxArgs) Then MaxArgs = 10
   'Make array of the correct size.
   ReDim ArgArray(MaxArgs)
   NumArgs = 0: InArg = False
   ArgArray(0) = NumArgs
   'Get command line arguments.
   CmdLine = Command()
   CmdLnLen = Len(CmdLine)
   'Go thru command line one character
   'at a time.
   For i = 1 To CmdLnLen
      C = Mid(CmdLine, i, 1)
      'Test for space or tab.
      If (C <> " " And C <> vbTab) Then
         'Neither space nor tab.
         'Test if already in argument.
         If Not InArg Then
         'New argument begins.
         'Test for too many arguments.
            If NumArgs = MaxArgs Then Exit For
            NumArgs = NumArgs + 1
            InArg = True
         End If
         'Concatenate character to current argument.
         ArgArray(NumArgs) = ArgArray(NumArgs) & C
         ArgArray(0) = NumArgs
      Else
         'Found a space or tab.
         'Set InArg flag to False.
         InArg = False
      End If
   Next i
   'Resize array just enough to hold arguments.
   ReDim Preserve ArgArray(NumArgs)
   'Return Array in Function name.
   GetCommandLine = ArgArray()
End Function
Avatar of JSMCM

ASKER

Hi guys, thanks for the reponses. You guys rock! I will try out your suggestions and let you know what happens. You probably won't hear from me for a few days though as I am in the process of writing some exams as well.

Thanks again
John
Avatar of JSMCM

ASKER

Hi emadat
Thanks so much, this is brilliant! I tried it too, and it reduced the time from about five minutes for a 2MB to under 10 seconds!

If I now stick these API calls into a seperate thread my applications going to work super fast.

Thanks very much

John
Johannesburg
South Africa
Glad I could help
Avatar of JSMCM

ASKER

Hi Emadat, One quick question.

If you try to use the "hOrgFile = CreateFile(fInput, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)", and the file already exists, what happens? Does it overwrite it, or does it just ignore it?

Thanks,
John
John, I will post the help for the "Careate File" function here, and it should be good for the explanation.

However, I'm still ready for further explanation if required.

=======================================================
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
=======================================================

===================
7 lpFileName
===================
Points to a null-terminated string that specifies the name of the object (file, pipe, mailslot, communications resource, disk device, console, or directory) to create or open.
If *lpFileName is a path, there is a default string size limit of MAX_PATH characters. This limit is related to how the CreateFile function parses paths.
Windows NT: You can use paths longer than MAX_PATH characters by calling the wide (W) version of CreateFile and prepending \\?\ to the path. The \\?\ tells the function to turn off path parsing. This lets you use paths that are nearly 32,000 Unicode characters long. You must use fully-qualified paths with this technique. This also works with UNC names. The \\?\ is ignored as part of the path. For example, \\?\C:\myworld\private is seen as C:\myworld\private, and \\?\UNC\tom_1\hotstuff\coolapps is seen as \\tom_1\hotstuff\coolapps.

===================
7 dwDesiredAccess
===================
Specifies the type of access to the object. An application can obtain read access, write access, read-write access, or device query access. This parameter can be any combination of the following values.
0
 Specifies device query access to the object. An application can query device attributes without accessing the device.
GENERIC_READ
 Specifies read access to the object. Data can be read from the file and the file pointer can be moved. Combine with GENERIC_WRITE for read-write access.
GENERIC_WRITE
 Specifies write access to the object. Data can be written to the file and the file pointer can be moved. Combine with GENERIC_READ for read-write access.

===================
7 dwShareMode
===================
Set of bit flags that specifies how the object can be shared. If dwShareMode is 0, the object cannot be shared. Subsequent open operations on the object will fail, until the handle is closed.
To share the object, use a combination of one or more of the following values:
FILE_SHARE_DELETE
 Windows NT only: Subsequent open operations on the object will succeed only if delete access is requested.
FILE_SHARE_READ
 Subsequent open operations on the object will succeed only if read access is requested.
FILE_SHARE_WRITE
 Subsequent open operations on the object will succeed only if write access is requested.

===================
7 lpSecurityAttributes
===================
Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpSecurityAttributes is NULL, the handle cannot be inherited.
Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the object. If lpSecurityAttributes is NULL, the object gets a default security descriptor. The target file system must support security on files and directories for this parameter to have an effect on files.
Windows 95: The lpSecurityDescriptor member of the structure is ignored.

===================
7 dwCreationDistribution
===================
Specifies which action to take on files that exist, and which action to take when files do not exist. For more information about this parameter, see the Remarks section. This parameter must be one of the following values:
CREATE_NEW
 Creates a new file. The function fails if the specified file already exists.
CREATE_ALWAYS
 Creates a new file. The function overwrites the file if it exists.
OPEN_EXISTING
 Opens the file. The function fails if the file does not exist.
 See the Remarks section for a discussion of why you should use the OPEN_EXISTING flag if you are using the CreateFile function for devices, including the console.
OPEN_ALWAYS
 Opens the file, if it exists. If the file does not exist, the function creates the file as if dwCreationDistribution were CREATE_NEW.
TRUNCATE_EXISTING
 Opens the file. Once opened, the file is truncated so that its size is zero bytes. The calling process must open the file with at least GENERIC_WRITE access. The function fails if the file does not exist.

===================
7 dwFlagsAndAttributes
===================
Specifies the file attributes and flags for the file.
Any combination of the following attributes is acceptable for the dwFlagsAndAttributes parameter, except all other file attributes override FILE_ATTRIBUTE_NORMAL.
FILE_ATTRIBUTE_ARCHIVE
 The file should be archived. Applications use this attribute to mark files for backup or removal.
FILE_ATTRIBUTE_COMPRESSED
 The file or directory is compressed. For a file, this means that all of the data in the file is compressed. For a directory, this means that compression is the default for newly created files and subdirectories.
FILE_ATTRIBUTE_HIDDEN
 The file is hidden. It is not to be included in an ordinary directory listing.
FILE_ATTRIBUTE_NORMAL
 The file has no other attributes set. This attribute is valid only if used alone.
FILE_ATTRIBUTE_OFFLINE
 The data of the file is not immediately available. Indicates that the file data has been physically moved to offline storage.
FILE_ATTRIBUTE_READONLY
 The file is read only. Applications can read the file but cannot write to it or delete it.
FILE_ATTRIBUTE_SYSTEM
 The file is part of or is used exclusively by the operating system.
FILE_ATTRIBUTE_TEMPORARY
 The file is being used for temporary storage. File systems attempt to keep all of the data in memory for quicker access rather than flushing the data back to mass storage. A temporary file should be deleted by the application as soon as it is no longer needed.

Any combination of the following flags is acceptable for the dwFlagsAndAttributes parameter.
FILE_FLAG_WRITE_THROUGH
 Instructs the system to write through any intermediate cache and go directly to disk. Windows can still cache write operations, but cannot lazily flush them.
FILE_FLAG_OVERLAPPED
 Instructs the system to initialize the object, so that operations that take a significant amount of time to process return ERROR_IO_PENDING. When the operation is finished, the specified event is set to the signaled state.
 When you specify FILE_FLAG_OVERLAPPED, the ReadFile and WriteFile functions must specify an OVERLAPPED structure. That is, when FILE_FLAG_OVERLAPPED is specified, an application must perform overlapped reading and writing.
 When FILE_FLAG_OVERLAPPED is specified, the system does not maintain the file pointer. The file position must be passed as part of the lpOverlapped parameter (pointing to an OVERLAPPED structure) to the ReadFile and WriteFile functions.
 This flag also enables more than one operation to be performed simultaneously with the handle (a simultaneous read and write operation, for example).
FILE_FLAG_NO_BUFFERING
 Instructs the system to open the file with no intermediate buffering or caching. When combined with FILE_FLAG_OVERLAPPED, the flag gives maximum asynchronous performance, because the I/O does not rely on the synchronous operations of the memory manager. However, some I/O operations will take longer, because data is not being held in the cache.
 An application must meet certain requirements when working with files opened with FILE_FLAG_NO_BUFFERING:
 7  File access must begin at byte offsets within the file that are integer multiples of the volumes sector size.
 7  File access must be for numbers of bytes that are integer multiples of the volumes sector size. For example, if the sector size is 512 bytes, an application can request reads and writes of 512, 1024, or 2048 bytes, but not of 335, 981, or 7171 bytes.
 7  Buffer addresses for read and write operations must be aligned on addresses in memory that are integer multiples of the volumes sector size.
 One way to align buffers on integer multiples of the volume sector size is to use VirtualAlloc to allocate the buffers. It allocates memory that is aligned on addresses that are integer multiples of the operating systems memory page size. Because both memory page and volume sector sizes are powers of 2, this memory is also aligned on addresses that are integer multiples of a volumes sector size.
 An application can determine a volumes sector size by calling the GetDiskFreeSpace function.
FILE_FLAG_RANDOM_ACCESS
 Indicates that the file is accessed randomly. The system can use this as a hint to optimize file caching.
FILE_FLAG_SEQUENTIAL_SCAN
 Indicates that the file is to be accessed sequentially from beginning to end. The system can use this as a hint to optimize file caching. If an application moves the file pointer for random access, optimum caching may not occur; however, correct operation is still guaranteed.
 Specifying this flag can increase performance for applications that read large files using sequential access. Performance gains can be even more noticeable for applications that read large files mostly sequentially, but occasionally skip over small ranges of bytes.
FILE_FLAG_DELETE_ON_CLOSE
 Indicates that the operating system is to delete the file immediately after all of its handles have been closed, not just the handle for which you specified FILE_FLAG_DELETE_ON_CLOSE.
 Subsequent open requests for the file will fail, unless FILE_SHARE_DELETE is used.
FILE_FLAG_BACKUP_SEMANTICS
 Windows NT only: Indicates that the file is being opened or created for a backup or restore operation. The operating system ensures that the calling process overrides file security checks, provided it has the necessary permission to do so. The relevant permissions are SE_BACKUP_NAME and SE_RESTORE_NAME.
 You can also set this flag to obtain a handle to a directory. A directory handle can be passed to some Win32 functions in place of a file handle.
FILE_FLAG_POSIX_SEMANTICS
 Indicates that the file is to be accessed according to POSIX rules. This includes allowing multiple files with names, differing only in case, for file systems that support such naming. Use care when using this option because files created with this flag may not be accessible by applications written for MS-DOS or Windows.

If the CreateFile function opens the client side of a named pipe, the dwFlagsAndAttributes parameter can also contain Security Quality of Service information. When the calling application specifies the SECURITY_SQOS_PRESENT flag, the dwFlagsAndAttributes parameter can contain one or more of the following values:
SECURITY_ANONYMOUS
 Specifies to impersonate the client at the Anonymous impersonation level.
SECURITY_IDENTIFICATION
 Specifies to impersonate the client at the Identification impersonation level.
SECURITY_IMPERSONATION
 Specifies to impersonate the client at the Impersonation impersonation level.
SECURITY_DELEGATION
 Specifies to impersonate the client at the Delegation impersonation level.
SECURITY_CONTEXT_TRACKING
 Specifies that the security tracking mode is dynamic. If this flag is not specified, Security Tracking Mode is static.
SECURITY_EFFECTIVE_ONLY
 Specifies that only the enabled aspects of the clients security context are available to the server. If you do not specify this flag, all aspects of the clients security context are available.
 This flag allows the client to limit the groups and privileges that a server can use while impersonating the client.

===================
7 hTemplateFile
===================
Specifies a handle with GENERIC_READ access to a template file. The template file supplies file attributes and extended attributes for the file being created.
Windows 95: This value must be NULL. If you supply a handle under Windows 95, the call fails and GetLastError returns ERROR_NOT_SUPPORTED.