Jump to content

Creating an OpenFileDialog during Load (File Type Plugin)


beannaich

Recommended Posts

Hello,

I have a file type I want to load, but it depends on an external palette file. This would have to be selected by the user, so I decided that for now I would just make a quick OpenFileDialog.

When I do this, however, I get an error about the STAThreadAttribute used by Windows forms applications.. So I tested with a generic Form, and the form opens just fine.

Here's my question:

Can you utilize OpenFileDialog during the FileType.OnLoad override? And if not, what suggestions can be made for navigating to an external palette file so a special file format can be loaded and rendered properly?

Thanks in advance!

Link to comment
Share on other sites

No. Do not do this. Your plugin will be removed from the forum if you do this. The FileType plugin system was not designed for a scenario like this.

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

forumSig_bmwE60.jpg

Link to comment
Share on other sites

Why is that not allowed? To be honest, I wasn't planning on making this available to the public, since the file type is very obscure. This was going to be for use by me and some of my friends in the game modding community. Am I to believe that there is absolutely no way to load this file type because it relies on an external palette file? Respectfully, that seems kind of lame. There are save widgets.. Why not load widgets?

Link to comment
Share on other sites

The FileType system doesn't have accommodations for showing UI during the Load process, and it can cause problems (like the kind you're seeing) because it's all done on a background thread. The reason this isn't in place isn't because it would be useful, but because none of the built-in file types had a need for it and I didn't want to ship something I didn't have confidence in.

But if you just want it for yourself or a private group of friends then that's fine. You can probably do it with something like ...

using System.Threading;

ManualResetEvent doneEvent = new ManualResetEvent(false);
DialogResult dialogResult = DialogResult.Cancel;
string fileName = null;
Exception error = null;

Thread dialogThread = new Thread(
   delegate()
   {
       try
       {
           using (OpenFileDialog dialog = new ...)
           {
               ... set properties on dialog ...
               dialogResult = dialog.ShowDialog();
               fileName = dialog.FileName;
               ... etc. as needed ...
           }
       }
       catch (Exception ex)
       {
           error = ex;
       }
       finally
       {
           doneEvent.Set();
       }
   });

dialogThread.TrySetApartmentState(ApartmentState.STA); // This is the key
dialogThread.Start();

doneEvent.WaitOne();

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

forumSig_bmwE60.jpg

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...