473,722 Members | 2,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Process.GetCurr entProcess() when starting a service

I have written a C# application which should run as a Windows
Service. I must avoid having multiple instances of the application
on one machine so I inserted the following code at the beginning
of the Main() function:

// if this is not the first instance of this application, exit
// immediately. Allow only one instance in the system
if
(Process.GetPro cessesByName(Pr ocess.GetCurren tProcess().Proc essName).Length >
1)
return;

I found out (After few days of struggle) that this code causes the
following problem when trying to start the service by SCM: After
the service is stopped, the next start attempt always fails with
timout. The following start always succeeds. So, every other
service start attempt fails.

I traced the problem to the GetCurrentProce ss() function and I got
same result with GetProcesses().

I have created a small C# project to demonstrate the problem. If
you need it for reproduction of the issue, just let me know where
to post it.

Thanks in advance,

--
Benzi Eilon
Oct 30 '06 #1
5 4487
Hi Benzi,

Based on my understanding, your Windows Service application has included
the listed code snippet to allow only 1 instance of service to run.
However, you find that after stopping your service, you can not restart it
second time with timeout in Services.msc. If I have misunderstood you,
please feel free to tell me, thanks.

What version of .Net Framework you are using, .Net1.1 or 2.0? I have
created a sample Windows Service in VS2005 by adding your listed code
snippet in the main method. However, it can be stopped and restarted
several times without timeout error.

Also, your code logic actually uses the process name as the identity to
implement singleton, which is not always correct, since other applications
may use the same name as your service application, so it will prevent your
service from starting. A better solution is using a named Mutex, which will
become system-wide. See the article below for a sample:
http://www.ai.uga.edu/mc/SingleInstance.html

Additionally, to check if your original process is terminated after
stopping, you may use Task Manager or Process Explorer to examine the
process list.

Finally, if you still can not figure out the root cause, please feel free
to send the sample project to me by removing "online." to get my real email
address.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 30 '06 #2
Hi Jeffrey,
I'm using .Net 1.1 which could explain the difference. System is Windows XP.

You are generally right that using a process name for setting it as
singleton is not full proof but in the environment where this service runs
it's OK.
Additionally, to check if your original process is terminated after
stopping, you may use Task Manager or Process Explorer to examine the
process list.
The timeout happens even if I wait a long time after the service is stopped
and for sure, the service is not running when I try to start it. Even more,
the timeout happens at the first SCM start attempt after service installation.
Finally, if you still can not figure out the root cause, please feel free
to send the sample project to me
OK. I'm about to do it now.

Thanks for your prompt reply,

--
Benzi Eilon
""Jeffrey Tan[MSFT]"" wrote:
Hi Benzi,

Based on my understanding, your Windows Service application has included
the listed code snippet to allow only 1 instance of service to run.
However, you find that after stopping your service, you can not restart it
second time with timeout in Services.msc. If I have misunderstood you,
please feel free to tell me, thanks.

What version of .Net Framework you are using, .Net1.1 or 2.0? I have
created a sample Windows Service in VS2005 by adding your listed code
snippet in the main method. However, it can be stopped and restarted
several times without timeout error.

Also, your code logic actually uses the process name as the identity to
implement singleton, which is not always correct, since other applications
may use the same name as your service application, so it will prevent your
service from starting. A better solution is using a named Mutex, which will
become system-wide. See the article below for a sample:
http://www.ai.uga.edu/mc/SingleInstance.html

Additionally, to check if your original process is terminated after
stopping, you may use Task Manager or Process Explorer to examine the
process list.

Finally, if you still can not figure out the root cause, please feel free
to send the sample project to me by removing "online." to get my real email
address.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 30 '06 #3
Hi Benzi,

Thanks for your information.

Yes, by creating a VS.net2003 Windows Service project with your code
snippet, I can reproduce the problem. Further research shows that the
problem lies in Process.Process Name property.

Below is some additional interesting test result:
1. In my 1G RAM machine, I tried to remove the string s =
Process.GetCurr entProcess().Pr ocessName code into the OnStop() method, I
find that the "stop" command will also experience the timeout problem.
2. On my another 2G RAM machine, I find that when the code is executed in
OnStop method, "stop" command will work well, however, it will still
experience the timeout if I execute the code in main method.

These tests show that my 2G RAM machine has a better hardware performance,
so OnStop method will return timely. However, in OnStart method, since the
OnStart wait period is very short, both 2 machines will fail to return
timely. So it makes sense.

Then I used the VS.net2003 debugger to attach the Windows Service process
while it is hang. By breaking the process while hang several times, I
always got the following managed call stack:

system.dll!Syst em.Diagnostics. PerformanceMoni tor.GetData(str ing item)
Line 1192 + 0xf4 bytes C#

