Interesting facts you have there. I've read JMC's blog as recommended, but it didn't help anything too much. However, I found Niya's multi threading article more of use. I've read it before, but I didn't understand much of it at that time. When I compared and analyzed your code with hers, I somehow made an understanding of how the code worked. My TCP server works fine now, including my richtextbox! I recreated my project to fit the new code, and optimized it. I can mark this thread as resolved, as my main question has been solved, but I got another one.
The way my code works is following:
[LONG VERSION]
Main UI thread has a button, when pressed it will go to my
TCP class (Class file), where it will start a new thread to do the 'TCP.Server' handling. My TCP server functions in a command based system, where the client sends commands of either login, chat, chat to user, share screen, send file... you name it. When such a command is received with its unique message ID, the TCP server will call my
CommandHandler class (Class file). This class analyzes the commands and does the selected functions. Basically, this is where the magic happens.
The problem I now face, is when a command from my third class has been executed, it needs to be returned to the main UI thread. I think I might set the code up wrong, but in order to make it work; the '
CommandHandler' class needed to be
Private only, not shared or something. Which means when I request for the richtextbox output, it will be returned to my
TCP class, which runs on a different thread than the
main UI thread. For the time being I've added a Public shared variable in the main UI, which the TCP class will update when needed. I currently use a timer to append the output to the richtextbox, as I can't directly touch anything in my UI thread with my TCP class thread. What I'm looking for is something similar, but easier method than Niya's. Like, when the variable's text has been changed, append the text to the richtextbox. I think of that as a way better way than using a timer, which constantly runs and drain memory.
[SHORT VERSION]
I have three classes which does my TCP handling, the
main UI thread, the
TCP class thread and the
CommandHandler class. They work like this:
main UI thread(If server not running) -> Starts the
TCP class thread(If message from user received) ->
CommandHandler(Execute commands based on messages) class
The
CommandHandler class will output ex. chat string from users, which the
Main UI thread wants. But it stops half way in the process, the
TCP thread.
Is there a way to create a sub/function which will run when a variable's text is changed?
CommandHandler(Output information) ->
TCP thread(Edits
Main UI's thread myVariable) -> Main UI thread(Appends text to richtextbox when variable's text has been changed)
Note, I don't want any timers or unnecessary loops.
Code:
Dim myVariable as string
'When myVariable has been changed: Richtextbox1.text &= Environment.NewLine & myVariable
The Main UI thread can then append new data from the TCP thread, which received its data from the CommandHandler class.
Anyway, you have my deepest thanks
@ident for helping with this frustrating problem!
With kind regards,
- Frek