Mono Console Application: Winforms Method Throws Exception On Linux Without X - winforms

I have a C# console application that I want to run on Linux. I've compiled it in MonoDevelop and on my machine (Ubuntu 12.04 LTS, 64 bit) it runs fine. However, on a VPS I have rented (same OS but no X11), while the application starts without problems, I get exceptions at runtime.
The call that causes the exception is the MeasureText method of class System.Windows.Forms.TextRenderer. In case you're wondering what this method is doing in a console app, it needs to measure the length of text rendered in a certain font for calculating the correct placements of text elements in SVG files it's creating. Exception message and stack trace excerpt below:
An exception was thrown by the type initializer for System.Windows.Forms.XplatUI
at System.Windows.Forms.TextRenderer.MeasureTextInternal (IDeviceContext dc, System.String text, System.Drawing.Font font, Size proposedSize, TextFormatFlags flags, Boolean useMeasureString) [0x00000] in <filename unknown>:0
at System.Windows.Forms.TextRenderer.MeasureText (System.String text, System.Drawing.Font font) [0x00000] in <filename unknown>:0
I think I have installed the font in question correctly on the server, at least it shows up when I call fc-list. So do I need X for this or are there ways around it? Am I just missing some package install on the server? Failing that, is there maybe an alternative that I could use to achieve the text measuring?

I tried out the suggestion made above and it worked. I installed xvfb and am now running mono through xvfb-run. Exception has disappeared and memory usage on server hasn't gone up by much.

Related

CefNet on Linux w/o X server

After a lot work invested in getting CefNet to work on Windows, I wanted to port my NET 7 app to Linux (Ubuntu). To my understanding CefNet is the only implementation that will work on Linux.
I got to a point where I am getting the exception below. Spying on source code using DotPeek leaves me under the impression that CefNet won't work in "headless" mode but will require "X server". Not sure why this would be necessary for off-screen rendering.
Any insight is appreciated.
Unhandled exception. System.InvalidOperationException: Operation is not valid due to the current state of the object.
at CefNet.CefScreenInfo.GetPrimaryScreenInfo()
at CefNet.Internal.WebViewGlue.GetScreenInfo(CefBrowser browser, CefScreenInfo& screenInfo)
at CefNet.Internal.CefRenderHandlerGlue.GetScreenInfo(CefBrowser browser, CefScreenInfo& screenInfo)
at CefNet.CefRenderHandler.GetScreenInfoImpl(cef_render_handler_t* self, cef_browser_t* browser, cef_screen_info_t* screen_info)

Trying to open an xaml file that is on a CD throws IOException on some computers and not others

I have a WPF application that displays loose XAML FlowDocuments (with Frame) that are located on a CD or DVD. This has worked just fine in the past, but now all of the sudden the program is crashing on a specific computer. The very strange thing is that this computer is 100% identical to other computers that this works just fine on (their hard drives are even imaged from the same hard drive image).
Not only does my application crash, but when I simply try to view these loose xaml files with internet explorer, it also cannot display them (they display fine in IE on other computers). Clicking on the more information button in IE shows me:
System.IO.IOException: The media is write protected.
Which I'm assuming is the same exception that my application is getting, though I can't actually tell the actual exception my program is getting because putting a try-catch around the Frame.Navigate function that I'm calling to display the xaml files doesn't actually catch this exception (Bonus Question: Why is that?), the program just crashes (and I don't have VS on this computer or remoting capabilities so I can't run the debugger).
Even more strange is the exception, why would it matter if the media is write protected if all I'm trying to do is read it?

"The specified procedure could not be found" error when running app on Windows XP (exception c0000139)

