The question makes no sense at all. If you really wanted to "prevent multithreading", you would not use threads at all. The problem not to avoid it, but to use threads properly. It's also not very nice for you to ask something based on your threading concern, but to say nothing about your threads, showing no thread-related code.
By these reasons, I'll answer only to a degree allowed by the information I could get from your exception information (and even that is not precise: you need to provide comprehensive exception information: type, message, parameters or inner exceptions, if any, indicate the line of code where the exception is thrown, etc). Actually, such problems are resolved relatively easily, because the UI libraries (
System.Windows.Forms
, WPF) are equipped to solve them.
You cannot call anything related to UI from non-UI thread. Instead, you need to use the method
Invoke
or
BeginInvoke
of
System.Windows.Threading.Dispatcher
(for both Forms or WPF) or
System.Windows.Forms.Control
(Forms only).
You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[
^],
Problem with Treeview Scanner And MD5[
^].
See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[
^],
Control events not firing after enable disable + multithreading[
^].
I only assumed that your exception were thrown from UI, first, because this is the most typical problem, and, second, because you show some UI code. In more general case, you would need to delegate some calls from one thread to another. In particular, there is a number of some other APIs requiring all the call from a single thread, but the logic of your application often requires using such APIs in different threads. The right approach to such situations is to have a single thread to work with such API; and all other threads should use this thread as a server to serve those calls. So, this special thread should be programmed in a special way to serve those requests, and provide a similar mechanism for delegating the calls to it from other threads.
To understand how to make such mechanism, please see my article, complete with source code and usage samples:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[
^].
—SA