Solved

NTservice is running but not calling my Worker thread

Posted on 2002-05-24
13
252 Views
Last Modified: 2010-04-02
Here is the run method of my service,

void CTest::Run(DWORD dwArgc, LPTSTR * ppszArgv) {

     // report to the SCM that we're about to start
     ReportStatus(SERVICE_START_PENDING);

     m_hStop = ::CreateEvent(0, TRUE, FALSE, 0);
     m_hPause = ::CreateEvent(0, TRUE, FALSE, 0);
     m_hContinue = ::CreateEvent(0, TRUE, FALSE, 0);

     ReportStatus(SERVICE_RUNNING);
     
                       
     while( ::WaitForSingleObject(m_hStop, 10) != WAIT_OBJECT_0 )
     {
          // if the service got a "pause" request, wait until the user
          // wants to "continue".
          if(::WaitForSingleObject(m_hPause, 5) == WAIT_OBJECT_0)
          {
               while(::WaitForSingleObject(m_hContinue, 50) != WAIT_OBJECT_0)
                    if(::WaitForSingleObject(m_hPause, 50) == WAIT_OBJECT_0)
                         goto Stop;
               ::ResetEvent(m_hPause);
               ::ResetEvent(m_hContinue);
          }
          AddToMessageLog(TEXT("Service working ....Before Thread"),EVENTLOG_INFORMATION_TYPE);
// above meesage is comming into event message log

// this below statement is not executed at all
 AfxBeginThread(MyFunc,NULL);

         
     }
         
Stop:


     if( m_hStop )
          ::CloseHandle(m_hStop);
     if(m_hPause)
          ::CloseHandle(m_hPause);
     if(m_hContinue)
          ::CloseHandle(m_hContinue);
}




// this below statement is not executed at all
 AfxBeginThread(MyFunc,NULL);

why???
0
Comment
Question by:srithi
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7032464
Hmm, have you tried

AddToMessageLog(TEXT("Service working ....Before Thread"),EVENTLOG_INFORMATION_TYPE);
// above meesage is comming into event message log

// this below statement is not executed at all
AfxBeginThread(MyFunc,NULL);

AddToMessageLog(TEXT("Service working ....AFTER Thread"),EVENTLOG_INFORMATION_TYPE);

?

And, what is 'MyFunc()' like?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7033536
How do you know that that line is not working?

You might be under the misconception that execution control will transfer to

  MyFunc

and keep running there for a while.  But instead, the AfxBeginThread() fn is called aand returns immediately.

-- Dan
0
 
LVL 1

Author Comment

by:srithi
ID: 7034051
i have increased the points by 50.


jkr,

// this below statement is not executed at all
AfxBeginThread(MyFunc,NULL);

>>AddToMessageLog(TEXT("Service working ....AFTER >>Thread"),EVENTLOG_INFORMATION_TYPE);

>>?
yeah i have put this ,but no message is comming into log.

Myfunc is a simple thread proc ,
UINT MyFunc(LPVOID lp)
{
// I WANT TO DO SOME FILE TRANSFERS FROM HERE

          return 1;
}

dan rollins,
as i said above its not calling the add message log funtion which i have put after AfxBeginThread,
as you said if the service just calls AfxBeginThread and returns,will it start the worker thread or not,can u give me some test code by which i can see whether Myfunc is executed or not.


0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7034065
You have a lot of unneeded complications if all you want to do is get some test code running... All of that Creating events and Waiting for events seems unnessicarity complicated for a first-draft.

>>can u give me some test code by which i can see whether Myfunc is executed or not.

I would just sprinkle some of these around:

    AfxMessageBox( "About to do Xxxxxxx" );
        ...
    AfxMessageBox( "Done with Xxxxxxx" );

    AfxMessageBox( "In the thread Proc" );
        ...
    AfxMessageBox( "Thread is exiting" );

=-=-=-=-=-=-=-
Also check your program flow... Remember that if the program exits before the thread gets going, the code in the thread will never execute.

-- Dan
0
 
LVL 1

Author Comment

by:srithi
ID: 7034091
can we have messageboxes in a service???,i tried to put some in mine but no message pops up.



>>Also check your program flow... Remember that if the >>program exits before the thread gets going, the
>>code in the thread will never execute.

my program does not terminate until i give a stop command
but still the thread does not work,
and istead of starting thread, i tried with a simple code which opens a file and writes something,but even this code is not executed.

thankx.



0
 
LVL 1

Expert Comment

by:senohp
ID: 7034133
Place a MessageBeep(-1) in the MyFunc; so you can hear any beep in the PC speaker when the function run.
You service will produce beep at 10 ms interval.

UINT MyFunc(LPVOID lp)
{
  MessageBeep (-1) ;
  return 0 ;
}

I guest the fault is in AddToMessageLog. The function generate an exception that is not handled and terminate the whole process.
If the AddToMessageLog is OK, your program will add many lines to the event log.  

Regards

Seno Hardijanto
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 49

Expert Comment

by:DanRollins
ID: 7034161
Message box should work if you set the Service to "Allow service to interact with the desktop"  It is in the "Log On" tab of the Properties for the service, which is in Services, which is in the Windows Control Panel.

-- Dan
0
 
LVL 1

Author Comment

by:srithi
ID: 7037402
points increased by 50.

but i tried something like this
i created the thread as suspended and next called REsume thread ,and it worked,now from the loop its calling the thread ,but now the trouble is ,i have placed some code to open a text file and write something and close using CStdioFile object ,but this code is not getting executed,after some i am getting one assertion,i am unable to fix this,plz help me out.
thank you all.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7037411
>>can we have messageboxes in a service???,

Yes - all you need is to set MB_SERVICE_NOTIFICATION. Alternatively, use 'OutputDebugString()' and a viewer like e.g. http://www.sysinternals.com/ntw2k/freeware/debugview.shtml ("DebugView").
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 150 total points
ID: 7037886
Make sure that the code that you execute works in a normal situation.  That is, write a simple dialog-based or console-style application and test your code thoroughly in that environment.  Once you are certain that the code works, copy it into the Service.

Thanks for taking the time to read my humble suggestions.

-- Dan
0
 
LVL 1

Author Comment

by:srithi
ID: 7038534
dan Rollins,
thankx for the help,
can you suggest me ,a good book on NTServices ,
i will accept your next comment as answer.

thank you to all experts.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7038575
sorry, I'm not big on book larn'in

I learned Services through the comprehensive information in MDSN and by using the AppWizard to create a Service, and then examining the code that it generated.

-- Dan
0
 
LVL 1

Author Comment

by:srithi
ID: 7038621
thankx dan.and thakx to all experts.

if u find some book plz pass the info to me.
my id is seshamsri@yahoo.com.

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now