Solved

How to move or resize the form during intensive job?

Posted on 1998-12-22
13
139 Views
Last Modified: 2010-05-03
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?

Thanks

0
Comment
Question by:Torus
13 Comments
 
LVL 13

Accepted Solution

by:
Mirkwood earned 50 total points
ID: 1451915
Here is an example from http://www.hilonet.com/vbthread/ 
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
http://www.microsoft.com/msj/0897/multithreading.htm

0
 
LVL 4

Expert Comment

by:chabaud
ID: 1451916
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.
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 1451917
;
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 2

Author Comment

by:Torus
ID: 1451918
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.



0
 
LVL 2

Author Comment

by:Torus
ID: 1451919
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?

Thanks
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1451920
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 .
0
 
LVL 2

Author Comment

by:Torus
ID: 1451921
SOrry, one more question. Is it possible the thread notifies me when it finishes the job.
Thanks
0
 
LVL 2

Author Comment

by:Torus
ID: 1451922
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
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1451923
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).
0
 
LVL 2

Author Comment

by:Torus
ID: 1451924
I am using NT, VB6. I will test the other later. But can it notifies me when finished?
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1451925
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.
0
 
LVL 2

Author Comment

by:Torus
ID: 1451926
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.

CAUSE

 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.
0
 
LVL 2

Author Comment

by:Torus
ID: 1451927
The article ID is Q198607. For more information, you can go to see it.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question