Programm is simply closeing but.. why??

General FreeBASIC programming questions.
Post Reply
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Programm is simply closeing but.. why??

Post by mrminecrafttnt »

I am try to make an auto restarting sub after an crash but it will simply close my programm without errors?? I don't know why it will do this.. ?
This is mytherious.. :/

Code: Select all

#define thread_uninitalized 0
#define thread_running 1
#define thread_crashed 2

type thread_controller
    state as uinteger
    declare sub reboot(t_adress as any ptr,byref state as uinteger)
end type

sub thread_controller.reboot(t_adress as any ptr,byref state as uinteger)
    PRINT "REBOOTING..";
    if state = thread_crashed then
        PRINT "EXECUTE..";
        threadcreate t_adress
        sleep 100
        if state = thread_running then print "REBOOT SUCCESFULL" ELSE PRINT "SOMETHING IS WORONG."
    else
        print "NO REASON TO REBOOT"
    end if
end sub



sub instable_thread
    PRINT "INSTABLE THREAD IS RUNNING NOW"
    static as integer threads
    static as integer reboot_state
    threads+=1
    on error goto error_handler
    dim as thread_controller ctrl
    ctrl.state = thread_running
    reboot_state = thread_running
    do
    if int(rnd*10000)=0 then error 10000
    loop
    exit sub
    
    error_handler:
    print "ERRORHANDLER STARTED!"
    print "THREADS : ";THREADS
    ctrl.state = thread_crashed
    reboot_state = thread_crashed
    threads-=1
    PRINT "RESTARTING..";
    ctrl.reboot @instable_thread,reboot_state
    PRINT "END OF ERRORHANDLER!!"
end sub


instable_thread

sleep
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Programm is simply closeing but.. why??

Post by Tourist Trap »

Hello,

can you show an output of what it does before it crashes?
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Re: Programm is simply closeing but.. why??

Post by mrminecrafttnt »

Tourist Trap wrote:Hello,

can you show an output of what it does before it crashes?
Yep it is now stablilized to show what it does and where it fails.

Code: Select all

#define thread_uninitalized 0
#define thread_running 1
#define thread_crashed 2

type thread_controller
    state as uinteger
    declare sub reboot(t_adress as any ptr,byref state as uinteger)
end type

sub thread_controller.reboot(t_adress as any ptr,byref state as uinteger)
    PRINT "REBOOTING..";
    if state = thread_crashed then
        PRINT "EXECUTE..";
        sleep 100
        print t_adress
        sleep 100
        threadcreate t_adress
        sleep 100
        if state = thread_running then print "REBOOT SUCCESFULL" ELSE PRINT "SOMETHING IS WORONG."
        sleep 100
    else
        print "NO REASON TO REBOOT"
    end if
end sub



sub instable_thread
    PRINT "INSTABLE THREAD IS RUNNING NOW"
    static as integer threads
    static as integer reboot_state
    threads+=1
    on error goto error_handler
    dim as thread_controller ctrl
    ctrl.state = thread_running
    reboot_state = thread_running
    do
    if int(rnd*10000)=0 then PRINT "SEND ERROR" :  error 10000
    loop
    exit sub
   
    error_handler:
    print "ERRORHANDLER STARTED!"
    print "THREADS : ";THREADS
    ctrl.state = thread_crashed
    reboot_state = thread_crashed
    threads-=1
    PRINT "RESTARTING..";
    ctrl.reboot @instable_thread,reboot_state
    PRINT "END OF ERRORHANDLER!!"
end sub


instable_thread

sleep
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Programm is simply closeing but.. why??

Post by fxm »

I do not know what you want to do but the biggest mistake is that a thread-sub must have an Any Ptr parameter (even if not used):
sub instable_thread (byval p as any ptr = 0)
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Programm is simply closeing but.. why??

Post by D.J.Peters »

I fixed only the syntax of the code not the logic if there any ;-)

Joshy

Code: Select all

#define thread_uninitalized 0
#define thread_running 1
#define thread_crashed 2

type thread_proc as sub(byval param as any ptr)

type thread_controller
  state as uinteger
  declare sub reboot(byval proc as thread_proc, byref state as uinteger)
end type

sub thread_controller.reboot(byval proc as thread_proc,byref state as uinteger)
  PRINT "REBOOTING..";
  if state = thread_crashed then
    PRINT "EXECUTE..";
    threadcreate proc
    sleep 100
    if state = thread_running then print "REBOOT SUCCESFULL" ELSE PRINT "SOMETHING IS WORONG."
  else
    print "NO REASON TO REBOOT"
  end if
end sub

sub instable_thread(byval param as any ptr)
  static as integer threads
  static as integer reboot_state
  PRINT "INSTABLE THREAD IS RUNNING NOW"
  threads+=1
  on error goto error_handler
  dim as thread_controller ctrl
  ctrl.state = thread_running
  reboot_state = thread_running
  do
    if int(rnd*10000)=0 then error 10000
    sleep 10
  loop
  exit sub
   
error_handler:
  print "ERRORHANDLER STARTED!"
  print "THREADS : ";THREADS
  ctrl.state = thread_crashed
  reboot_state = thread_crashed
  threads-=1
  PRINT "RESTARTING..";
  ctrl.reboot @instable_thread,reboot_state
  PRINT "END OF ERRORHANDLER!!"
end sub


instable_thread(0)

sleep
Post Reply