简体   繁体   中英

C#: MethodInfo.Invoke a method that needs input from user to process

I have a DLL file that has a method inside it which creates a FolderBrowserDialog and waits for user selection to proceed. Here is its code:

        public void setRoot() {
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        if (fbd.ShowDialog() == DialogResult.OK)
        {
            root = fbd.SelectedPath;
            searchRoot();
        }
    }

However, on another program I am trying to invoke this method and I get the program waiting forever. My guess is that the invoking program is waiting for the FileBrowserDialog to select a file. This is the output: MethodInfo.Invoke(class object, parameters) How can I get past that and make the invoking program select the directory for that invoked method?

A method called with MethodInfo.Invoke is not different than one called directly from code. It's the same code that's being run, will still run on the same thread, with the same permissions as a direct call. If your Reflection-invoked method hangs while working fine without reflection, there's something else that's causing it.

It looks, from your screenshot, that you're running it in a Console application. I'm guessing the other app, isn't a console app, which means it has a running message pump waiting for Windows messages, making the dialog work. Running Windows dialogs in a console application won't work so easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM