ScriptUO

Scripting Resources & Utilities => Stealth Client => Topic started by: epsilon2 on January 13, 2016, 07:15:41 AM

Title: Disconnect() in Pascal and C#, why does ist not work?
Post by: epsilon2 on January 13, 2016, 07:15:41 AM
Hi,

ich playaround a bit with stealth and come to a point where I need help.

Ii used:

pascal:

Code: [Select]
[sup]//main script:
program test1;
//Uses Gumps;
Uses Api;

BEGIN

UOSay('Hello World');
Disconnect();
 
END.
[/sup]

c#:

Code: [Select]
[sup]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ScriptSDK.API;
using ScriptSDK.Attributes;
using ScriptSDK.Configuration;
using ScriptSDK.ContextMenus;
using ScriptSDK.Items;
using ScriptSDK.Utils;
using ScriptSDK.Data;
using ScriptSDK.Engines;
using ScriptSDK.Mobiles;
using ScriptSDK.Targets;
using ScriptSDK.Gumps;

namespace WpfApplication1
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    ///
   

    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            ushort xx =0;
            InitializeComponent();
                   
            Stealth.Client.SendTextToUO("Hello World");
            Stealth.Client.SetSilentMode(true);
 
            ScriptLogger.Initialize(); // Should be always called once before using the ScriptLogger
            ScriptLogger.LogToStealth = true; //Allows logger to Write to Stealth UI Console
            ScriptLogger.LogToConsole = true; //Allows logger to Write to .Net Console (if active)
            ScriptLogger.LogToIDE = true; //Allows logger to write to Visual Studio related Debug Log
            ScriptLogger.LogToFile = true;
            ScriptLogger.FileName = "C:\\Users\\epsil\\Desktop\\EsyUO + scripte\\Stealth\\scripts\\BodMiner\\WpfApplication1\\bin\\Debug\\test.txt";
            /*
             Sends Text to Enabled Engines. On IDE and Console its a print without linebreak, on Stealth with Linebreak
             */
            ScriptLogger.Write("Hello World");
            /*
             Sends Text to Enabled Engines. On IDE and Console its a print with linebreak
             */
            uint z = Stealth.Client.GetGumpsCount() - 1;

            for (xx = 0; xx <= z; xx++)
            {
                var TEXT = Stealth.Client.GetGumpFullLines(xx);
                for (int x = 0; x <= TEXT.Count(); x++)
                { ScriptLogger.Write(TEXT[x]); }
            }

   //         System.Threading.Thread.Sleep(115000);
            Stealth.Client.Disconnect();
            Stealth.Client.Wait(10000);
 //           System.Threading.Thread.Sleep(35000);
 //           Stealth.Client.Connect();
 //           System.Threading.Thread.Sleep(35000);
 //           Stealth.Client.Disconnect();
 //           System.Threading.Thread.Sleep(35000);
 //           Stealth.Client.ChangeProfile("England3.0");
 //           Stealth.Client.Connect();
 //           System.Threading.Thread.Sleep(35000);

        }
    }
}[/sup]

why does it nor work? Stealth or Client do not Disconnect.

regards.
Title: Re: Disconnect() in Pascal and C#, why does ist not work?
Post by: Crome969 on January 13, 2016, 10:29:54 AM
So i modded your text to add the code tag around.

Now can look at the issue :

1) In Pascal, stealth handles the threading itself but in c#, if you use ui, you need to handle code in a thread or you will kill your application.

2) You need a backgroundworker or a thread handle the api calls else either the api calls or the handling of ui may not work as intendet.
You could check a sample here  : https://sourceforge.net/projects/scriptsdk/files/Samples/
I made there a sample project using windows forms and having a start\stop of thread via ui and back and forward interactions.

3) If this still is an issue, i can check if disconnect may works not as intended and fix it.



PS : I just see you also say pascal not work, lemme confirm this..

PPS : Works for me using this script :


Code: [Select]
Program New;
begin
 Disconnect;
end.


PPPS : Ich mag deine deutschen Comments im C# code :P
Title: Re: Disconnect() in Pascal and C#, why does ist not work?
Post by: epsilon2 on January 14, 2016, 03:43:26 AM
Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using ScriptSDK.Engines;
using ScriptSDK.Data;
using ScriptSDK.API;

namespace App.RebirthUO.Scripts.UIThreadTest
{
    public partial class UITester : Form
    {
        /// <summary>
        /// Should be called in Mainmethod, handles the view of UI.
        /// Use UITester.Perform(); to load UI.
        /// </summary>
        public static void Perform()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new UITester());
        }

        /// <summary>
        /// Default Constructor wich constructs components
        /// </summary>
        public UITester()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Button Event for Play-Button Starts Thread if not running yet.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbplay_Click(object sender, EventArgs e)
        {
            if (!ScriptThread.IsBusy)
                ScriptThread.RunWorkerAsync();
        }

        /// <summary>
        /// Button Event for Stop-Button Stops Thread if already running.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbstop_Click(object sender, EventArgs e)
        {
            if(ScriptThread.IsBusy)
                ScriptThread.CancelAsync();
        }

        /// <summary>
        /// Code handled by Thread.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScriptThread_DoWork(object sender, DoWorkEventArgs e)
        {
            // Stores "Thread" to var
            var worker = (BackgroundWorker)sender;

            // Invokes Thread safe Actions onto UI. In this case disable play button , enable stop button.
            Invoke((MethodInvoker)delegate() { tsbplay.Enabled = false; });
            Invoke((MethodInvoker)delegate() { tsbstop.Enabled = true; });

            //Enables logger methods from SDK
            ScriptLogger.Initialize();

            //Initializes Loggin Event, wich performs an action whenever ScriptLogger gets new Logfiles.
            ScriptLogger.OnLogging += ScriptLogger_OnLogging;

            //Sample loop to repeat code inside, until thread gets stopped or application closed.
            while (!worker.CancellationPending)
            {
                //Sends Text to Logger, wich shares it onto UI through event.
                ScriptLogger.WriteLine("Hello Thread!");
                Stealth.Client.Disconnect();
                // Let the thread sleep when not requiring actions. Thread.Sleep works better then "Waits" via Api-Call.
                Thread.Sleep(500);
            }

            // Invokes Thread safe Actions onto UI. In this case disable stop button, enable play button.
            Invoke((MethodInvoker)delegate() { tsbplay.Enabled = true; });
            Invoke((MethodInvoker)delegate() { tsbstop.Enabled = false; });
        }

        /// <summary>
        /// Event wich gets performed by thread (as Event to Scriptlogger) and writes send data via Invoke onto UI.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ScriptLogger_OnLogging(object sender, ScriptLoggerArgs e)
        {
            Invoke((MethodInvoker)delegate()
            {
                lblogger.Items.Add(string.Format("{0} : {1}", DateTime.UtcNow, e.Text));
            });
        }

        /// <summary>
        /// Just sample code for the Contextmenu onto Listbox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lblogger.Items.Clear();
        }

        /// <summary>
        /// Just sample code for the Contextmenu onto Listbox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            File.WriteAllLines(DateTime.UtcNow.ToFileTimeUtc() + ".log", lblogger.Items.Cast<string>().ToList());
        }
    }

}

works fine
Title: Re: Disconnect() in Pascal and C#, why does ist not work?
Post by: Crome969 on January 14, 2016, 05:28:05 AM
Then it was your code, you need to keep in mind to handle threads properly, that sample show how to..

PS : Please use the "code" tag by start [ code ] and then [ /code ] as ending (without space). Makes it much easyier to read.