473,385 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

thread appears to run after SetEvent

131 128KB
I don't know if I've setup the thread correctly or not, but its behavoir isn't what I'd expect. I'm creating the thread within a BN_CLICKED message

Expand|Select|Wrap|Line Numbers
  1. typedef struct my_TNFO
  2. {
  3.     HANDLE h_event;
  4.     HANDLE h_thread;
  5.     DWORD  thread_id;
  6.     BOOL   running;
  7.     BOOL   init;
  8.     BOOL   canceled;
  9. } TNFO;   
  10.  


Expand|Select|Wrap|Line Numbers
  1. ...
  2.    if(HIWORD(wParam) == BN_CLICKED)
  3.    {
  4.        memset(&tnfo, 0, sizeof(tnfo));
  5.  
  6.        tnfo.running = TRUE;   // thread active
  7.        tnfo.init = TRUE;      // thread started
  8.        tnfo.canceled = FALSE; // user controlled 
  9.  
  10.        tnfo.h_event = CreateEvent(NULL, TRUE, FALSE, NULL);
  11.        tnfo.h_thread = CreateThread(...);
  12.        ...
  13.     }
  14. ...
  15.  
the thread function actually calls a recursive directory parser function. I've set it up as

Expand|Select|Wrap|Line Numbers
  1. DWORD WINAPI threads_ParseDisc(LPVOID lParam)
  2. {
  3.     TNFO *tmp = (TNFO *)lParam;
  4.  
  5.     if(tmp->init == TRUE)
  6.     {
  7.         tmp->init = FALSE;        
  8.         // call recursive function        
  9.     }
  10.  
  11.     while(WaitForSingleObject(tmp->h_event, INFINITE) != WAIT_OBJECT_0)
  12.         Sleep(1000);
  13.  
  14.     CloseHandle(tmp->h_event);
  15.     CloseHandle(tmp->h_thread);
  16.     tmp->running = FALSE;
  17.  
  18.     return 0;
  19. }
in the recursive function, I've tried both Send and PostMessage.

Expand|Select|Wrap|Line Numbers
  1. ...
  2. HANDLE hFind = INVALID_HANDLE_VALUE;
  3.  
  4. hFind = FindFirstFile(tmp, &wfd);
  5.  
  6. if(hFind == INVALID_HANDLE_VALUE)
  7. {
  8.     PostMessage(hwnd_to_main, WMU_DISC_DONE, 0, 0);
  9.         return;
  10. }
  11.  
  12. if(tmp->canceled == TRUE)
  13. {
  14.     PostMessage(hwnd_to_main, WMU_DISC_CANCEL, 0, 0);       
  15.         return;
  16. }
  17.  
  18. do
  19. {
  20.    // if directory, call self... (path, tnfo);
  21.    // if file, populate list view control
  22.  
  23. } while(FindNextFile(...) != 0);
  24.  
  25. FindClose(hFind);
  26.  
  27. ++progd->count;
  28. PostMessage(hwnd_to_main, WMU_DISC_COUNT, (WPARAM)0, (LPARAM)(ULONG)progd->count);
  29.  
progd->count was devised to stop the thread when the recursion ended by posting a message to the main window. When it reaches 1, the def procedure sends the WMU_DISC_DONE message to set the event

All that *appears* to work; however, if I cancel the recursion thread, the list view continues to populate for a bit and then deletels all but 1 item though I send the message LVM_DELETEALLITEMS.

If I move the (tmp->canceled == TRUE) test inside of the do-while, the app gets loopy. Any idea as to why this behavoir occurs?

If more information is needed, please ask.

TIA
Apr 28 '14 #1

✓ answered by weaknessforcats

I guess you would save the current tab in a stack. You could pop/push around the various tabs.

4 1523
weaknessforcats
9,208 Expert Mod 8TB
OK. You have created an event object and a thread object and the thread is listening for an event. Where is the event?

I think you need to call SetEvent to put your event object into the signaled state. Otherwise, WaitForSingleObject will hear nothing.
Apr 29 '14 #2
divideby0
131 128KB
Thank you for the reply... as always, I appreciate your time and feedback.

The problem went away after moving the (tmp->canceled == TRUE) test to the bottom of the do-while. I still have no clue as to way the program bogged down if that test was at the top of the loop.

SetEvent was called within one of the WM_USER messages. The thread ended, but had a sort of delayed effect before. Moving the canceled test "fixed it."

Do you know of a way store the previously selected tab of a tab control? All I can get is the current selection, which isn't helpful for what I need to do.

TIA
Apr 29 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
I guess you would save the current tab in a stack. You could pop/push around the various tabs.
Apr 30 '14 #4
divideby0
131 128KB
I wish that I could learn to think like a programmer; thank you for that! Just what it needed. :)

Cheers
Apr 30 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: John Doe | last post by:
Hi all, I know the standard doesn't care about threads (wrongly :-) But in current compilers implementation, is the "list" which holds count of the occupied and free heap addresses SHARED among...
2
by: Gionni | last post by:
I have to translate this code in VB6 in C#: Declare Function OpenEvent Lib "kernel32.dll" Alias "OpenEventA" _ (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _ ByVal lpName As...
3
by: J.Marsch | last post by:
Issue: I have 3 threads, syncing with a ReaderWriterLock (in "real life", there will be more). Thread 1 (there could be any number of these) Gets a read lock with infinite timeout. It runs in...
11
by: Hardy Wang | last post by:
Hi, I have a windows form application, and there are 2 buttons in the form. In first button's click event I have code like: Thread t = new Thread(new ThreadStart(fileProcessor)); t.Start(); To...
3
by: Stephen Miller | last post by:
I have an ASP.Net application that sends a NetworkStream to a .Net Service, which has a TcpListener listening on a port for the ASP.Net client. When it receives a request it creates a new thread...
6
by: k.mellor | last post by:
Hi, I hope someone can help. I have written a simple form to demonstrate my problem/question. The code follows. The form starts a thread, which using delegates updates a label (Every second...
1
by: Martin Evans | last post by:
I know this has been seen before but it is not making too much sense (after reading many posts). It all appears to work fine but then dies after about 40 invocations. My app has Python embedded,...
4
by: Donos | last post by:
Hi I have a HANDLE to an Event, like this.. HANDLE h = ::CreateEvent(NULL, FALSE, FALSE, NULL); This is running in one thread in one class. For example we will call that class as "Class A"...
29
by: NvrBst | last post by:
I've read a bit online seeing that two writes are not safe, which I understand, but would 1 thread push()'ing and 1 thread pop()'ing be thread-safe? Basically my situation is the follows: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.