Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 Threading Client
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 3-Jul-12 0:18am
Comments
Wes Aday at 3-Jul-12 7:49am
   
"Any Ideas?" Yes rethink your architecture.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
Kodemaster123 at 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 at 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
Ragamayura at 6-Jul-12 6:15am
   
Good solution
Sergey Alexandrovich Kryukov at 11-Jul-12 16:04pm
   
Thank you, Ragamayura.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
Kodemaster123 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 165
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 5 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100