Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 147
  • Last Modified:

How to move or resize the form during intensive job?

I wrote a program that should do some intensive job, e.g read a large file and then analyse the data. I have added a
Doevents in the function that does the intensive job.  
It is no problem if I move the form after the program enters
the function. However, if I move the form and once the function starts executing, the form is frozen until the function finishes. It seems the Doevents is no use at this situation. Why? How can I solve this problem?


1 Solution
Here is an example from 
The code
1.Assuming that we have an application that among other things can copy a file on the click of a button.

2.The copy button could have code like this:

Private Sub cmdCopyFile_Click()
Dim lpThreadID As Long
Dim hThread As Long

'spawn a new thread of execution starting with the AsyncFileCopy function
hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf AsyncFileCopy, ByVal 0&, ByVal 0&, lpThreadID)
CloseHandle hThread 'clean up after ourselves
End Sub

3.Code this for you module:

Option Explicit

Public gsOriginalFile As String
Public gsDuplicateFile As String
Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hHandle As Long) As Long

Public Function AsyncFileCopy() As Long
'Assuming that gsOriginalFile & gsDuplicateFile
'are valid path/file names set elsewhere.

FileCopy gsOriginalFile, gsDuplicateFile
End Function

See also

I don't understand your problem.

Are you sure you call doEvents regularly in your function.
The DoEvents statement allow the windows messages to be processed by your appli or other appli including the paint message.
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

TorusAuthor Commented:
Hi chabaud, I am sure that I add the Doevents in the loop on the function that does intensive job. My problem is, for Examples, if the function that does the intensive job called  "TEST".

If now the program is executing the "TEST" function, I can move the form everywhere on the screen.  However, if the program now run the other code before calling "TEST"(the code before is not intensive), I can still move the form, but while I moved it and once the "TEST" is called, the form is frozen at once and I can't move it anymore until the "TEST" completes.

TorusAuthor Commented:
Mirkwood, if I want the function that used to create the thread has more than 1 Parameter or for example, return the result from a complex calculation. Can I do that since I just see that the createthread just allow to pass one argument?

You can pass a instance of a struct as parameter and some of these values can be for meant as output and some for input.
You can also pass an instance of a class .
TorusAuthor Commented:
SOrry, one more question. Is it possible the thread notifies me when it finishes the job.
TorusAuthor Commented:
Mirkwood, I tested the program in the design environment and that's no problem. However, once I compiled it to exe and run it today every time, Dr.Watson appears
Hmm, it works ok with me. Have you also tried the other MSJ article I recommended. Does that crash to. Are you using win95, btw?
Maybe we should just go for the timer solution. That one is easier (altough not as good).
TorusAuthor Commented:
I am using NT, VB6. I will test the other later. But can it notifies me when finished?
Sure, you execute a function. At the end of this function you can do anything you want: Raise an event of a class or call another function. The correct way to do it ofcourse is with semafoors. No example available though.
TorusAuthor Commented:
Mirkwood, I found a reference from microsoft article about CreateThread

When running your Visual Basic application, you may encounter an error similar to the following:

The instruction at 0x660bd3b1 referenced memory at 0x0000009c. The
memory could not be written.

This error may occur immediately following a declared API function call within a Visual Basic callback function.


 When the error occurs, the callback function is called by a thread that was not created by Visual Basic.

For instance, certain API calls such as CreateThread() and RegisterServiceCtrlHandler() will create an additional thread before invoking the callback function that was
previously setup by using the AddressOf operator. Although the same code may have worked in previous versions of Visual Basic, calling API's of this nature are currently
unsupported in Visual Basic.

Furthermore, additional care must be taken with regards to what code can be executed within the callback function. Any use of the following within the callback function may
cause undesirable results:

          File I/O.
          Error handling.
          Fixed size arrays.
          Set statements.
          COM method calls that return HRESULTs (such as any Visual Basic ActiveX           object).
          Declare calls.
          Global objects such as the Application object.
          Most of the Visual Basic run-time files.
TorusAuthor Commented:
The article ID is Q198607. For more information, you can go to see it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now