Page 1 of 1

Threads

Posted: Sat Nov 19, 2011 1:56 pm
by deseven
Hello.

I'm trying to write a simple program which can sync clipboards between two computers.

I started with basic examples from help file (NetworkClient.pb and NetworkServer.pb) and put all server-client communications in different threads. It works fine under windows, but i can't manage to run it under OS X.

If i run it just as it is the debugger tells me about "invalid memory access".
If i run it with "Create threadsafe executable" option checked - application just hangs and nothing happens.

Sources: http://dl.dropbox.com/u/943974/sync.zip

What am i doing wrong?

Re: Threads

Posted: Sat Nov 26, 2011 7:50 am
by deseven
Anyone?..

Re: Threads

Posted: Sat Nov 26, 2011 9:17 am
by jesperbrannmark
func.pb

Code: Select all

Procedure AddToLog(log,string.s)
  CurTime.s = FormatDate("%hh:%ii:%ss",Date())
  Select log
    Case 1
      If IsGadget(1)
        Debug CurTime + ": " + string
      EndIf
    Case 2
      If IsGadget(2)
        Debug CurTime + ": " + string
      EndIf
  EndSelect
EndProcedure
?

Re: Threads

Posted: Sat Nov 26, 2011 9:20 am
by jesperbrannmark
Why

Code: Select all

ListenThread = CreateThread(@ListenServer(),ListenPort)
  ConnectThread = CreateThread(@ConnectClient(),ConnectPort)
  ClipCheckThread = CreateThread(@ClipCheck(),dummy)
instead of running one main thread?

Re: Threads

Posted: Sat Nov 26, 2011 2:49 pm
by deseven
Thank you.

Well, looks like it runs fine without AddGadgetItem(), but why?
And how can i work with user interface if i can't use gadgets inside threads?
jesperbrannmark wrote:Why
instead of running one main thread?
Because i want to control whether i need the server/client or not. And also because there's a delay in clipboard check (without the delay it just eats too much cpu).

Re: Threads

Posted: Sun Nov 27, 2011 11:08 am
by jesperbrannmark
This is a bad hack but you could proably do something like this

Code: Select all

Global GadgetString.s = ""
;The thread
Procedure Thread(x.l)
  GadgetString.s = "Hello"
  Repeat
    Delay(5)
  ForEver
EndProcedure
OpenWindow(0,100,100,640,480,"Test")
TextGadget(1,0,0,100,100,"")
;Start thread
TheThread = CreateThread(@Thread(),dummy)
Repeat
  If GadgetString
    SetGadgetText(1,GadgetString)
    GadgetString=""
  EndIf
Until WaitWindowEvent(50)=#PB_Event_CloseWindow
So use a global string...

Re: Threads

Posted: Sun Nov 27, 2011 2:48 pm
by deseven
I thought about something like that, but that's really ridiculous :)

So, is this a bug? What do you think?

Re: Threads

Posted: Sun Nov 27, 2011 3:54 pm
by jesperbrannmark
I havnt looked at the code in detail. But I know from my own stuff that you shouldnt do gadget stuff in a thread. First I thought I just start one thread per window and all would be fine.
That doesnt work very well on windows and on mac.. its not possible.
So main thread handle windowrelated events... and extra threads handle the extra stuff.

Its not a bug, its just something you probably need to adapt when doing stuff on the mac (and the pc, but on pc you dont get a crash)

Re: Threads

Posted: Sun Nov 27, 2011 4:14 pm
by deseven
jesperbrannmark wrote:you shouldnt do gadget stuff in a thread
If so - what is the correct way to do that? Put info in different global variables and monitor them for changes? That's not really good i think...

However, thanks for your help.