| | |
Threading
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2006
Posts: 7
Reputation:
Solved Threads: 0
Hi
I have created a class that creates another thread. That background thread than creates a tcp client, connects to listener and waits for a message that will be send by the listener. When the message is received, it raises an event and waits for new messages. The problem is that the event is raised in background thread, instead in the thread that created that instance of my class. Does anybody has any idea how could I notify my main thread, so that thread could raise the event or if anybody has any other solution. I guess I need to implement something like Invoke method that every control has. but I don't know how I can do that. Any advice will be appreciated.
Thanks in advance
Uros
I have created a class that creates another thread. That background thread than creates a tcp client, connects to listener and waits for a message that will be send by the listener. When the message is received, it raises an event and waits for new messages. The problem is that the event is raised in background thread, instead in the thread that created that instance of my class. Does anybody has any idea how could I notify my main thread, so that thread could raise the event or if anybody has any other solution. I guess I need to implement something like Invoke method that every control has. but I don't know how I can do that. Any advice will be appreciated.
Thanks in advance
Uros
Last edited by Uros Bregar; Jun 14th, 2008 at 6:48 pm.
if I understand your problem correctly you are raising an event from your background thread and it is calling a function from your main class. and you probably are getting a runtime error. the reason for that error is that your main class is getting handled by your main thread and you are calling main method's function from background method. so what you have to do is ,like you said , to use invoke method. since you can not call any function accross threads, you should send the function to the main thread and main thread will run it for you. here is some sample code. it checks if we have to use invoke method(if there is a cross thread call) and send it to other thread and call the same method if needed. any way . if you have further questions please don't hesitate to ask.
mainwriting area is a rich text box.
mainwriting area is a rich text box.
C# Syntax (Toggle Plain Text)
private void write(string str) { if (MainWritingArea.InvokeRequired) { writeOnRichBox w = new writeOnRichBox(write); MainWritingArea.Invoke(w, new Object[] { str }); } else { MainWritingArea.Text = str + "\n" + MainWritingArea.Text; } }
I forgott to send you the coe fot delegate that I used in that function. I am sending the code again here it is.
if you try to change richtextbox.text property directly you might get a cross thread reference error. instead you should call write method. if there is no cross thread reference it will just edit the text , but if there is a cross reference it will use invoke method. invoke method gets a delegate and an object array as parameter. in the main thread it calls fires delegate and sends objects list to it as parameter. in our case when we write
it will run
C# Syntax (Toggle Plain Text)
public delegate void writeOnRichBox(string str); private void write(string str) { if (MainWritingArea.InvokeRequired) { writeOnRichBox w = new writeOnRichBox(write); MainWritingArea.Invoke(w, new Object[] { str }); } else { MainWritingArea.Text = str + "\n" + MainWritingArea.Text; } }
C# Syntax (Toggle Plain Text)
writeOnRichBox w = new writeOnRichBox(write); MainWritingArea.Invoke(w, new Object[] { str });
write(str); method in the main thread. •
•
Join Date: Jul 2006
Posts: 7
Reputation:
Solved Threads: 0
Thanks for your reply. I think I wasn't specific enough. As I wrote I have a class that defines an event(MessageReceived). Beside that it also has a background thread in which I connect to tcp lisener and wait for incoming messages. When i receive the message(that happens in the background thread) I want to fire the MessageReceived event, but I want it to fire in the main thread. I am not actualy updating any control so i can not use the control.Invoke - I just want to raise the event in the same thread, in which the instance of my class was created.
hello again!
first of all since you are listening your connection from the thread you will have to throw acception from your client. and your code in your backgroun thread probably will look like this
which will end with an error so like you have to call Message Received from the main thread like this
which does the same thing, only does it on the thread which Form1 belongs to. I don't know if this is helping you but there is also various examples about Invoke Method. you might wanna check them out too.. I needed the same functions couple days ago and google it.
first of all since you are listening your connection from the thread you will have to throw acception from your client. and your code in your backgroun thread probably will look like this
C# Syntax (Toggle Plain Text)
public delegate void PassMessage(string str); public event PassMessage MessageReceived; .......... while (true) { if (stream.DataAvailable) MessageReceived(getMessage()); }
which will end with an error so like you have to call Message Received from the main thread like this
C# Syntax (Toggle Plain Text)
public delegate void PassMessage(string str); public event PassMessage MessageReceived; .......... while (true) { if (stream.DataAvailable){ PassMessage p = new PassMessage(MessageReceived); Form1.Invoke(p,new Object[] {getMessage()} } }
![]() |
Similar Threads
- how would you apply threading to a web server application (C++)
- good threading tutorials (C++)
- Threading Issue (C#)
- pre-threading and forking (Linux Servers and Apache)
- Threading (multi threading) in Linux (C)
- Multi-threading with C++.NET (C++)
Other Threads in the C# Forum
- Previous Thread: DragDrop event
- Next Thread: Bluetooth communication in dot net 1.1
| Thread Tools | Search this Thread |
.net access ado.net algorithm array backup barchart bitmap box broadcast buttons c# check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime decryption degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ httpwebrequest i18n image imageprocessing index input install java label list listbox listener mandelbrot math microsystems mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer update uploadatextfile usercontrol users validation view visualstudio webbrowser whileloop windows winforms wpf xml





