aelyo-softworks / Wice

Windows Interface Composition Engine is a .NET C# UI engine for creating Windows application.
Other
114 stars 5 forks source link

How to use STAThread in winform application ? #3

Closed Charltsing closed 11 months ago

Charltsing commented 11 months ago

https://learn.microsoft.com/en-us/dotnet/api/system.stathreadattribute Windows Forms apps must be single-threaded if they communicate with Windows system components such as the Clipboard or Windows common dialog boxes, or if they use system features such as drag-and-drop functionality.

I try Wice in my winform application, but Main() must be [MTAThread], if i try [STAThread], it report Win32Exception(0x80004005), in DirectN.TextServicesFunctions.Shutdown(ITextServicesservices), when windows is closed.

Maybe it is probably because DirectN is an MTAThread ??

Do you know, How to use STAThread in winform + Wice application ?

smourier commented 11 months ago

Wice doesn't mandate to be started in MTA nor STA.

What scenario are you trying the involes Wice & Winforms? This is clearly not something that's been tested as Wice can run for itself independently from any other UI framework.

If you can share some reproducing code, I can have a look at it.

Charltsing commented 11 months ago

[STAThread] static void Main()

1

close application, it report 2

smourier commented 11 months ago

Ok, this is because Wice's RichTextBox visual internally uses Windows' Richedit component (text services https://learn.microsoft.com/en-us/windows/win32/api/tom/) and this component is (probably) STA-based which means it must be disposed on the same thread on which it was created. When it's not it causes this error.

So, it's more a bug of the Wice Gallery that wasn't disposing this component at the right time. I've just fixed https://github.com/aelyo-softworks/Wice/commit/ea549b601221200695a2ce70b5596c118aadcbf6

Charltsing commented 11 months ago

i try lastest Wice.Samples.Gallery, it still report same error error.zip

i try dispose desc in HomePage, it works fine.

public class HomePage : Page, IDisposable RichTextBox desc = new RichTextBox();

public void Dispose() { desc.Dispose(); }

smourier commented 11 months ago

Yeah, I had done the work to enable this (making sure IDisposable's Dispose on gallery pages is called), but also lost some of my changes like this one... Anyway there were other places where dispose of the RichTextBox (and derivated classes like CodeBox) was not done on the UI thread. This, should hopefully be fixed now.