Handling connections and GUI at the same time

Windows specific forum
Kaiser
Enthusiast
Enthusiast
Posts: 118
Joined: Tue Jan 11, 2005 8:36 am

Handling connections and GUI at the same time

Post by Kaiser »

Hi again! :P 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 :(
maw

Post 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.
Kaiser
Enthusiast
Enthusiast
Posts: 118
Joined: Tue Jan 11, 2005 8:36 am

Post by Kaiser »

Aw dang it... any way to do it in PB 3.94?
maw

Post 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!
Kaiser
Enthusiast
Enthusiast
Posts: 118
Joined: Tue Jan 11, 2005 8:36 am

Post 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! :P
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4792
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post 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! :P
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.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
Kaiser
Enthusiast
Enthusiast
Posts: 118
Joined: Tue Jan 11, 2005 8:36 am

Post by Kaiser »

Hey! :D 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 :D.
Kaiser
Enthusiast
Enthusiast
Posts: 118
Joined: Tue Jan 11, 2005 8:36 am

Post by Kaiser »

:bump: :(
Kaiser
Enthusiast
Enthusiast
Posts: 118
Joined: Tue Jan 11, 2005 8:36 am

Post by Kaiser »

Bumping again...... nobody?
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post 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.
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
maw

Post 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.
Amundo
Enthusiast
Enthusiast
Posts: 200
Joined: Thu Feb 16, 2006 1:41 am
Location: New Zealand

Post by Amundo »

Hi Kaiser,

Hope it doesn't seem like I'm stalking you, but I've PM'd you.

Thanks!
Post Reply