Windows Forms - How can I prevent a System.AccessViolationException when opening a new thread

2

I am hoping this issue is an obvious one to someone 😊 I have a Windows Forms application that does some independent processes on separate sub-forms. It has a MainForm that opens first and then it opens the sub-forms based on an “is active” database value for each sub-form. This process runs as expected often for many hours without any issues… opening and closing when necessary. The issue that I am having trouble figuring out is one where the application just freezes and has to be manually closed and restarted which of course while it isn’t running stops processing altogether.

There are always 2 errors in the Windows Application error log. First is a “.Net Runtime” and the second is a “Application Error”. There is a third Information entry that appears to be logged when the application is restarted but not 100% certain if it is present before it is restarted.

The MainForm is opening the different sub-forms in a new thread using different versions of this:

try
{
Thread CS = new Thread(new ThreadStart(CS_Start));
CS.SetApartmentState(ApartmentState.STA);
CS.Start();
}
catch { }

And this is the code it is called in the new thread which is opens one of the sub-forms…

private void CS_Start ()
        {
            try
            {
                using (CS_Searches CS_SearchForm = new CS_Searches())
                {
                    if (!CS_SearchForm.Visible)
                        CS_SearchForm.ShowDialog();
                }
            }
            catch { }
        }

There are 5 separate forms that the MainForm opens. Each form does completely separate tasks and will also close itself based on the “is active” value using: this.Dispose();

The issue seems to always occur at the different CS.Start(); lines. I have tried catching the error but unfortunately it never hits this code. I’ve also modified the CS_Start by adding the using statement. Unfortunately I haven’t been able to replicate the issue as it runs great until it doesn’t and the frequency can be all over the map. Based on all my Googling it appears to be a memory access violation but I am having no luck confirming this or finding a fix. Any assistance would be greatly appreciated.

Thanks, Greg

These are the error log details:

Error - .Net Runtime

Application: OA.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Reflection.TargetInvocationException
Stack:
   at System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Object[], System.Signature, Boolean)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[])
   at System.Delegate.DynamicInvokeImpl(System.Object[])
   at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry)
   at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry)
   at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
   at System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef)
   at System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr, Int32, Int32)
   at System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
   at System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
   at System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window)
   at OA.MainForm.CS_Start()
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadHelper.ThreadStart()

Error - Application Error

Faulting application name: OA.exe, version: 1.0.0.0, time stamp: 0x5d7835c6
Faulting module name: KERNELBASE.dll, version: 6.1.7601.18015, time stamp: 0x50b8479b
Exception code: 0xe0434352
Fault offset: 0x0000000000009e5d
Faulting process id: 0xed0
Faulting application start time: 0x01d5683c859e21b3
Faulting application path: C:\! OA\OA.exe
Faulting module path: C:\Windows\system32\KERNELBASE.dll
Report Id: f77be558-d4a1-11e9-a767-00505691533d

Information - Windows Error Reporting

Fault bucket , type 0
Event Name: CLR20r3
Response: Not available
Cab Id: 0

Problem signature:
P1: OA.exe
P2: 1.0.0.0
P3: 5d793cd6
P4: System.Windows.Forms
P5: 4.0.30319.17929
P6: 4ffa5c0e
P7: 4cba
P8: 27
P9: System.AccessViolationException
P10: 

Attached files:

These files may be available here:
C:\Users\AUTO.WERHQ\AppData\Local\Microsoft\Windows\WER\ReportArchive\AppCrash_oa_c95e0af449b9b5070e88c537a95b8ce90dee049_25993682

Analysis symbol: 
Rechecking for solution: 0
Report Id: bbe1964a-d56f-11e9-a767-00505691533d
Report Status: 0

Information - Windows Error Reporting – Report File above content

