Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i am writing a program that launches a random file in a directory. the file can be of any time, but mostly video or image files. each time i launch a file i want to close the previous opened one. the code is:

    string FolderSelected = "";
    string FileName = "";
    Process proc;

    List<string> FilesDisplayed = new List<string>();

    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (FolderSelected == string.Empty)
            FolderSelected = Properties.Settings.Default.FilesDefaultFolder;

        if (proc != null)
        {
            proc.CloseMainWindow();
            proc.Close();
        }
        FileName = FetchRandomFile();
        proc = Process.Start(FileName);


    }

the problem is, that i keep getting proc = null (the file is launched properly) and i cannot fetch the previously opened process in order to close it. I know that .NET reuses processes and thats why it returns Null but i need to override this behavior. please help, Guy.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
298 views
Welcome To Ask or Share your Answers For Others

1 Answer

EDIT: Thanks to leppie's comment, I suspect I know the answer: my guess is that you're "starting" something like an image, and it's reusing an existing process to open the document instead of creating a new one.

I've reproduced this with this simple test app:

using System;
using System.Diagnostics;

public class Test
{
    static void Main()
    {
        Process proc = Process.Start("image.tif");
        Console.WriteLine(proc == null);
    }
}

This prints "true" because it's using dllhost.exe to host the Windows Image Viewer, rather than creating a new process.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...