Page 1 of 1
Handling connections and GUI at the same time
Posted: Wed Mar 22, 2006 10:43 pm
by Kaiser
Hi again!

sorry to be a bother ^^;;...
Anyhow! this is something that has got me going around for a lot, and it's that I wonder how it's possible to handle all GUI functions at the same time there's a ReceiveNetworkData function? I mean... say, if I open a pop-up menu due to a right-click, the program stops receiving data until I select the option, or also what about if I need to handle a configuration window while also receiving network data -and- updating it in the main window? I'm working on a customizable chat program, so that's why my question, I mean to make the user able to customize his/her options without the window interrupting the main program core's flow...
Also tried making a Thread and placing the network stuff there, but it didn't work

Posted: Thu Mar 23, 2006 3:45 pm
by maw
Threads are the way to do it. In one of my programs I'm using a thread to update a ListViewGadget with new entries in the main window so that it gets updated even when a user drags the window. Using network code in a thread should be no problem!
Just remember to use ThreadSafe compiler option! And if there ever is a risk that the same string might be read/written to from both main program and thread at the same time you must Mutex it.
ThreadSafe is only available in PB4.
Posted: Thu Mar 23, 2006 6:07 pm
by Kaiser
Aw dang it... any way to do it in PB 3.94?
Posted: Thu Mar 23, 2006 6:14 pm
by maw
Yes.. But you have to use Mutex every single time you use a string even the string is only used in one place. That's because all string manipulations in PB uses a shared buffer..
So I would seriosly recommend using PB4 for it!
Posted: Fri Mar 24, 2006 2:27 am
by Kaiser
Okay so... what's Mutex and how to use it? I'm sure there have been people who have done apps that require network functions and handling GUI at the same time... using PB 3.94!

Posted: Fri Mar 24, 2006 4:18 am
by Fangbeast
Kaiser wrote:Okay so... what's Mutex and how to use it? I'm sure there have been people who have done apps that require network functions and handling GUI at the same time... using PB 3.94!

G'day. There are lots of chat clients written in PB3.94 and earlier that mated gui with network functions. Have a look at Pureare.net and find the codearchive and the showcase areas. I think there are still things there to help you.
And as for a mutex, it's just a flag to tell every other procedure that "This thing is in use, don't touch!!"
global mutex.l
procedure mutilatestring(name.s)
if mutex = 0 ; If nothing else is playing with strings
mutex = 1 ; lock it for our own use
"Mr " + name.s ; now do what we have to
endif
mutex = 0 ; Other procedures are now allowed to do string work
endprocedure
procedure stuffupaddress(address.s)
if mutex = 0 ; If nothing else is playing with strings
mutex = 1 ; lock it for our own use
"No and Street " + address.s ; now do what we have to
endif
mutex = 0 ; Other procedures are now allowed to do string work
endprocedure
So we have 2 procedures manipulating strings and each one tries to lock the string buffer for it's own exclusive use so that neither of the two; separate procedures; can possibly trash each other.
Okay, so what happens if you run both procedures and only one gets done because of this locking? Create a thread that monitors your variables and checks to see if they are filled before continuring on to something else. Make the thread either check and recheck that all variables are completed or make the thread re-launch the procedures from a common stack with the variables you want those procedures to finish.
Posted: Sun Mar 26, 2006 4:18 am
by Kaiser
Hey!

that's clever and I think I can work around a small practice program for that... but the thread for monitoring variables confuses me a bit... I guess. Know any program or example that shows this "Mutex" method? (using network functions, of course), it would be greatly appreciated, really

.
Posted: Fri Mar 31, 2006 5:23 pm
by Kaiser
:bump:

Posted: Mon Apr 03, 2006 8:30 pm
by Kaiser
Bumping again...... nobody?
Posted: Wed Apr 12, 2006 2:44 pm
by dell_jockey
Kaiser,
mutexes are only needed, when you need to update variables from multiple threads.
In the setup you described in your original question, the GUI part would probably only need to read the variables to be able to display them. If that is indeed the case, (GUI only reading data structure contents) then you can forget about mutexes.
The key point is that you want to guarantee that only one single thread is updating a variable at a time. It you only have a single thread that needs write access to variables, no mutexes are needed.
Posted: Wed Apr 12, 2006 2:52 pm
by maw
Actually, that's not true. Without threadsafe, as is the case with PB 3.94, you have to mutex every single read or write if you use threads. That's because PB use a single string buffer and if you read and/or write strings at the same time, even if not the same string, you will run into problems.
Posted: Thu Apr 20, 2006 6:50 am
by Amundo
Hi Kaiser,
Hope it doesn't seem like I'm stalking you, but I've PM'd you.
Thanks!