PostEvent / thread problem

Just starting out? Need help? Post your questions and find answers here.
camille
User
User
Posts: 66
Joined: Tue Nov 19, 2019 12:52 pm

PostEvent / thread problem

Post by camille »

Hi,

I'm trying to do something probably very simple thing: Fill an editor gadget with a threaded

Code: Select all

cmd /c dir c:\windows\system32

Code: Select all

DisableExplicit

Enumeration EventCustomValue #PB_Event_FirstCustomValue
  #MyEvent_ThreadStart
  #MyEvent_ThreadUpdate
  #MyEvent_ThreadEnd
EndEnumeration

Structure udtThreadStatus
  line.s
EndStructure

Define thread.udtThreadStatus

Procedure GetCMDFolderContent(*thread.udtThreadStatus)

  comspec$ = GetEnvironmentVariable("comspec")

  DOS = RunProgram(comspec$, "/c dir c:\windows\system32", "", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)

  If DOS
    PostEvent(#MyEvent_ThreadStart)
    While ProgramRunning(DOS)
      If AvailableProgramOutput(DOS)
        *thread\line = ReadProgramString(DOS)
        ;Debug *thread\line
        PostEvent(#MyEvent_ThreadUpdate)
      EndIf
    Wend
    CloseProgram(DOS)
    PostEvent(#MyEvent_ThreadEnd)
  EndIf

EndProcedure

If OpenWindow(0, 320, 320, 422, 250, "DOS Output", #PB_Window_SystemMenu)
  EditorGadget(1, 8, 8, 406, 233)

  *thread.udtThreadStatus = AllocateMemory(SizeOf(udtThreadStatus))
  cmdThreadID = CreateThread(@GetCMDFolderContent(), *thread)
  Repeat
    event = WaitWindowEvent()

    Select event
      Case #MyEvent_ThreadStart
        Debug "Thread started"

      Case #MyEvent_ThreadUpdate
        ;Debug "Thread updated"
        Debug *thread\line
        ;AddGadgetItem(1, -1, *thread\line)

      Case #MyEvent_ThreadEnd
        Debug "Thread ended"
    EndSelect

  Until event = #PB_Event_CloseWindow
EndIf
When I run this, the debug output in the main loop produces dozens of duplicated lines.
When I comment that out and use the debug statement in the thread function, everything looks fine.

Why does this happen, how do I avoid this?
User avatar
HeX0R
Addict
Addict
Posts: 992
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: PostEvent / thread problem

Post by HeX0R »

You can't do that like this!

Here is a quick'n dirty fix for your code

Code: Select all

EnableExplicit

Enumeration EventCustomValue #PB_Event_FirstCustomValue
	#MyEvent_ThreadStart
	#MyEvent_ThreadUpdate
	#MyEvent_ThreadEnd
EndEnumeration

Procedure GetCMDFolderContent(*dummy)
	Protected *Buff, a$, comspec$, DOS

	comspec$ = GetEnvironmentVariable("comspec")

	DOS = RunProgram(comspec$, "/c dir c:\windows\system32", "", #PB_Program_Hide | #PB_Program_Open | #PB_Program_Read)

	If DOS
		PostEvent(#MyEvent_ThreadStart)
		While ProgramRunning(DOS)
			If AvailableProgramOutput(DOS)
				a$    = ReadProgramString(DOS)
				*Buff = AllocateMemory(StringByteLength(a$) + 2)
				PokeS(*Buff, a$)
				PostEvent(#MyEvent_ThreadUpdate, 0, 0, 0, *Buff)
			EndIf
		Wend
		CloseProgram(DOS)
		PostEvent(#MyEvent_ThreadEnd)
	EndIf

EndProcedure

Procedure main()
	Protected cmdThreadID, event, *B
	
	If OpenWindow(0, 320, 320, 422, 250, "DOS Output", #PB_Window_SystemMenu)
		EditorGadget(1, 8, 8, 406, 233)

		cmdThreadID             = CreateThread(@GetCMDFolderContent(), 0)
		Repeat
			event = WaitWindowEvent()

			Select event
				Case #MyEvent_ThreadStart
					Debug "Thread started"

				Case #MyEvent_ThreadUpdate
					;Debug "Thread updated"
					*B = EventData()
					Debug PeekS(*B)
					FreeMemory(*B)

				Case #MyEvent_ThreadEnd
					Debug "Thread ended"
			EndSelect

		Until event = #PB_Event_CloseWindow
	EndIf
EndProcedure

main()
User avatar
mk-soft
Always Here
Always Here
Posts: 5386
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PostEvent / thread problem

Post by mk-soft »

Show perhaps MiniThreadControl

Link: viewtopic.php?f=12&t=73231
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply