Kodemaster123 wrote:
I can't use a new thread to connect so I need to put the window on the new thread
Wrong. Not even close to truth. You don't need windows of forms for threading, you don't need them for communications.
Now, calling a function from original thread… First of all, let's agree that you do not create another thread with a window (not clear what it is supposed to mean; another instance of
Application
? it would be possible, but why? A window? A window means nothing outside application, unless you show it as modal; again, why?) — it makes no sense. Basically, you can call any function from any thread: functions are agnostic to threads, but the result of it? It depends on the data parameters passed, notably "this" parameter and you can get in a big trouble if you share some objects between different threads, in particular, it these are UI objects.
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[
^].
—SA