如何在webbrowser和rgiesecke.dllexport中使用C#类库 [英] How to use C# class library with webbrowser and rgiesecke.dllexport

查看:122
本文介绍了如何在webbrowser和rgiesecke.dllexport中使用C#类库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试创建一个显示带有webbrowser的表单的C#类库,则库的执行会显示错误:System.Threading.ThreadStateException:ActiveX控件,它不允许显示表单和应用程序结束。



我有一个用C ++编写的控制台应用程序,它加载了一个C#类库,它在启动时应该显示一个带有webbrowser的表单,但是当执行代码以显示表单时,应用程序显示上一个错误并完成。



我相信使用RGiesecke.DllExport的非托管包与正常情况不兼容应用程序的主题



我尝试过:



我试试:



If I try to create a C# class library that shows a form with a webbrowser in it, the execution of the library shows the error: System.Threading.ThreadStateException: ActiveX control, and it does not allow to show the form and the application ends.

I have a console application made in C++, which loads a C# class library, which, when started, should show a form with a webbrowser in it, but when executing the code to show the form the application shows the previous error and finish.

I believe that the unmanaged package using RGiesecke.DllExport is incompatible with the normal threads of an application

What I have tried:

I try:

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public class MainModule
{
    [DllExport(ExportName = "ObjectWebJS", CallingConvention =
    CallingConvention.StdCall)]
    static public int ObjectWebJS(int a, int b)
    {
        LoadForm();
        return 0;
    }
    [STAThread]
    public static void LoadForm()
    {
        ObjectForm form1 = new ObjectForm();
        form1.ShowDialog();
    }
}

推荐答案

将此属性应用于入口点方法(C#和Visual Basic中的 Main()方法)。 它对其他方法没有影响。

Apply this attribute to the entry point method (the Main() method in C# and Visual Basic). It has no effect on other methods.



您的调用代码位于MTA线程中。将 [STAThread] 属性应用于它调用的方法将无效。



您需要测试当前线程的公寓状态,并在必要时启动新的STA线程。


Your calling code is in an MTA thread. Applying the [STAThread] attribute to the method it calls will have no effect.

You need to test the current thread's apartment state, and spin up a new STA thread if necessary.

public static void LoadForm()
{
    if (Thread.CurrentThread.ApartmentState == ApartmentState.STA)
    {
        LoadFormCore();
    }
    else
    {
        ThreadStart run = LoadFormCore;
        Thread t = new Thread(run);
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
        t.Join();
    }
}

private static void LoadFormCore()
{
    ObjectForm form1 = new ObjectForm();
    form1.ShowDialog();
}


这篇关于如何在webbrowser和rgiesecke.dllexport中使用C#类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