need to optimize this code...badly

Started by
24 comments, last by docyoung83 19 years, 10 months ago
Well, it''s officially something wrong with my GUI and not the actual loading code. I wrote a console app that loaded in the same file that my GUI loads in, and it completed everything in about 5 seconds. I inserted the EXACT same code into the GUI program and ran it, and it took no less that 57 seconds. Something is horribly wrong with my GUI and I don''t know what. Nevertheless, I don''t have the time/patience to worry about figuring it out, since I know that by sticking it in a thread it will run in 5 seconds (or faster) like the console version. Unforunately, this brings up a slightly different problem. After completely getting rid of the threads and doing it my way, I''ve discovered that I''ve changed the program so much that it is no simple task just reverting back to the old code. Borland is great for drag-and-drop programming in some respects, but it others it just straight pisses me off.
Advertisement
It might be a result of the input message queue becomming full. Because you are maxing your main thread out, the windows messages get piled up in your program''s message queue. This can cause all kinds of trouble. Any lengthy processing, even 5 seconds or more, really, you should put in a second thread so that your main app can handle messages until you are done.
Well, maybe somebody can help me out with this really quick. It is only a syntax error, but after too many pointers start getting thrown into the mix I have a difficult time keeping everything straight. I'm having trouble setting the correct parameter for lpStartAddress as well as the receiving parameter for the function.

class TImage { .... };int main(){    TImage *image = new TImage();    StartThreads(image);}void StartThreads(TImage *image){    HANDLE loadThread;    loadThread = CreateThread(NULL, 0, ThreadLoadPic, (LPVOID)(ℑ), CREATE_SUSPENDED, NULL);    SetThreadPriority(loadThread, THREAD_PRIORITY_HIGHEST);    ResumeThread(loadThread);    WaitForSingleObject(loadThread, INFINITE);    CloseHandle(loadThread);}DWORD WINAPI ThreadLoadPic(LPVOID theImage){    // read in file and set data in the original TImage object    (TImage *)theImage->Size = "FILE_SIZE";    (TImage *)theImage->FileName = "FILE_NAME";        // etc etc etc}  


How should I be casting the lpStartAddress parameter for the CreateThread function, and how should I be receiving it and using it in the ThreadLoadPic function? Thanks.

[edited by - StonieJ on June 9, 2004 10:46:11 PM]
Don''t raise the thread priority to highest just cause you can, first off. The scheduler will allot enough time for your processing thread.

You want:
loadThread = CreateThread(NULL, 0, ThreadLoadPic, (LPVOID)image, CREATE_SUSPENDED, NULL);
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Put the function ThreadLoadPic before StartThreads (or prototype the ThreadLoadPic function, but I prefer idea of definition is declaration) so that StartTreads doesn''t assume ThreadLoadPic is of type ''int ThreadLoadPic (void)'' (or whatever the default prototype is).

Skizz
On the loosing your original code part you should check your source into a SCCS or make a copy of your source tree whenever you stabilize some functionality or before you embark on a radical change.

Doing that has saved my butt countless times!
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }

This topic is closed to new replies.

Advertisement