system.dll!Syst em.Diagnostics. PerformanceCoun terLib.GetPerfo rmanceData(stri n
g item) Line 956 + 0xa8 bytes C#
system.dll!Syst em.Diagnostics. PerformanceCoun terLib.get_Cate goryTable()
Line 114 + 0x56 bytes C#

system.dll!Syst em.Diagnostics. PerformanceCoun terLib.GetPerfo rmanceData(stri n
g[] categories, int[] categoryIndexes ) Line 941 + 0x3e bytes C#

system.dll!Syst em.Diagnostics. NtProcessManage r.GetProcessInf os(System.Diagn o
stics.Performan ceCounterLib library) Line 2664 + 0xa3 bytes C#
system.dll!Syst em.Diagnostics. NtProcessManage r.GetProcessInf os(string
machineName, bool isRemoteMachine ) Line 2636 + 0xa4 bytes C#
system.dll!Syst em.Diagnostics. ProcessManager. GetProcessInfos (string
machineName) Line 2243 + 0x29 bytes C#

system.dll!Syst em.Diagnostics. Process.EnsureS tate(System.Dia gnostics.Proces s
.State state) Line 1092 + 0x116 bytes C#
system.dll!Syst em.Diagnostics. Process.get_Pro cessName() Line 657 + 0x11
bytes C#
> dummyservice.ex e!DummyService. Service1.Main() Line 33 + 0x12 bytes C#
So, it seems that the .Net1.1 Process.Process Name property internally uses
PerformanceCoun terLib to query the process/thread information. And there is
certain loop calling in the implementation which causes the long
time-consuming.

Then I tried to understand why the .Net2.0 did not experience the hang
timeout issue. In Reflector, it reveals that .Net2.0 changed the
Process.get_Pro cessName implementation, which will not use
PerformanceCoun terLib internally to query the process/thread information. I
assume that our CLR team has awared of the ProcessName property performance
problem in .Net1.1, so they changed the implementation in .Net2.0.

To workaround this problem, you upgrade your project to the .Net2.0, which
will make the problem go away. Does it meet your need? If you still want to
go with .Net1.1, I think you have to change your implementation to use
Mutex approach.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 31 '06 #4
Hi Jeffrey,

Thanks for the detailed explanation. I guess that I have to try the
mutex approach as, according to what you discovered, I can't work
around the problem using ProcessName.

Thanks again,
--
Benzi Eilon
""Jeffrey Tan[MSFT]"" wrote:
Hi Benzi,

Thanks for your information.

Yes, by creating a VS.net2003 Windows Service project with your code
snippet, I can reproduce the problem. Further research shows that the
problem lies in Process.Process Name property.

Below is some additional interesting test result:
1. In my 1G RAM machine, I tried to remove the string s =
Process.GetCurr entProcess().Pr ocessName code into the OnStop() method, I
find that the "stop" command will also experience the timeout problem.
2. On my another 2G RAM machine, I find that when the code is executed in
OnStop method, "stop" command will work well, however, it will still
experience the timeout if I execute the code in main method.

These tests show that my 2G RAM machine has a better hardware performance,
so OnStop method will return timely. However, in OnStart method, since the
OnStart wait period is very short, both 2 machines will fail to return
timely. So it makes sense.

Then I used the VS.net2003 debugger to attach the Windows Service process
while it is hang. By breaking the process while hang several times, I
always got the following managed call stack:

system.dll!Syst em.Diagnostics. PerformanceMoni tor.GetData(str ing item)
Line 1192 + 0xf4 bytes C#

system.dll!Syst em.Diagnostics. PerformanceCoun terLib.GetPerfo rmanceData(stri n
g item) Line 956 + 0xa8 bytes C#
system.dll!Syst em.Diagnostics. PerformanceCoun terLib.get_Cate goryTable()
Line 114 + 0x56 bytes C#

system.dll!Syst em.Diagnostics. PerformanceCoun terLib.GetPerfo rmanceData(stri n
g[] categories, int[] categoryIndexes ) Line 941 + 0x3e bytes C#

system.dll!Syst em.Diagnostics. NtProcessManage r.GetProcessInf os(System.Diagn o
stics.Performan ceCounterLib library) Line 2664 + 0xa3 bytes C#
system.dll!Syst em.Diagnostics. NtProcessManage r.GetProcessInf os(string
machineName, bool isRemoteMachine ) Line 2636 + 0xa4 bytes C#
system.dll!Syst em.Diagnostics. ProcessManager. GetProcessInfos (string
machineName) Line 2243 + 0x29 bytes C#

system.dll!Syst em.Diagnostics. Process.EnsureS tate(System.Dia gnostics.Proces s
.State state) Line 1092 + 0x116 bytes C#
system.dll!Syst em.Diagnostics. Process.get_Pro cessName() Line 657 + 0x11
bytes C#
dummyservice.ex e!DummyService. Service1.Main() Line 33 + 0x12 bytes C#

