ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   it's a problem about multithread in vba: (https://www.excelbanter.com/excel-programming/303168-its-problem-about-multithread-vba%EF%BC%9A.html)

floyd_cn

it's a problem about multithread in vba:
 
------------------begin macro-----------------------
'btnconfirm is a button on the user interface
Private Sub btnConfirm_Click()
'run the process of analyzing in a new thread
Dim tId As Long, hThread As Long
hThread = CreateThreadL(0, 0, AddressOf DurativeFunc, 0, 0, tId)
End Sub

'the following is parts of module1

Declare Function CreateThreadL Lib "kernel32" Alias "CreateThread" _
(ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress

As Long, _
ByVal lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As

Long

Function DurativeFunc(ByVal lpvThreadParm As Long) As Long
Dim dwResult As Long
dwResult = 0

'problem:
'the following process(in fact it's a timeconsuming process,here just simplified)

work well in a non-multithread function
'but here(as a multithread function) it only modify the content of cells(1,3)
'why???
For i = 1 To 10
ActiveSheet.Cells(i, 3) = i
Next

DurativeFunc = dwResult
End Function
------------------------end macro---------------------
i just want to update the user interface(informing user of what's going on),when a

time-consuming process is running.

anyway if anyone gets time to help i would be very happy.
any other method to solve the problem is appreciated.


All times are GMT +1. The time now is 01:32 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com