Results 1 to 3 of 3

Thread: Confused (DIZZY!)

  1. #1
    Join Date
    Oct 2009
    Posts
    55
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Confused (DIZZY!)

    Well Caused made this tutorial, but the thing is i dont understand it:

    In this tutorial I'll be showing you how to use the TTimer object to run multiple functions or procedures at once. This is called Multithreading.

    It's very usefull when you want to create your progress report all the time the script is running, or when you just want to scan for a certain event, while a script is running.

    It's not "real" multithreading though. Because real MT typically enables you to safe Critical Sections, synchronize threads or vary the priority of a thread...

    Example Script (no timers):
    We are wanting to use an extra thread for our proggy in this script. We also want another thread, to do stuff, like "painting" a progress report or such.

    All of that will happen while our actuall script is looping the woodcutting procedures.

    Code:
    program Timers;
    var
       loadcount : integer;
    
    procedure proggy;
    begin
      ClearReport;
      AddToReport('Scriptname');
      AddToReport('Time Running: '+IntToStr(GetTimeRunning));
      AddToReport('Loads: '+IntToStr(loadcount));
    end;
    
    procedure Woodcutting;
    begin
      WriteLn('Woodcutting');
      wait(5000);
    end;
    
    procedure Dropping;
    begin
      WriteLn('Dropping');
      wait(5000);
    end;
    
    procedure mainloop;
    begin
      Woodcutting;
      Dropping;
      inc(loadcount);
    end;
    
    begin
    
      repeat
        mainloop;
       
        proggy;
      until(loadcount=10);
    
    end.

    Generating a Timer and calling it:
    We will generate a new form(not visible) and Create a new timer on that form. We may also apply our timer to the undetermined nil pointer, which would spare us the form, but may result in exceptions when not freed correctly.

    I hope i commented everything suffieciently. If you still got any question don't bother to ask .

    Code with timers/Multithreading:
    Code:
    program Timers;
    
    var
     //Timer and Form Vars
      frmDesign: TForm;
      thread1, thread2: TTimer;
     //proggy var
      loadcount : integer;
    
    procedure proggy;
    begin
      ClearReport;
      AddToReport('Scriptname');
      AddToReport('Time Running: '+IntToStr(GetTimeRunning));
      AddToReport('Loads: '+IntToStr(loadcount));
    end;
    
    //The procedure of our first thread/timer
    procedure t1(sender: TObject);
    begin
      proggy;
    end;
    
    //The procedure of our second thread/timer
    procedure t2(sender: TObject);
    begin
      writeln('Timer 2 Looping, painting the progress report and such');
    end;
    
    //Creating the Form
    procedure InitForm;
    begin
      frmDesign := CreateForm;
    end;
    
    //Creating the Timer
    procedure InitTimer;
    begin
      thread1 := TTimer.Create(frmDesign);
      thread2 := TTimer.Create(frmDesign);
    end;
    
    procedure SafeInitFormandTimer;
    var
      v: TVariantArray;
    begin
      setarraylength(v, 0);
      ThreadSafeCall('InitForm',v);
      ThreadSafeCall('InitTimer',v);
     
      //Function which is run everytime the timer loops
      thread1.OnTimer := @t1;
      //timer interval in ms
      thread1.Interval := 300;
      //enabled
      thread1.enabled := TRUE;
     
      //Same as above for the second timer/thread
      thread2.OnTimer := @t2;
      thread2.Interval := 200;
      thread2.enabled := TRUE;
    end;
    
    procedure Woodcutting;
    begin
      WriteLn('Woodcutting');
      wait(5000);
    end;
    
    procedure Dropping;
    begin
      WriteLn('Dropping');
      wait(5000);
    end;
    
    procedure mainloop;
    begin
      Woodcutting;
      Dropping;
      inc(loadcount);
    end;
    
    begin
      //Generating and setting up the timer
      SafeInitFormandTimer;
     
      repeat
        mainloop;
       
       // proggy;
      until(loadcount = 10);
    
      // And Freeing the memory
      FreeForm(frmDesign);
    end.
    Conclusion:
    So, this technique can be used whenever you need multiple tasks done at the "same time". Watch out that you do not write in the same var or use a function multiple times at the same time, that may cause access violations and stops the script.

    Another good thing, where a thread/timer comes in handy is, when you want your script to download a website, but you do not want the whole script to stop and wait till it's done. Or when you want to download multiple websites at once .

    If anything is unclear, feel free to ask .

    ~caused

    in the first code i dont understand how id make it so it calls upon my autoresponder durin the script the whole time u know. Help?
    Edgville WoodCutter Version 0.0.3 is out! Get it Now! Version 0.0.3!
    Get it here:
    http://www.villavu.com/forum/showthr...110#post648110
    Yesterday is history. - Tommorrow is a Mistery. - But today is a Gift.
    (Only if it was that simple)

  2. #2
    Join Date
    Sep 2007
    Location
    Michigan
    Posts
    3,862
    Mentioned
    2 Post(s)
    Quoted
    1 Post(s)

    Default

    I'd call your auto-responder in a random check procedure. TTimers can be unstable and crash the script/scar.
    (Scripts outdated until I update for new SRL changes)
    AK Smelter & Crafter [SRL-Stats] - Fast Fighter [TUT] [SRL-Stats]
    If you PM me with a stupid question or one listed in FAQ I will NOT respond. -Narcle
    Summer = me busy, won't be around much.

  3. #3
    Join Date
    Feb 2007
    Location
    Access Violation at 0x00000000
    Posts
    2,865
    Mentioned
    3 Post(s)
    Quoted
    18 Post(s)

    Default

    Yup. Did some tests with 'Multithreading' in scar and they can work properly for a while, but then stop your script out of the blue...
    Ce ne sont que des gueux


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •