2013-10-14 61 views
3

我试图呈现一个html代码,将其导出到图像,然后使用该图像将其放入一个pdf文档。我正在做一个asp.net/C# web应用程序。我遇到的问题是,当我做一个以上的时间把它给我这个错误:我可以在Web应用程序中使用Awesomeium吗?

You are attempting to re-initialize the WebCore. The WebCore must only be initialized once per process and must be shut down only when the process exits. 

这是我的代码:

WebCore.Initialize(new WebConfig(), false); 
using (WebView webView = WebCore.CreateWebView((pdfWidth - 80).ToString().ToInt() + 20, (pdfHeight/2).ToString().ToInt() + 20)) 
{ 
    webView.LoadHTML(html); 

    while (webView.IsLoading || !webView.IsDocumentReady) 
       WebCore.Update(); 

    Thread.Sleep(1100); 
    WebCore.Update(); 

    string fileName = Server.MapPath("result." + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Millisecond.ToString() + ".png"); 
    BitmapSurface surface = (BitmapSurface)webView.Surface; 
    surface.SaveToPNG(fileName, true); 

    WebCore.Shutdown(); 
} 

我想,我不能初始化每次WebCore呈现WebCore,所以我把它放在Global.asax中。这样做后,当我在调试时,出现了另一个错误:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt. The current thread is not currently running code or the call stack could not be obtained. 

任何想法我该怎么做?

感谢

+0

从你所描述的'WebCore.Initialize'应该在'Application_Start'中,'WebCore.Shutdown'应该放在'Global.asax'的'Application_End'中。 – Romoku

+1

我这样做,它给了我第二个错误:试图读取或写入受保护的内存。这通常表明其他内存已损坏。当前线程当前不在运行代码或无法获取调用堆栈。 – robert00769

回答

4

简短的回答:是的,可以。

但是根据WebCore documentation

Note that Awesomium and Awesomium.NET are not thread-safe. All API members should be called from the thread that initialized the WebCore (see Initialize(WebConfig)) or created the first WebView, WebSession or technology specific WebControl.

也期待在的WebCore.Update() method descriptionExceptions部分:

System.AccessViolationException - You attempted to access the member from a thread other than thread where WebCore was created.

因此考虑到在网络应用程序的每个请求在不同的线程中通常处理,AccessViolationException是有记录的特征

因此,您在这里的任务是确保所有Awesomium调用将在特殊的专用线程中处理。该线程应该在应用程序级别上运行,并且它的访问必须在请求之间共享。从技术上讲,您需要编写一种消息循环或同步上下文,您可以将您的delegateActionFunc与Awesomium代码相提并论,以便仅在此线程中提供其执行。

+0

你能提供一个例子吗? – Exzile

0

您可能需要调用webview.invoke或webview.begininvoke,并且您将需要创建一个委托来访问在另一个线程中创建webcore.initialize之前创建的webview,我将使用单例模式使用相同的webview实例。

相关问题