Improve company productivity with a Business Account.Sign Up

x
?
Solved

NTservice is running but not calling my Worker thread

Posted on 2002-05-24
13
Medium Priority
?
267 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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 600 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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

606 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