Version=1
EventType=CLR20r3
EventTime=132127743632961454
ReportType=2
Consent=1
ReportIdentifier=d5c9cb47-d56e-11e9-a767-00505691533d
IntegratorReportIdentifier=d5c9cb46-d56e-11e9-a767-00505691533d
Response.type=4
Sig[0].Name=Problem Signature 01
Sig[0].Value=OA.exe
Sig[1].Name=Problem Signature 02
Sig[1].Value=1.0.0.0
Sig[2].Name=Problem Signature 03
Sig[2].Value=5d793cd6
Sig[3].Name=Problem Signature 04
Sig[3].Value=System.Windows.Forms
Sig[4].Name=Problem Signature 05
Sig[4].Value=4.0.30319.17929
Sig[5].Name=Problem Signature 06
Sig[5].Value=4ffa5c0e
Sig[6].Name=Problem Signature 07
Sig[6].Value=4cba
Sig[7].Name=Problem Signature 08
Sig[7].Value=27
Sig[8].Name=Problem Signature 09
Sig[8].Value=System.AccessViolationException
DynamicSig[1].Name=OS Version
DynamicSig[1].Value=6.1.7601.2.1.0.256.48
DynamicSig[2].Name=Locale ID
DynamicSig[2].Value=1033
DynamicSig[22].Name=Additional Information 1
DynamicSig[22].Value=276c
DynamicSig[23].Name=Additional Information 2
DynamicSig[23].Value=276cb04a1eafd1e19b2fc54286fd4e13
DynamicSig[24].Name=Additional Information 3
DynamicSig[24].Value=b00b
DynamicSig[25].Name=Additional Information 4
DynamicSig[25].Value=b00bccb4ee23035129f9f2009e37821c
UI[2]=C:\! OA\OA.exe
UI[3]=OA has stopped working
UI[4]=Windows can check online for a solution to the problem.
UI[5]=Check online for a solution and close the program
UI[6]=Check online for a solution later and close the program
UI[7]=Close the program
LoadedModule[0]=C:\! OA\OA.exe
LoadedModule[1]=C:\Windows\SYSTEM32\ntdll.dll
LoadedModule[2]=C:\Windows\SYSTEM32\MSCOREE.DLL
LoadedModule[3]=C:\Windows\system32\KERNEL32.dll
LoadedModule[4]=C:\Windows\system32\KERNELBASE.dll
LoadedModule[5]=C:\Windows\system32\ADVAPI32.dll
LoadedModule[6]=C:\Windows\system32\msvcrt.dll
LoadedModule[7]=C:\Windows\SYSTEM32\sechost.dll
LoadedModule[8]=C:\Windows\system32\RPCRT4.dll
LoadedModule[9]=C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscoreei.dll
LoadedModule[10]=C:\Windows\system32\SHLWAPI.dll
LoadedModule[11]=C:\Windows\system32\GDI32.dll
LoadedModule[12]=C:\Windows\system32\USER32.dll
LoadedModule[13]=C:\Windows\system32\LPK.dll
LoadedModule[14]=C:\Windows\system32\USP10.dll
LoadedModule[15]=C:\Windows\system32\IMM32.DLL
LoadedModule[16]=C:\Windows\system32\MSCTF.dll
LoadedModule[17]=C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
LoadedModule[18]=C:\Windows\system32\MSVCR110_CLR0400.dll
LoadedModule[19]=C:\Windows\assembly\NativeImages_v4.0.30319_64\mscorlib\9da5bb33cd1c34b7851c088f0cf749cc\mscorlib.ni.dll
LoadedModule[20]=C:\Windows\system32\ole32.dll
LoadedModule[21]=C:\Windows\system32\CRYPTBASE.dll
LoadedModule[22]=C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clrjit.dll
LoadedModule[23]=C:\Windows\system32\OLEAUT32.dll
LoadedModule[24]=C:\Windows\assembly\NativeImages_v4.0.30319_64\System\d191674dfd2e0fb89abf108445359453\System.ni.dll
LoadedModule[25]=C:\Windows\assembly\NativeImages_v4.0.30319_64\System.Drawing\fe0b5caab219a62708e42ad3ab7f0440\System.Drawing.ni.dll
LoadedModule[26]=C:\Windows\assembly\NativeImages_v4.0.30319_64\System.Windows.Forms\3fac3f0dccd0fdc80be79bd7fcf2261a\System.Windows.Forms.ni.dll
LoadedModule[27]=C:\Windows\assembly\NativeImages_v4.0.30319_64\System.Configuration\3dd1bdef318925394a5f51fb254b7004\System.Configuration.ni.dll
LoadedModule[28]=C:\Windows\assembly\NativeImages_v4.0.30319_64\System.Core\8ae856324ba290331c8d1442b3321a22\System.Core.ni.dll
LoadedModule[29]=C:\Windows\assembly\NativeImages_v4.0.30319_64\Microsoft.CSharp\fc2e811c0a4fbcb589f7775a25c7377a\Microsoft.CSharp.ni.dll
LoadedModule[30]=C:\Windows\Microsoft.NET\Framework64\v4.0.30319\nlssorting.dll
LoadedModule[31]=C:\Windows\system32\CRYPTSP.dll
LoadedModule[32]=C:\Windows\system32\rsaenh.dll
LoadedModule[33]=C:\Windows\system32\uxtheme.dll
LoadedModule[34]=C:\Windows\assembly\NativeImages_v4.0.30319_64\System.Xml\7a54d4ebaa554c877a48c06148506a0e\System.Xml.ni.dll
LoadedModule[35]=C:\Windows\assembly\NativeImages_v4.0.30319_64\System.Runteb92aa12#\29ffe930d3112e330be0c61b4f83bf5a\System.Runtime.Serialization.ni.dll
LoadedModule[36]=C:\Windows\system32\ws2_32.dll
LoadedModule[37]=C:\Windows\system32\NSI.dll
LoadedModule[38]=C:\Windows\system32\mswsock.dll
LoadedModule[39]=C:\Windows\System32\wshtcpip.dll
LoadedModule[40]=C:\Windows\System32\wship6.dll
LoadedModule[41]=C:\Windows\system32\NLAapi.dll
LoadedModule[42]=C:\Windows\system32\napinsp.dll
LoadedModule[43]=C:\Windows\system32\pnrpnsp.dll
LoadedModule[44]=C:\Windows\system32\DNSAPI.dll
LoadedModule[45]=C:\Windows\System32\winrnr.dll
LoadedModule[46]=C:\Windows\system32\IPHLPAPI.DLL
LoadedModule[47]=C:\Windows\system32\WINNSI.DLL
LoadedModule[48]=C:\Windows\system32\rasadhlp.dll
LoadedModule[49]=C:\Windows\System32\fwpuclnt.dll
LoadedModule[50]=C:\Windows\WinSxS\amd64_microsoft.windows.common-controls_6595b64144ccf1df_5.82.7601.17514_none_a4d6a923711520a9\comctl32.dll
LoadedModule[51]=C:\Windows\WinSxS\amd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7601.17514_none_fa396087175ac9ac\comctl32.dll
LoadedModule[52]=C:\Windows\WinSxS\amd64_microsoft.windows.gdiplus_6595b64144ccf1df_1.1.7601.17825_none_2b253c8271ec7765\gdiplus.dll
LoadedModule[53]=C:\Windows\system32\wininet.dll
LoadedModule[54]=C:\Windows\system32\api-ms-win-downlevel-user32-l1-1-0.dll
LoadedModule[55]=C:\Windows\system32\api-ms-win-downlevel-shlwapi-l1-1-0.dll
LoadedModule[56]=C:\Windows\system32\api-ms-win-downlevel-version-l1-1-0.dll
LoadedModule[57]=C:\Windows\system32\version.DLL
LoadedModule[58]=C:\Windows\system32\api-ms-win-downlevel-normaliz-l1-1-0.dll
LoadedModule[59]=C:\Windows\system32\normaliz.DLL
LoadedModule[60]=C:\Windows\system32\iertutil.dll
LoadedModule[61]=C:\Windows\system32\api-ms-win-downlevel-advapi32-l1-1-0.dll
LoadedModule[62]=C:\Windows\system32\USERENV.dll
LoadedModule[63]=C:\Windows\system32\profapi.dll
LoadedModule[64]=C:\Windows\system32\Secur32.dll
LoadedModule[65]=C:\Windows\system32\SSPICLI.DLL
LoadedModule[66]=C:\Windows\system32\SHELL32.dll
LoadedModule[67]=C:\Windows\system32\api-ms-win-downlevel-advapi32-l2-1-0.dll
LoadedModule[68]=C:\Windows\system32\api-ms-win-downlevel-ole32-l1-1-0.dll
LoadedModule[69]=C:\Windows\assembly\NativeImages_v4.0.30319_64\System.ServiceModel\b6c60909860ace07b6b6617c71d089ff\System.ServiceModel.ni.dll
LoadedModule[70]=C:\Windows\assembly\NativeImages_v4.0.30319_64\System.Servd1dec626#\ca7c2fd7257e0452df4a39ec969e8913\System.ServiceModel.Internals.ni.dll
LoadedModule[71]=C:\Windows\assembly\NativeImages_v4.0.30319_64\SMDiagnostics\7ce23442406c8bff478964ae214251b5\SMDiagnostics.ni.dll
LoadedModule[72]=C:\Windows\assembly\NativeImages_v4.0.30319_64\System.Serv30e99c02#\fe0b22eaecf87715a043c1c76884bae6\System.ServiceModel.Channels.ni.dll
LoadedModule[73]=C:\Windows\assembly\NativeImages_v4.0.30319_64\System.Servf73e6522#\0e0f2d16bef2c0e1dd62edff4b3cf02c\System.ServiceModel.Web.ni.dll
LoadedModule[74]=C:\Windows\assembly\NativeImages_v4.0.30319_64\System.IdentityModel\7b2df4768784cae3f8714f1ececbe25a\System.IdentityModel.ni.dll
LoadedModule[75]=C:\Windows\assembly\NativeImages_v4.0.30319_64\System.Net.Http\8653972f8d9923f44566604ae0de1aea\System.Net.Http.ni.dll
LoadedModule[76]=C:\Windows\system32\rasapi32.dll
LoadedModule[77]=C:\Windows\system32\rasman.dll
LoadedModule[78]=C:\Windows\system32\rtutils.dll
LoadedModule[79]=C:\Windows\system32\winhttp.dll
LoadedModule[80]=C:\Windows\system32\webio.dll
LoadedModule[81]=C:\Windows\system32\credssp.dll
LoadedModule[82]=C:\Windows\system32\dhcpcsvc6.DLL
LoadedModule[83]=C:\Windows\system32\dhcpcsvc.DLL
LoadedModule[84]=C:\Windows\system32\CFGMGR32.dll
LoadedModule[85]=C:\Windows\system32\RpcRtRemote.dll
LoadedModule[86]=C:\Windows\system32\WindowsCodecs.dll
LoadedModule[87]=C:\! OA\OpenPop.dll
LoadedModule[88]=C:\Windows\system32\bcrypt.dll
LoadedModule[89]=C:\Windows\system32\CLBCatQ.DLL
LoadedModule[90]=C:\Windows\System32\ieframe.dll
LoadedModule[91]=C:\Windows\System32\api-ms-win-downlevel-shell32-l1-1-0.dll
LoadedModule[92]=C:\Windows\system32\SXS.DLL
LoadedModule[93]=C:\Windows\system32\api-ms-win-downlevel-shlwapi-l2-1-0.dll
LoadedModule[94]=C:\Windows\system32\PROPSYS.dll
LoadedModule[95]=C:\Windows\system32\apphelp.dll
LoadedModule[96]=C:\Windows\system32\urlmon.dll
LoadedModule[97]=C:\Windows\system32\CRYPT32.dll
LoadedModule[98]=C:\Windows\system32\MSASN1.dll
LoadedModule[99]=C:\Windows\system32\MSHTML.dll
LoadedModule[100]=C:\Windows\System32\netprofm.dll
LoadedModule[101]=C:\Windows\System32\npmproxy.dll
LoadedModule[102]=C:\Windows\system32\schannel.DLL
LoadedModule[103]=C:\Windows\system32\ncrypt.dll
LoadedModule[104]=C:\Windows\system32\bcryptprimitives.dll
LoadedModule[105]=C:\Windows\system32\WINTRUST.dll
LoadedModule[106]=C:\Windows\system32\GPAPI.dll
LoadedModule[107]=C:\Windows\system32\cryptnet.dll
LoadedModule[108]=C:\Windows\system32\WLDAP32.dll
LoadedModule[109]=C:\Windows\system32\SensApi.dll
LoadedModule[110]=C:\Windows\system32\MLANG.dll
LoadedModule[111]=C:\Windows\System32\jscript9.dll
LoadedModule[112]=C:\Windows\system32\msimtf.dll
LoadedModule[113]=C:\Windows\system32\d2d1.dll
LoadedModule[114]=C:\Windows\system32\DWrite.dll
LoadedModule[115]=C:\Windows\system32\dxgi.dll
LoadedModule[116]=C:\Windows\system32\dwmapi.dll
LoadedModule[117]=C:\Windows\system32\d3d11.dll
LoadedModule[118]=C:\Windows\system32\D3D10Warp.dll
LoadedModule[119]=C:\Windows\system32\WINMM.dll
LoadedModule[120]=C:\Windows\system32\MMDevAPI.DLL
LoadedModule[121]=C:\Windows\system32\ntmarta.dll
LoadedModule[122]=C:\Windows\system32\SETUPAPI.dll
LoadedModule[123]=C:\Windows\system32\DEVOBJ.dll
LoadedModule[124]=C:\Windows\system32\OLEACC.DLL
LoadedModule[125]=C:\Windows\system32\uiautomationcore.dll
LoadedModule[126]=C:\Windows\system32\PSAPI.DLL
LoadedModule[127]=C:\Windows\system32\ImgUtil.dll
LoadedModule[128]=C:\Program Files\Internet Explorer\ieproxy.dll
LoadedModule[129]=C:\Windows\System32\UIAnimation.dll
LoadedModule[130]=C:\Windows\system32\T2EMBED.DLL
LoadedModule[131]=C:\Windows\System32\msxml3.dll
LoadedModule[132]=C:\Windows\system32\XmlLite.dll
LoadedModule[133]=C:\Windows\Microsoft.NET\Framework64\v4.0.30319\diasymreader.dll
FriendlyEventName=Stopped working
ConsentKey=CLR20r3
AppName=OA
AppPath=C:\! OA\OA.exe
c#
.net
thread-safety
windows-forms-designer
access-violation
asked on Stack Overflow Sep 12, 2019 by user5606654

1 Answer

0

This is really interesting to explore - you may go with making dumps and exploring them with WInDbg. But I would consider creating the same application as Console Application and exploring its behavior. It may turn out it was some kind GUI related bug/feature where you reached maybe Handlers/GDI limit or something like this.

answered on Stack Overflow Sep 13, 2019 by Sergey Sheff

User contributions licensed under CC BY-SA 3.0