Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6773581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:41:07+00:00 2026-05-26T15:41:07+00:00

I’ve been struggling with a problem that I have for days now and I

  • 0

I’ve been struggling with a problem that I have for days now and I just cannot find a solution.

My problem is I have an application that calls a DLL (Metatrader 4 is the application). The DLL receives arrays and integers from Metatrader, does a lot of calculations, and returns a double value to Metatrader.

The C# DLL looks as follows:

using System;
using System.Collections.Generic;
using System.Text;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Linq;
using System.Threading;

using Encog;
using Encog.Neural.Networks;
using Encog.Neural.Networks.Layers;
using Encog.Neural.Networks.Training;
using Encog.Neural.Networks.Training.Propagation.Resilient;
using Encog.Engine.Network.Activation;
using Encog.ML.Data;
using Encog.ML.Data.Basic;
using Encog.App.Quant.Indicators;
using Encog.App.Quant;
using Encog.Neural.NeuralData;
using System.Data;
using System.ComponentModel;


namespace NN_1_DLL
{
public static class UnmanagedExports
{


    static DataTable dt;
    static DataGridView gridview;
    static Form frm;

    static double NeuralOutput = 0;

    static double LowNormalize;
    static double HighNormalize;
    static double dataHigh;
    static double dataLow;

    static int NNInputs;
    static int NNOutputs;

    static double[] trainingData;
    static double[] neuralInput;
    static int trainingDataSize;
    static int trainingSets;
    static int epochMax;
    static int hiddenLayerNeurons;


    static double[][] TRAINING_INPUT;
    static double[][] TRAINING_OUTPUT;
    static double[] NEURAL_INPUT;

    static bool isShowGUI;

    static bool FormInitiated = false;





    public static void GUI()
    {

        int i, j;


        dt = new DataTable("Table");

        for (i = 0; i < NNInputs; i++)
        {
            dt.Columns.Add("Input " + i, typeof(double));
        }

        dt.Columns.Add("Output", typeof(double));


        for (i = 0; i < TRAINING_INPUT.GetLength(0); i++)
        {
            DataRow dataRow = dt.NewRow();

            for (j = 0; j < TRAINING_INPUT[i].GetLength(0); j++)
            {

                //dataRow["Input " + j] = TRAINING_INPUT[i][j];
                dataRow["Input " + j] = ((((dataLow - dataHigh) * TRAINING_INPUT[i][j] - HighNormalize * dataLow + dataHigh * LowNormalize) / (LowNormalize - HighNormalize)));


            }

            //dataRow["Output"] = TRAINING_OUTPUT[i][0];
            dataRow["Output"] = ((((dataLow - dataHigh) * TRAINING_OUTPUT[i][0] - HighNormalize * dataLow + dataHigh * LowNormalize) / (LowNormalize - HighNormalize)));


            dt.Rows.Add(dataRow);

        }




        if (FormInitiated == false)
        {


            frm = new Form();
            frm.Name = "frm";
            frm.ControlBox = false;
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            frm.BackColor = System.Drawing.Color.LightGray;
            frm.Size = new System.Drawing.Size(800, 600);
            frm.Show();

            gridview = new DataGridView();
            gridview.Name = "gridview";
            gridview.Height = 600;
            gridview.Width = 800;
            frm.Controls.Add(gridview);

            FormInitiated = true;

        }


        gridview.DataSource = dt;


    }




    static void PrepareData()
    {

        double[] array_input = new double[NNInputs];
        double[] array_output = new double[NNOutputs];
        int i = 0;
        int j = 0;
        int refpoint = 0;


        //PREPARE DATA FOR NEURAL NETWORK

        dataHigh = Math.Max(trainingData.Max(), neuralInput.Max());
        dataLow = Math.Min(trainingData.Min(), neuralInput.Min());


        for (i = 0; i < trainingData.GetLength(0); i++)
        {
            trainingData[i] = (((trainingData[i] - dataLow) / (dataHigh - dataLow)) * (HighNormalize - LowNormalize) + LowNormalize);
        }

        NEURAL_INPUT = new double[neuralInput.GetLength(0)];

        for (i = 0; i < neuralInput.GetLength(0); i++)
        {
            NEURAL_INPUT[i] = (((neuralInput[i] - dataLow) / (dataHigh - dataLow)) * (HighNormalize - LowNormalize) + LowNormalize);
        }




        TRAINING_INPUT = new double[trainingSets][];
        TRAINING_OUTPUT = new double[trainingSets][];

        for (i = 0; i < trainingSets; i++)
        {
            refpoint = (i * (NNInputs + NNOutputs));

            for (j = refpoint; j < refpoint + NNInputs; j++)
            {

                array_input[j - refpoint] = trainingData[j];
            }

            refpoint = refpoint + NNInputs;

            for (j = refpoint; j < refpoint + NNOutputs; j++)
            {

                array_output[j - refpoint] = trainingData[j];
            }


            TRAINING_INPUT[i] = array_input;
            TRAINING_OUTPUT[i] = array_output;

            array_input = new double[NNInputs];
            array_output = new double[NNOutputs];

        }



    }




    static void ComputeNN()
    {

        int i = 0;

        BasicNetwork network = new BasicNetwork();
        network.AddLayer(new BasicLayer(new ActivationTANH(), true, NNInputs));
        network.AddLayer(new BasicLayer(new ActivationTANH(), true, hiddenLayerNeurons));
        network.AddLayer(new BasicLayer(new ActivationTANH(), true, NNOutputs));

        network.Structure.FinalizeStructure();
        network.Reset();

        IMLDataSet trainingSet = new BasicMLDataSet(TRAINING_INPUT, TRAINING_OUTPUT);

        ITrain train = new ResilientPropagation(network, trainingSet);

        int epoch = 1;


        do
        {
            train.Iteration();

            epoch++;
        } while ((epoch < epochMax));




        INeuralData input = new Encog.Neural.Data.Basic.BasicNeuralData(NNInputs);
        for (i = 0; i < NNInputs; i++)
        {
            input[i] = NEURAL_INPUT[i];
        }

        IMLData output = network.Compute(input);
        NeuralOutput = ((((dataLow - dataHigh) * output[0] - HighNormalize * dataLow + dataHigh * LowNormalize) / (LowNormalize - HighNormalize)));

    }






    [DllExport("NNExportDLL", CallingConvention = CallingConvention.StdCall)]
    static double NNExportDLL([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] double[] training_data, int training_data_size, int inputs, int outputs, int  training_sets, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] double[] neural_input, int epoch_Max, int hidden_Layer_Neurons, double Low_Normalize, double High_Normalize, int ShowGUI)
    {

        //Define some global variables

        LowNormalize = new double();
        HighNormalize = new double();
        NNInputs = new int();
        NNOutputs = new int();
        trainingData = new double[training_data_size];
        trainingDataSize = new int();
        trainingSets = new int();
        epochMax = new int();
        hiddenLayerNeurons = new int();
        neuralInput = new double[inputs];

        LowNormalize = Low_Normalize;
        HighNormalize = High_Normalize;
        NNInputs = inputs;
        NNOutputs = outputs;
        trainingData = training_data;
        trainingDataSize = training_data_size;
        trainingSets = training_sets;
        epochMax = epoch_Max;
        hiddenLayerNeurons = hidden_Layer_Neurons;
        neuralInput = neural_input;

        if (ShowGUI == 1) { isShowGUI = true; } else { isShowGUI = false; }

        if (isShowGUI == true)
        {
            PrepareData();
            ComputeNN();
            GUI();
        }


        if (isShowGUI == false)
        {
            PrepareData();
            ComputeNN();
        }


        return (NeuralOutput); 


    }

}
}

I have to use threading due to the fact that the GUI freezes during calculations, there is absolutely no response from the GUI(). I have tried the backgroundworker class but the DLL returns the value before the functions are completed and the GUI still freezes.

