Solved

How to move or resize the form during intensive job?

Posted on 1998-12-22
13
138 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now