Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6973901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:10:04+00:00 2026-05-27T17:10:04+00:00

The program’s code is like below: using System.Diagnostics; class Test { static void Main()

  • 0

The program’s code is like below:

using System.Diagnostics;

class Test
{
    static void Main()
    {
        using (Process p = Process.Start("notepad.exe"))
        {
        }
    }
}

The problem is when this program has been closed and the Notepad is still running, the Notepad will lock it. And I don’t want to close the Notepad.
I tried to use Unlocker to detect the problem.
The result screen shot is like below:

Result Image Link

So what I want is starting a exe file and dispose all thread resources immediately. Or I can’t uninstall my program until all the program opened by it are closed. That may cause unpleasant user experience. Requiring system restart is also unpleasant. Unlocker can do it nicely. If I use Unlocker to unlock the parent folder, it won’t close the Notepad, and then I can delete this program file normally.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:10:05+00:00Added an answer on May 27, 2026 at 5:10 pm

    The problem is that you never close the running process, so it continues to run, which causes Unlocker to indicate that it is locked.

    I know you think you did, because you called p.Close(), but the trick is that the Process.Close() method does not cause the process to close. It does not send a WM_CLOSE or WM_QUIT message to the Notepad window. All it does is free the resources associated with the process.

    Instead, you need to use the Process.CloseMainWindow() method. This will send the window a WM_CLOSE message, causing it to close properly.

    So update your code to look like this (as someone else mentioned, there’s no need to explicitly call Dispose if you’ve already wrapped the object creation in a using statement):

    using System.Diagnostics;
    
    class Test
    {
        static void Main()
        {
            using (Process p = Process.Start("notepad.exe"))
            {
                p.CloseMainWindow();
                p.Close();
            }
        }
    }
    

    Note that you could use the Process.Kill() method, but this is like turning off your computer by ripping the power cord out of the wall. Instead of asking the application to close nicely, it forces it to terminate, just like clicking “End Task” in Task Manager. It should therefore be used only when the process is locked up or otherwise not responding to polite requests to close.


    Also, as Rolice mentioned in a comment, the above code isn’t going to be very useful. You’re just starting Notepad and then immediately closing it.

    Instead, you probably want to start the process, allow the user to interact with it, and then clean up after they’re done with it.

    In that case, you need to use the Process.WaitForExit() method. This method will block the calling thread, waiting indefinitely for the process to exit. When the user finally closes Notepad, control will return to your application’s thread and you can call p.Close().

    For example:

    using System.Diagnostics;
    
    class Test
    {
        static void Main()
        {
            using (Process p = Process.Start("notepad.exe"))
            {
                p.WaitForExit();
                p.Close();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: class Program { static void Main(string[] args) { Department deathStar
I have the following code: class Program { static void Main(string[] args) { new
I have a piece of code like this: Class Program { static StreamReader sr
Assume that we have the following code: class Program { static volatile bool flag1;
I am currently using the below code to run a process and print its
I would like to call a windows program within my code with parameters determined
I have a program that simulates mouse click. Code is something like this: [DllImport(user32.dll,
The code below consists of two classes : SmartForm (simple model class) SmartForms (plural
Am getting the same error on the code below using either trusted or SQL
I would like to cross-compile a simple program for ARM architecture using the arm-linux-gcc

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.