Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
I need to run a function in a window from a seprate thread.

I am making a application that connects to a server. I need to make the connect function open a window that says that it's connecting. There is a function in that window that I need to call. The window needs to be on a seprate thread to the connecting thread. I can't use a new thread to connect so I need to put the window on the new thread.

To summerise:

- I am running a window in a seprate thread
- I need to call a function on that window from the original thread

Any Ideas?

Kodemaster123
Posted
Comments
[no name] 3-Jul-12 7:49am    
"Any Ideas?" Yes rethink your architecture.

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
 
Share this answer
 
Comments
Kodemaster123 3-Jul-12 19:40pm    
I am making an application library for basic communication. I need the tcp client to connect to the server on the main thread so it is still accessible to the application using the library. I want to show a dialog saying to the user that the connection is in progress. In the same dialog I want it to tell the user wether or not the computer could connect to the server. I have made a function that will tell the user this. I need to call this function from the main thread. I have found another way of doing it with the backgroundWorker class (can't believe I didn't think of this before). I am going to use the background worker progress value to tell the window whats going down. I.E. 0 - still working, 1 - worked, 2 - failed

EDIT--

Yeah, that didn't work... I just remembered that report progress only works from the new thread to the old one, not the old one to the new one
Sergey Alexandrovich Kryukov 3-Jul-12 22:33pm    
This is done via Invoke/BeginInvoke, as I explained before. Make a non-UI thread doing connection and communications, thread wrapper with notification event, in UI, add notification handler, but, as the handler will be called in a non-UI thread, call Invoke or BeginInvoke in the handler to notify the UI. That's it.
--SA
Raghuveer Kasyap 6-Jul-12 6:15am    
Good solution
Sergey Alexandrovich Kryukov 11-Jul-12 16:04pm    
Thank you, Ragamayura.
--SA
 
Share this answer
 
Comments
Kodemaster123 3-Jul-12 19:41pm    
(can't believe I didn't think of this before). I am going to use the background worker progress value to tell the window whats going down. I.E. 0 - still working, 1 - worked, 2 - failed

EDIT--

Yeah, that didn't work... I just remembered that report progress only works from the new thread to the old one, not the old one to the new one
Kodemaster123 3-Jul-12 20:40pm    
This does work, This may not be the best way of doing it but I will create a file that the other thread checks for to tell the other thread the status.
Okay, I fixed it myself. I used a background worker to create and display the dialog and on the dialog I made another background worker and it uses a while loop to wait until a file exsis, on the windows thread there is an event handler for "runworkercompleted" when the event is called (and the file exists) the windows thread reads the file and displays the correct message. On the origional thread the client makes the connection and when it is done creates the file with a success or fail code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900