Just a bug in your code. Background threads cannot access the ui components directly. They must be call from the UI thread. To do do you use the dispatcher.
simulation causes bug : System Access Violation

I've been working with a BackgroundWorker that is kicked into gear often and regularly.
It needs to make use of its own RichTextbox in order to calculate and complete its task of drawing a bitmap representation of a selected word's distribution in the text provided with an Rtf string (used to replicate the original RichTextBox it is drawing without causing a threading error).
the method (backgroundworker) works great. does exactly what it is supposed to do. but after some time (30 minutes last Crash-Test) the app crashes and complains about a "System Access Violation"
This problem has been occurring in an app I'm working on and I've tried changing the "Platform Target" to "x86" when another post suggested that making that change (from "Any CPU" to "x86") would correct this problem but the error persists.
I've made a 'Crash Test' app that demonstrates the problem which can be downloaded from my GoogleDrive and was written in MS C#2019
download zipped file with Word Map Crash Test solution
Running the app's executable only took a few minutes for it to crash and terminate. If you load it in VS C#2019 and run it in debugger mode you'll see where it crashes and dies inside a Try-Catch. Just check the 'Auto Test' check box and it will quickly tell the background worker to repeatedly draw WordMaps of random words in the text and you'll get that same System Access Violation error.
I've run the Crash-test a few times and had it report how many Bitmaps it was able to draw before crashing.
These are the results:
421
1907
2389
672
384
604
332
278
91
120
320
993
2100
1438
1038
295
352
1714
858
46
500
2479
1083
170
501
61
2898
1618
3356
5420
444
1000
188
942
243
1961
1253
3659
774
1397
there doesn't seem to be any pattern ... its as if it was random?
the time it takes to crash may be related to the amount of memory available on my computer. Since this is the first time I've ever encountered this problem with a background worker, I ~think~ it has something to do with the RichTextBox it needs to create and dispose so frequently ... but really, I have no idea. If you can figure out what the problem is, you're an MS rockstar.
Developer technologies Visual Studio Debugging
Sysinternals
Developer technologies Visual Studio Other
Developer technologies C#
2 answers
Sort by: Most helpful
-
Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
2022-04-17T18:36:25.12+00:00 -
Christ Kennedy 41 Reputation points
2022-04-19T17:26:24.543+00:00 I've solved the problem by no longer resorting to the BackgroundWorker which was crashing the app.
There is now a Timer which starts to Tick every 100ms and reproduces what the BackgroundWorker usies a FiniteStateMachine that cycles through the states :public enum enuDrawMap_Stages { Idle, // exit CopyRTX, // creates the needed objects CalculateSize, // makes some necessary measurements CreateDivisions, // divides the final bitmap into small workable rectangles called Divisions DrawDivisions, // draws one division every time the Timer Tick event occurs _numDrawMap_Stages };
- this way the UI has time to manage the User's inputs even though the map is being drawn
because the map drawing task has been broken down into so many much smaller parts - there was also an earlier issue that produced an odd behavior in my Words : Creative Writer's Word Processor project
- the code for which can viewed here Words
- a context menu was appearing at a distance below the mouse
- an issue I posted about here context-menu-appearing-far-from-mouse.html
- the distance between the mouse and menu was relative to the splitterDistance that held the picturebox & its faulty ContextMenu
- with the resolution of the System Access Violation error by avoiding the use of the BackgroundWorker here
the ContextMenu location error has disappeared and may be related
- this way the UI has time to manage the User's inputs even though the map is being drawn