Advertisement

08.08.2008 at 05:15PM PDT, ID: 23634404
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.8

SendMessage vs PostMessage (and lost messages..)

Asked by ahalya in Delphi Programming, Windows Programming

Tags: , ,

I have a thread communicating with a window via messages.

I want to use PostMessage, but for some reason I seem to lose many of the messages sent via postmessage.  This happens occasionally, and seems to be unpredictable.  

If I use SendMessage, I receive every message, but am struck if SendMessage is called while I'm waiting inside a WaitForSingleObject. (I use a semaphore and WaitForSingleObject function to determine whether the thread is active or not).

Post Message, I lose many, and it appears the MC_ThreadEnd message is lost most of the time This is the last message sent just before the thread terminates).

I have attached a sample log created from these communications to show how I track the messages. They aretimed tightly (nearest micro second in the field following :: symbol in the file)

comments, Solutions, Alternatives please.  (MY OS is XP SP2 with all patches/updates applied. Compiler Delphi 7 with all patches applied).Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
///////////////////////////////////////////////////////////////////
//  These two procedures process WM_Debug, & WM_Thread messages  //
///////////////////////////////////////////////////////////////////
procedure TMainForm.DebugMessages(var M: TMessage);
 
var ErrMsg: array[0..255] of char;
    StrSize: word;
 
begin;
  StrSize := GlobalGetAtomName(M.LParam, ErrMsg, 256);
  GlobalDeleteAtom(M.LParam);
  if StrSize > 0 then
  begin;
    case M.WParam of
    MC_ErrMsg:    LogInfo(nil, string(ErrMsg));
    MC_DebugMsg : LogInfo(nil, string(ErrMsg));
    MC_MutexMsg : LogInfo(nil, string(ErrMsg));
  end;
else
    TimedMessageDlg('Error message not found !', mtError,[mbOk], 0, 10, mrOk);
end;
 
 
procedure TMainForm.ThreadMessages(var M: TMessage);
 
begin;
  case M.WParam of
    MC_MotorStart : LogInfo(nil, Format('%s stepper motor started', [FeedbackChannelNames[M.LParam - md_ref+countAO]]));
    MC_MotorStop  : LogInfo(nil, Format('%s stepper motor stopped', [FeedbackChannelNames[M.LParam - md_ref+countAO]]));
 
    MC_ThreadCreate: LogInfo(nil, Format('%s. New Thread started', [GetAtomMsg]));
    MC_ThreadQuit  : LogInfo(nil, Format('%s. Thread Quit Request', [GetAtomMsg]));
    MC_ThreadProc  : LogInfo(nil, Format('%s. ThreadProc activated', [GetAtomMsg]));
    MC_ThreadInfo  : LogInfo(nil, Format('%s', [GetAtomMsg]));
    MC_ThreadEnd:
      begin;
        LogInfo(nil, Format('%s. Thread Ended', [GetAtomMsg]));
        dec(ThreadCount);
        IsMultiThread := (ThreadCount > 1);
        CloseHandle(hEPTThread);
        hEPTThread := 0;
      end;
  end;
 
  Caption := Format('HCT DAQ [%d threads active]', [ThreadCount]);
  if (M.WParam = MC_MotorStop) then DAQModule.SetCWButtonState(M.LParam, false);
  if (M.WParam = MC_MotorStart) then DAQModule.SetCWButtonState(M.LParam, true);
end;
 
/////////////////////////////////////////////////////////////////
//   Messages are sent from a separate thread. Example below   //
////////////////////////////////////////////////////////////////
 
<part of the Thread function>
 
    loop := 0; steps := 10;
    while (not TEPTTargets(Ptr^).QuitThread) and (loop < steps) do
    begin;
      updateVariables(steps - loop);//Update if EPTs active
      for EPTransducer := ep_Vert to ep_IntP do ApplyOutput(EPTransducer);
 
      PostMessage(hMainform, WM_Debug, MC_EPTMsg,  AddGlobalAtomFmt('%s <- Calling Sleep', [getPCTAsString]));
      //Break the Sleep command into 4 individual sleeps to avoid a long hang-up
      if not TEPTTargets(Ptr^).QuitThread then sleep(TEPTTargets(Ptr^).TimeDelay div 4);
      if not TEPTTargets(Ptr^).QuitThread then sleep(TEPTTargets(Ptr^).TimeDelay div 4);
      if not TEPTTargets(Ptr^).QuitThread then sleep(TEPTTargets(Ptr^).TimeDelay div 4);
      if not TEPTTargets(Ptr^).QuitThread then sleep(TEPTTargets(Ptr^).TimeDelay - (3 * TEPTTargets(Ptr^).TimeDelay div 4));
      PostMessage(hMainform, WM_Debug, MC_EPTMsg,  AddGlobalAtomFmt('%s -> Done Sleep', [getPCTAsString]));
 
      inc(loop);
    end;
  except on e:exception do
    PostMessage(hMainform, WM_Debug, MC_ErrMsg, GlobalAddAtom(pchar(E.Message)));
    end;
 
  if TEPTTargets(Ptr^).QuitThread then
     PostMessage(hMainform, WM_Thread, MC_ThreadQuit, AddGlobalAtomFmt('%s <EPT>', [getPCTAsString]));
  PostMessage(hMainform, WM_Thread, MC_ThreadEnd, AddGlobalAtomFmt('%s', [getPCTAsString]));
Attachments:
 
Log Created using the messages
 
[+][-]08.09.2008 at 04:48AM PDT, ID: 22195943

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.09.2008 at 05:55AM PDT, ID: 22196150

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.09.2008 at 10:41AM PDT, ID: 22196974

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.09.2008 at 01:37PM PDT, ID: 22197603

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.09.2008 at 01:42PM PDT, ID: 22197620

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.10.2008 at 02:34AM PDT, ID: 22199155

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Delphi Programming, Windows Programming
Tags: Windows API, PostMessage, Delphi
Sign Up Now!
Solution Provided By: bernani
Participating Experts: 3
Solution Grade: A
 
 
[+][-]08.10.2008 at 05:53AM PDT, ID: 22199463

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.10.2008 at 07:27AM PDT, ID: 22199708

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.10.2008 at 11:11PM PDT, ID: 22202093

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-44 / EE_QW_EXPERT_20070906