When my program execute a Proc (DoIt proc), this proc take 32 sec in finish all operations (read a listview and write the read bytes in a File). The listview is very large and 32 sec is OK for me.
But I tried execute this Proc like a thread (CreateThread, NULL,NULL, ADDR DoIt......), and the thread takes 2 min 30 sec for the same Job.
I make the thread in a new dialog that show a progress bar with the state of the file:
;===============================================================
.if uMsg == WM_INITDIALOG
? ? ? ? ?Invoke SendMessageA , hWnd_ListView, LVM_GETITEMCOUNT, NULL, NULL
? ? ? ? ?Invoke SendDlgItemMessage,hWnd,ID_PROGRESS1,PBM_SETRANGE32,0,eax
? ? ? ? ?Invoke SetTimer, hWnd, 1, 1000, NULL? ? ?;start up timer
? ? ? ? ?mov TimerID, eax
? ? ? ? ?Invoke CreateThread,NULL,NULL, ADDR DoIt, hWnd, THREAD_PRIORITY_HIGHEST, ADDR ThreadID
? ? ? ? ?mov ThreadID, eax
? ? ? ? ?Invoke CloseHandle, ThreadID
?.elseif uMsg == WM_TIMER
? ? ? ? Iinvoke SendDlgItemMessage,hWnd, ID_PROGRESS, PBM_SETPOS, Ind_ListView,0? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?;Ind_ListView == The actual item read
?? ? ? ? .IF File_End == TRUE
? ? ? ?? ? ? ? ? invoke KillTimer, hWnd, TimerID2
? ? ? ? ? ? ? ? ?invoke EndDialog, hWnd, NULL
? ? ?? ?.endif?
? ?
.endif
;===============================================================
Why the thread is slow. There is a better idea for show the progress bar?
Thaks to all.
But I tried execute this Proc like a thread (CreateThread, NULL,NULL, ADDR DoIt......), and the thread takes 2 min 30 sec for the same Job.
I make the thread in a new dialog that show a progress bar with the state of the file:
;===============================================================
.if uMsg == WM_INITDIALOG
? ? ? ? ?Invoke SendMessageA , hWnd_ListView, LVM_GETITEMCOUNT, NULL, NULL
? ? ? ? ?Invoke SendDlgItemMessage,hWnd,ID_PROGRESS1,PBM_SETRANGE32,0,eax
? ? ? ? ?Invoke SetTimer, hWnd, 1, 1000, NULL? ? ?;start up timer
? ? ? ? ?mov TimerID, eax
? ? ? ? ?Invoke CreateThread,NULL,NULL, ADDR DoIt, hWnd, THREAD_PRIORITY_HIGHEST, ADDR ThreadID
? ? ? ? ?mov ThreadID, eax
? ? ? ? ?Invoke CloseHandle, ThreadID
?.elseif uMsg == WM_TIMER
? ? ? ? Iinvoke SendDlgItemMessage,hWnd, ID_PROGRESS, PBM_SETPOS, Ind_ListView,0? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?;Ind_ListView == The actual item read
?? ? ? ? .IF File_End == TRUE
? ? ? ?? ? ? ? ? invoke KillTimer, hWnd, TimerID2
? ? ? ? ? ? ? ? ?invoke EndDialog, hWnd, NULL
? ? ?? ?.endif?
? ?
.endif
;===============================================================
Why the thread is slow. There is a better idea for show the progress bar?
Thaks to all.
Try using PostMessage instead of SendDlgItemMessage - and cache the result of GetDlgItem instead of calling it all the time.
Disabling the Listview while you're adding should shave off a couple seconds too.
Thank you very much for the replays.
I tried disabling the timer (and therefore the progress bar don't update), only creating the thread, and the time is the same (aprox 2 min 30 sec).
Is very high the difference between the Proc and the Thread and i don't understand why.
Thanks again.
I tried disabling the timer (and therefore the progress bar don't update), only creating the thread, and the time is the same (aprox 2 min 30 sec).
Is very high the difference between the Proc and the Thread and i don't understand why.
Thanks again.
this may be only because your "main" thread consumes CPU instead of idling ... ? or maybe you create multiple working-threads simultaneously and they're fighting for the CPU ? or maybe just the dialog box somewhat sux when operated by different threads? i never use dialogs...
and after you solve the 'thread' problem, apply the things mentioned by f0dder and JimmyClif.
and after you solve the 'thread' problem, apply the things mentioned by f0dder and JimmyClif.
Morlok, did you try PostMessage? Look up PostMessage and SendMessge (which SendDlgMessage is based upon).
Thanks to all, again.
I am trying "ti_mo_n's ideas", and discarding the dialogs (and therefore without SendDlgMessage).
The program at the start creates a window with this WindProc:
WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
? ? ? .iF uMsg == WM_CLOSE
? ? ? ? ? ? invoke PostQuitMessage,NULL? ? ?
? ? ? .ELSEIF uMsg==WM_CREATE
? ? ? ? ? ?invoke Read_Archive_To_ListView
? ? ? .ELSEIF uMsg==WM_COMMAND
? ? ? ? ? mov eax,wParam
? ? ? ? ? .IF ax == IDM_WITH_THREAD
? ? ? ? ? ? ? ? invoke CreateThread,NULL,NULL, ADDR Write_File_From_ListView , hWnd, THREAD_PRIORITY_HIGHEST, ADDR ThreadID? ? ? ?;THIS TAKE 2 min 08 sec
? ? ? ? ? ? ? ? invoke CloseHandle, eax
? ? ? ? ? .ELSEIF ax == IDM_WITH_INVOKE
? ? ? ? ? ? ? ?invoke Write_File_From_ListView? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ;THIS TAKE 46 sec
? ? ? ? ? .ENDIF
? ? ?.ELSE
? ? ? ? ? ? invoke DefWindowProc,hWnd,uMsg,wParam,lParam
? ? ? ? ? ? ret
? ? ?.ENDIF
? ? ?xor? ? eax,eax
? ? ?ret
WndProc endp
The Proc "Write_File_From_ListView ", only Create a File, Read 2 Listviews (the ListView are not visibles), and Write the file with the content of the listviews.
the write file method is:
? ? ?- @@@
? ? ?- Read an Item and subItems in First Listview
? ? ?- Set file Pointer to the end of the file
? ? ?- Write file
? ? ?- Read some Items and subItems (related to the item in the first list) in the Second Listview
? ? ?- Set file Pointer to the end of the file
? ? ?- Write file
? ? ?- Loop @@@
Now there isn't nothing that disturb the thread in the main window but the time difference remains very high.
Some idea?
Thaks
The Morlok
I am trying "ti_mo_n's ideas", and discarding the dialogs (and therefore without SendDlgMessage).
The program at the start creates a window with this WindProc:
WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
? ? ? .iF uMsg == WM_CLOSE
? ? ? ? ? ? invoke PostQuitMessage,NULL? ? ?
? ? ? .ELSEIF uMsg==WM_CREATE
? ? ? ? ? ?invoke Read_Archive_To_ListView
? ? ? .ELSEIF uMsg==WM_COMMAND
? ? ? ? ? mov eax,wParam
? ? ? ? ? .IF ax == IDM_WITH_THREAD
? ? ? ? ? ? ? ? invoke CreateThread,NULL,NULL, ADDR Write_File_From_ListView , hWnd, THREAD_PRIORITY_HIGHEST, ADDR ThreadID? ? ? ?;THIS TAKE 2 min 08 sec
? ? ? ? ? ? ? ? invoke CloseHandle, eax
? ? ? ? ? .ELSEIF ax == IDM_WITH_INVOKE
? ? ? ? ? ? ? ?invoke Write_File_From_ListView? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ;THIS TAKE 46 sec
? ? ? ? ? .ENDIF
? ? ?.ELSE
? ? ? ? ? ? invoke DefWindowProc,hWnd,uMsg,wParam,lParam
? ? ? ? ? ? ret
? ? ?.ENDIF
? ? ?xor? ? eax,eax
? ? ?ret
WndProc endp
The Proc "Write_File_From_ListView ", only Create a File, Read 2 Listviews (the ListView are not visibles), and Write the file with the content of the listviews.
the write file method is:
? ? ?- @@@
? ? ?- Read an Item and subItems in First Listview
? ? ?- Set file Pointer to the end of the file
? ? ?- Write file
? ? ?- Read some Items and subItems (related to the item in the first list) in the Second Listview
? ? ?- Set file Pointer to the end of the file
? ? ?- Write file
? ? ?- Loop @@@
Now there isn't nothing that disturb the thread in the main window but the time difference remains very high.
Some idea?
Thaks
The Morlok
Can you post some source code which we can use where this delay happens... I don't feel like re-creating your scenario from scratch.
Cheers,
JC
Cheers,
JC
Ok, i made a skeleton program, that fill 2 listviews (with dummy data), and write a dummy file with their contents.
there are 2 buttons, to chosse between Invoke or Thread.
My Results:
Invoke ---> 13 secs
Thread ---> 41 secs
Invoke is 3 times more quickly than Create Thread.
You can modify the total items with :
MAX_ITEMS EQU 1000
Thanks for yours ideas, but remenber that the principal is the time difference.
The Morlok
PD:Really i don't have a good programming style, but that is only a fast skeleton
there are 2 buttons, to chosse between Invoke or Thread.
My Results:
Invoke ---> 13 secs
Thread ---> 41 secs
Invoke is 3 times more quickly than Create Thread.
You can modify the total items with :
MAX_ITEMS EQU 1000
Thanks for yours ideas, but remenber that the principal is the time difference.
The Morlok
PD:Really i don't have a good programming style, but that is only a fast skeleton
Ah doh, PostMessage is not an option in your case, pardon my blunder.
I would suggest not storing the data in a listview, but instead use a virtual listview.
I suspect the reason threading slows this down is because SendMessage is synchroneous, and thus require a context switch from the worker-thread to the messageloop thread and back to the worker-thread again. Context switches are expensive, and certainly something you'll want to avoid in an intensive datacrunching situation.
I would suggest not storing the data in a listview, but instead use a virtual listview.
I suspect the reason threading slows this down is because SendMessage is synchroneous, and thus require a context switch from the worker-thread to the messageloop thread and back to the worker-thread again. Context switches are expensive, and certainly something you'll want to avoid in an intensive datacrunching situation.
ok fOdder, I will try your suggestions. (virtual list and not threading)
Thanks.
Thanks.
threading is fine until you use virtual listview
threading is fine until you use virtual listview
What do you mean, "until"?
Btw, as for using a virtual listview, I of course mean so you can access data directly from the local copy, instead of having to send messages. Threading is fine, but the message sending overhead and synchroneous behaviour kills performance in your example.
Btw, as for using a virtual listview, I of course mean so you can access data directly from the local copy, instead of having to send messages. Threading is fine, but the message sending overhead and synchroneous behaviour kills performance in your example.
That's exactly what I meant :)
That overhead is the same if you're using a regular listview - the advantage of a listview in virtual mode is that you don't have to send messages around to access the data, you have direct access to your data structures :)