We have a dll that has been running fine on Vista and Win7 for a while, but when running on XP we get the error "The specified procedure could not be found" when LoadLibrary() is called to load it.
I've tried running it through Dependency Walker with the Profile option, with Global Flags set to show loader snaps and the only seemingly useful information I can get out of it is:
LDR: Snapping imports for MyDll.dll from KERNEL32.dll
LDR: LdrGetProcedureAddress by NAME - RtlGetLastWin32Error
LDR: exception c0000139 thrown within function LdrpSnapIAT
Exception record: 0012E490
Context record: 0012E4A4
Context->Eip = 7C9673BE
Context->Ebp = 0012E7C0
Context->Esp = 0012E770
LDR: LdrpSnapIAT - caught exception c0000139 snapping thunks (#2)
LDR: LdrpWalkImportTable - LdrpSnapIAT #2 failed with status c0000139
I believe that exception c0000139 is 'Entry Point Not Found'.
We've taken care to ensure that there aren't any direct dependencies on post-XP APIs (there are a few, but these are dynamically loaded with LoadLibrary()). The dll was built with WINVER and _WIN32_WINNT set to 0x0501. The entry point of the library is visible in the exports shown in Dependency Monitor, and there are no obvious missing dependencies. The same application runs absolutely fine in Vista and Win7. An earlier version of the dll worked fine on XP (due to the nature of this component it's difficult to see which change caused things to break).
Edit: other things I think I've discounted:
Visual studio runtimes are installed
The manifest files from a working dll and one that doesn't are the same
Is there anything else obvious I'm missing?
I've no idea if this will help you but I had the exact same issue today with loader snaps. It turned out that my problem was visible in depends.exe but I didn't know the significance of dlls being pink... I've only ever diagnosed export issues with depends, not import issues.
My issue was that I was using "RegGetValue" which is only available on Vista+. Unfortunately there's no proper error reporting if you use a Vista only API on Windows XP. Depends showed this in the import section like so:
I notice that your issue was with Kernel32.dll rather than Advapi.dll so check the imports with depends...

How to debug a fatal error that happens after calling Application.Exit() in .NET CF 3.5 WinForms application for Windows CE 6?

I am porting a .NET CF 1.0 WinForms application (for older versions of Windows CE) to .NET CF 3.5 (for Windows CE 6). The problem is that, a few seconds after Application.Exit() is called, I get a flash of a "fatal error" message box, which simply says something to the effect of "A fatal error has occurred and the application will terminate.". Since I'm using a Chinese version of Windows CE, the message is in Chinese and I'm not sure what the exact message is in English. Anyway, the error message then automatically disappears and the application fails to terminate and release resources completely, such that the whole operating system becomes unusable (launching any application would result in the perpetual hourglass animation, docking the device in its cradle also does not cause ActiveSync to connect) until I warm boot the device.
This fatal error apparently never occurred in its original form (.NET CF 1.0) on the older device.
And because it's not a .NET exception, it is not caught by the .NET runtime.
What can I do?
Because you can not catch the exception which is happening at Application.Exit(), this sounds like you are facing a bug I've seen before. Please try to comment out all the lines where you set the Font attribute. If the application exists without the error message, you are facing a bug which affects NetCF 3.5 at WinCE 6.0 only. See this link for more information.
Sounds like a Dispose or Finalizer has a problem that's showing up when the GC is cleaning house. Check all app finalizers and all Dispose overrides. If that fails to find it, look at any worker thread shutdowns (things sitting in blocking calls, reading handles that might be invalidated, etc).
I've came across this issue recently and the issue was the forms weren't being disposed. So what I had to do was on every form load I added the form instance to a global list that contained all opened forms and upon the application exit I loop through the list and did a form.dispose on each. That solved my problem instantly.

What is causing ObjectDisposedException from SerialPort while debugging .NET winform?

Vista SP1
Visual Studio 2008 SP1
.NET 3.5 SP1
C#
I have a winforms app I'm playing with that uses a SerialPort object as a private variable. When the application is compiled and executed, it works great. It also works running in debug mode wihtout any breakpoints. 90% of the time when I stop at a breakpoint and try to step through code I get an 'unhandled exception ocurred' dialog with these details:
System.ObjectDisposedException was unhandled
Message="Safe handle has been closed"
Source="mscorlib"
ObjectName=""
StackTrace:
at Microsoft.Win32.Win32Native.SetEvent(SafeWaitHandle handle)
at System.Threading.EventWaitHandle.Set()
at System.IO.Ports.SerialStream.AsyncFSCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
InnerException:
The frustrating thing is, I do not have to be stepping over serial-related code! I just have to have done something with the port. So I might read a string, manipulate the string, add two numbers together, whatever, and then BANG.
Again, this works just fine when NOT debugging, or when debugging wihtout any breakpoints. There seems to be something about stopping at a breakpoint that makes the CLR dispose the SerialStream on a different thread.
There is a lot of chatter online about problems with renoving USB devices causing this. But I'm using the build-in motherboard port on COM1.
I don't think I had this issue in .NET 2.0 so I may have to go back to that...
I need to simplify the application quite a bit before I can post code - but has anyone seen behavior like this in the debugger before?
Thanks so much!
I had the same problem just this morning. Surprisingly, it simply has gone away when I DISABLED the following options in VS2008 Tools->Options->Debugging->General:
"Enable the exception assistant"
"Enable .NET Framework source stepping"
"Step over properties and operators"
"Enable property evaluation and other implicit function calls"
I have no idea why, but it worked for me.
Perhaps your port is getting closed by the OS as it does not get a response from your application (it is stopped at a breakpoint).
Well I'm not so sure this is an answer, but there was definately something about that project. It was originally written in 2.0 and converted to 3.5 by VS2008. I created a new project in C#-Express 2008 adding the original classes one-by-one and it works like a charm now! No idea what is different.
I have this too. This must be some kind of bug with the debugger. The above advice worked: Disable the "Enable property evaluation and other implicit function calls."
I have a class with properties that do serial I/O. I thought that perhaps the debugger was helpfully trying to display the property value when I hovered the mouse over it, thus doing the IO from the debugger thread. But that doesn't seem to be the case. I'm really unsure what the cause is.
I had the same problem and did the following:
serialPortLock = Monitor.TryEnter(serialPort, 3000);
Thread.Sleep(5);
serialPort.Write(msg, 0, msg.Length);
and the same for my Read().
Looks like it's a good workaround for me!

Resources