The GUI() is built from the data calculated in the PrepareData() and ComputeNN() function, so they have to be executed first before the GUI() function is executed. Also the PrepareData() and ComputeNN() functions must be completed before the function returns the double value to Metatrader.

I have absolutely no experience with threading and GUIs within DLL’s, so would appreciate any advice!

Maybe I’m trying to do something that is either impossible or stupid. The whole reason for the GUI is for me to see if the data is calculated as it is supposed to be (more of a way for me to debug the code). I will later on add charts and such to visualize the training process/data. If there is a better way to do this please let me know!*

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:41:07+00:00Added an answer on May 26, 2026 at 3:41 pm

    Try using the GUI thread’s dispatcher:

    double d = 0d;
    App.Current.Dispatcher.BeginInvoke((Action)(() =>{ 
            d = callYourMethod();
        }), System.Windows.Threading.DispatcherPriority.Normal);
    

    you can look at this link for a tutorial on the WPF dispatcher.

    Give the BackgroundWorker another try. Declare these static fields:

    private static BackgroundWorker prepareDataBW;
    private static BackgroundWorker computeNnBW;
    
    
    private static bool isFinishedPrepareData;
    private static bool isFinishedComputeNN;
    
    private static Action BackgroundWorkerFinishedAction;
    

    and these static methods (the event handlers):

            static void prepareDataBW_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                isFinishedPrepareData = true;
                BackgroundWorkerFinishedAction();
            }
            static void computeNnBW_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                isFinishedComputeNN = true;
                BackgroundWorkerFinishedAction();
            }
            static void BackgroundWorkerFinishedAction()
            {
                if(isFinishedComputeNN && isFinishedPrepareData && isShowGUI)
                {
                    UpdateUI();
                }
                else if(isFinishedComputeNN && isFinishedPrepareData)
                {
                    ShowGUI();
                }
            }
    

    then instead of:

        if (ShowGUI == 1) //Show GUI
        {
            PrepareData();
            ComputeNN();
            ShowGUI();
        }
    
        else
    
        {
            PrepareData();
            ComputeNN();
        }
    

    try something like this:

            prepareDataBW = new BackgroundWorker();
            prepareDataBW.DoWork += delegate { 
            prepareDataBW();
                };
                prepareDataBW.RunWorkerCompleted += new RunWorkerCompletedEventHandler(prepareDataBW_RunWorkerCompleted);
    
            computeNnBW = new BackgroundWorker();
            computeNnBW.DoWork += delegate { 
                computeNN(); 
            };
            computeNnBW.RunWorkerCompleted += new RunWorkerCompletedEventHandler(computeNnBW_RunWorkerCompleted);
    
            BackgroundWorkerFinishedAction += new Action(BackgroundWorkerFinishedAction);
            prepareDataBW.RunWorkerAsync();
            computeNnBW.RunWorkerAsync();
    

    Based on all of your code, and in order to test your app you should try this:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using RGiesecke.DllExport;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using System.Linq;
    using System.Threading;
    
    using Encog;
    using Encog.Neural.Networks;
    using Encog.Neural.Networks.Layers;
    using Encog.Neural.Networks.Training;
    using Encog.Neural.Networks.Training.Propagation.Resilient;
    using Encog.Engine.Network.Activation;
    using Encog.ML.Data;
    using Encog.ML.Data.Basic;
    using Encog.App.Quant.Indicators;
    using Encog.App.Quant;
    using Encog.Neural.NeuralData;
    using System.Data;
    using System.ComponentModel;
    
    
    namespace NN_1_DLL
    {
        public static class UnmanagedExports
        {
    
            static DataTable dt;
            static DataGridView gridview;
            static Form frm;
    
            static double NeuralOutput = 0;
    
            static double LowNormalize;
            static double HighNormalize;
            static double dataHigh;
            static double dataLow;
    
            static int NNInputs;
            static int NNOutputs;
    
            static double[] trainingData;
            static double[] neuralInput;
            static int trainingDataSize;
            static int trainingSets;
            static int epochMax;
            static int hiddenLayerNeurons;
    
    
            static double[][] TRAINING_INPUT;
            static double[][] TRAINING_OUTPUT;
            static double[] NEURAL_INPUT;
    
            static bool isShowGUI;
    
            static bool FormInitiated = false;
    
    
            public static BackgroundWorker bw = new BackgroundWorker();
    
            public static void Main()
            {
                initForm();
                bw.DoWork += delegate
                {
                    double d = NNExportDLL(
                    new double[] { 2d, 3.4, 5d }, 3, 2, 2, 1, new double[] { 2d, 3d, 5d, 6d }, 3, 100, 4d, 8d, 1);
                    Console.Write(d);
                };
                Application.Run(frm);
            }
    
    
            public static void initForm()
            {
                if (FormInitiated == false)
                {
                    frm = new Form();
                    frm.Name = "frm";
                    frm.ControlBox = false;
                    frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                    frm.BackColor = System.Drawing.Color.LightGray;
                    frm.Size = new System.Drawing.Size(800, 700);
                    //frm.ShowDialog();
    
                    FlowLayoutPanel flp = new FlowLayoutPanel();
                    flp.Dock = DockStyle.Fill;
    
                    frm.Controls.Add(flp);
    
                    gridview = new DataGridView();
                    gridview.Name = "gridview";
                    gridview.Height = 600;
                    gridview.Width = 800;
                    flp.Controls.Add(gridview);
    
                    Button b = new Button();
                    b.Text = "Refresh";
                    b.Click += new EventHandler(b_Click);
                    b.Dock = DockStyle.Bottom;
                    flp.Controls.Add(b);
    
                    FormInitiated = true;
                }
    
            }
    
            static void b_Click(object sender, EventArgs e)
            {
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                bw.RunWorkerAsync();
    
            }
    
            static void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                bw.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                RefreshData();
            }
    
    
            static void RefreshData()
            {
                int i, j;
                dt = new DataTable("Table");
                for (i = 0; i < NNInputs; i++)
                {
                    dt.Columns.Add("Input " + i, typeof(double));
                }
                dt.Columns.Add("Output", typeof(double));
                for (i = 0; i < TRAINING_INPUT.GetLength(0); i++)
                {
                    DataRow dataRow = dt.NewRow();
                    for (j = 0; j < TRAINING_INPUT[i].GetLength(0); j++)
                    {
                        //dataRow["Input " + j] = TRAINING_INPUT[i][j];
                        dataRow["Input " + j] = ((((dataLow - dataHigh) * TRAINING_INPUT[i][j] - HighNormalize * dataLow + dataHigh * LowNormalize) / (LowNormalize - HighNormalize)));
                    }
                    //dataRow["Output"] = TRAINING_OUTPUT[i][0];
                    dataRow["Output"] = ((((dataLow - dataHigh) * TRAINING_OUTPUT[i][0] - HighNormalize * dataLow + dataHigh * LowNormalize) / (LowNormalize - HighNormalize)));
                    dt.Rows.Add(dataRow);
                }
                gridview.DataSource = dt;
            }
    
            static void PrepareData()
            {
    
                double[] array_input = new double[NNInputs];
                double[] array_output = new double[NNOutputs];
                int i = 0;
                int j = 0;
                int refpoint = 0;
    
    
                //PREPARE DATA FOR NEURAL NETWORK
    
                dataHigh = Math.Max(trainingData.Max(), neuralInput.Max());
                dataLow = Math.Min(trainingData.Min(), neuralInput.Min());
    
    
                for (i = 0; i < trainingData.GetLength(0); i++)
                {
                    trainingData[i] = (((trainingData[i] - dataLow) / (dataHigh - dataLow)) * (HighNormalize - LowNormalize) + LowNormalize);
                }
    
                NEURAL_INPUT = new double[neuralInput.GetLength(0)];
    
                for (i = 0; i < neuralInput.GetLength(0); i++)
                {
                    NEURAL_INPUT[i] = (((neuralInput[i] - dataLow) / (dataHigh - dataLow)) * (HighNormalize - LowNormalize) + LowNormalize);
                }
    
    
    
    
                TRAINING_INPUT = new double[trainingSets][];
                TRAINING_OUTPUT = new double[trainingSets][];
    
                for (i = 0; i < trainingSets; i++)
                {
                    refpoint = (i * (NNInputs + NNOutputs));
    
                    for (j = refpoint; j < refpoint + NNInputs; j++)
                    {
    
                        array_input[j - refpoint] = trainingData[j];
                    }
    
                    refpoint = refpoint + NNInputs;
    
                    for (j = refpoint; j < refpoint + NNOutputs; j++)
                    {
                        if (trainingData.Length - 1 >= j)
                        {
                            array_output[j - refpoint] = trainingData[j];
                        }
                    }
    
    
                    TRAINING_INPUT[i] = array_input;
                    TRAINING_OUTPUT[i] = array_output;
    
                    array_input = new double[NNInputs];
                    array_output = new double[NNOutputs];
    
                }
    
    
    
            }
    
            static void ComputeNN()
            {
    
                int i = 0;
    
                BasicNetwork network = new BasicNetwork();
                network.AddLayer(new BasicLayer(new ActivationTANH(), true, NNInputs));
                network.AddLayer(new BasicLayer(new ActivationTANH(), true, hiddenLayerNeurons));
                network.AddLayer(new BasicLayer(new ActivationTANH(), true, NNOutputs));
    
                network.Structure.FinalizeStructure();
                network.Reset();
    
                IMLDataSet trainingSet = new BasicMLDataSet(TRAINING_INPUT, TRAINING_OUTPUT);
    
                ITrain train = new ResilientPropagation(network, trainingSet);
    
                int epoch = 1;
    
    
                do
                {
                    train.Iteration();
    
                    epoch++;
                } while ((epoch < epochMax));
    
    
    
    
                INeuralData input = new Encog.Neural.Data.Basic.BasicNeuralData(NNInputs);
                for (i = 0; i < NNInputs; i++)
                {
                    input[i] = NEURAL_INPUT[i];
                }
    
                IMLData output = network.Compute(input);
                NeuralOutput = ((((dataLow - dataHigh) * output[0] - HighNormalize * dataLow + dataHigh * LowNormalize) / (LowNormalize - HighNormalize)));
    
            }
    
            [DllExport("NNExportDLL", CallingConvention = CallingConvention.StdCall)]
            static double NNExportDLL(
                [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] double[] training_data,
                int training_data_size, int inputs, int outputs, int training_sets,
                [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] double[] neural_input,
                int epoch_Max,
                int hidden_Layer_Neurons,
                double Low_Normalize,
                double High_Normalize,
                int ShowGUI)
            {
    
                //Define some global variables
    
                LowNormalize = new double();
                HighNormalize = new double();
                NNInputs = new int();
                NNOutputs = new int();
                trainingData = new double[training_data_size];
                trainingDataSize = new int();
                trainingSets = new int();
                epochMax = new int();
                hiddenLayerNeurons = new int();
                neuralInput = new double[inputs];
    
                LowNormalize = Low_Normalize;
                HighNormalize = High_Normalize;
                NNInputs = inputs;
                NNOutputs = outputs;
                trainingData = training_data;
                trainingDataSize = training_data_size;
                trainingSets = training_sets;
                epochMax = epoch_Max;
                hiddenLayerNeurons = hidden_Layer_Neurons;
                neuralInput = neural_input;
    
                if (ShowGUI == 1) { isShowGUI = true; } else { isShowGUI = false; }
    
    
                PrepareData();
                ComputeNN();
    
                return (NeuralOutput);
    
    
            }
    
        }
    }
    

    the main problem was actually your trying to run the frm.show() without using Application.Run.

    Another edit: don’t forget to substitute what’s in the bw.DoWork delegate with your own logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.