So, it seems that the .Net1.1 Process.Process Name property internally uses
PerformanceCoun terLib to query the process/thread information. And there is
certain loop calling in the implementation which causes the long
time-consuming.

Then I tried to understand why the .Net2.0 did not experience the hang
timeout issue. In Reflector, it reveals that .Net2.0 changed the
Process.get_Pro cessName implementation, which will not use
PerformanceCoun terLib internally to query the process/thread information. I
assume that our CLR team has awared of the ProcessName property performance
problem in .Net1.1, so they changed the implementation in .Net2.0.

To workaround this problem, you upgrade your project to the .Net2.0, which
will make the problem go away. Does it meet your need? If you still want to
go with .Net1.1, I think you have to change your implementation to use
Mutex approach.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 31 '06 #5
Hi Benzi,

Yes, since the .Net1.1 Process class internal implementation replies very
much on the PerformanceCoun ter to query the process/thread information,
there is no way to tell the Process class to workaround the performance
issue. Since using Mutex to implement singleton is a good solution and will
eliminate the performance issue, I recommend you to go with this solution.

Additionally, normally in Windows Service application, you should not place
time-consuming code in the Main method, since the Services.msc will only
wait the Main method to response in a short period. If you really need to
process some length initialization code during service startup, you'd
better spawn another thread to do the work.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4189
by: Ken Varn | last post by:
Everytime I call Process.GetCurrentProcess() the system goes out to the floppy drive. Why does this happen. I decided to use FileMon to get a log of what is happening when this occurs. Here is the log: 1 4:31:10 PM BlockedConfigAp:1340 IRP_MJ_CREATE A: DASD SUCCESS Attributes: Any Options: Open 2 4:31:11 PM BlockedConfigAp:1340 IRP_MJ_DEVICE_CONTROL A: DASD INVALID DEVICE REQUEST IOCTL: 0x560000 3 4:31:11 PM BlockedConfigAp:1340...
2
2080
by: Eric Caron | last post by:
Hi Here's my problem, I create a windows service with Config.xml file. All setup for my service is in this file. I create a windows application to manage these settings (Config.XML File). This work good if the two applications are in the same folder and Config.XML is in the folder too but If not, my windows application setting don't work. In my windows form I have a Service controler components to manage my service (start, stop, ...)....
3
2224
by: dice | last post by:
I have built a .NET based service that uses a number of related 3rd party dlls. If I compile my code into a dll and use this in a test app all is well - however if I compile my code as a service and try to run the service I get the following exception: System.IO.FileNotFoundException: The specified module could not be found. at CES.VendClientInt.VendClient.GetCardTypes(...) at YQ_Epos_server.CodeHandler.start(...) I have tried copying...
1
1779
by: Glenn | last post by:
I have used this code successfully in a form application. I tried to add the same code in a service and have not been able to get the application to start. I have the service starting with a local account and the Interact with the Desktop is Checked. Windows media player seems to start but then closes almost immediately. I would appreciate any help. Thanks, Glenn
17
9237
by: Danieltbt05 | last post by:
just installed SQL server 2000 and using my client , i can't locate the server. I used SQL query analyzer to search but no servers were found. Error message is as below Server : Msg17,level 16, state1 SQL server does not exist or denied access.
2
400
by: Macca | last post by:
Hi, I would like to get the memory usage of the process my app is running in so i can monitor levels of memory during runtime. I've been told I can use the System.Diagnostics.Process class to do this. I just want the current process my app is running in and not other processes on the machine. Is there a way the above class can automatcally detect what process the code is running in?
5
6752
by: Reddy | last post by:
I am running process.sart to run gpg.exe to encrypt files. It's working fine with console application. When i use same code in windows service. It's not working. It's not even throwing error.Follwoing is the code i am using. Please somebody help me. Process p = new Process(); p.StartInfo.WorkingDirectory = Path; p.StartInfo.FileName = fileName; p.StartInfo.Arguments =arguments; p.Start();
1
2227
by: mikelujan | last post by:
Hi, Our application starts an external application using System.Diagnostics.Process class and the Start() method, as per code snippet below. This application run as a Windows service, and must start several instances of the same application, like multiple Windows Calculators for instance. We are experiencing difficulties starting applications after a certain number have been started. We changed our app to work as a Console...
6
7477
by: Terry Carroll | last post by:
I am trying to do something with a very large tarfile from within Python, and am running into memory constraints. The tarfile in question is a 4-gigabyte datafile from freedb.org, http://ftp.freedb.org/pub/freedb/ , and has about 2.5 million members in it. Here's a simple toy program that just goes through and counts the number of members in the tarfile, printing a status message every N records (N=10,000 for the smaller file;...
0
8863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.