Hey everyone,

I've been struggling with this problem for hours now and I have read and tried every example I could find but did not find a solution to my problem:

I have a DLL written in C#. The DLL export function calls three functions:

PrepareData(): Heavy calculations
ComputeNN(): Mild calculations
GUI(): Creates/updates GUI that uses above calculation values

So basically the DLL looks as follows:

[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 epochMax, int hiddenLayerNeurons, double Low_Normalize, double High_Normalize, int ShowGUI)
{


   PrepareData(training_data, training_data_size, training_sets, neural_input);
   ComputeNN(epochMax, hiddenLayerNeurons);
   GUI();

   return (NeuralOutput);

}

The DLL is called by an external application many times. I have coded the GUI so that at first run the GUI is created, and onwards it just gets updated. The problem is the GUI is obviously unresponsive due to the calculation functions using the threads the whole time.

I have tried many threading functions but I cannot get it to work, would appreciate it if someone could give me some advice.

Thanks!

Some indication of what you have tried would be helpful.

Is this a windows forms application or some other flavour of C#?

It seems the DLL is written by you. Is this correct?

What is calling the DLL and how?

Is ComputeNN dependant upon the results of PrepareData?

I thought about your problem a bit more.
What do you think about the following.

Remove the GUI call from NNExportDLL function and make it an external function (or wrapper it in an GUIUpdate function).
Then make the calling app call NNExportDLL in background threads and call GUIUpdate at a less frequent interval on its main thread.

All you have to is to call all heavy-computing ops to different thread other things become incerdibly slooooooooooooooooooooooow. Main thread is dedicated for drawing window content only. The simplest method is shown in following text, but IAsyncresult can do the trick much better.

Thread t = new Thread
            (
                new ThreadStart(
                    delegate{
                        PrepareData(training_data, training_data_size, training_sets, neural_input);
                        ComputeNN(epochMax, hiddenLayerNeurons);
                    }
                )
            );

@VIeditorlover: Not a bad solution but that alone will not fix the problem.

What I think is happening is :
NNExportDLL must wait for ComputeNN to finish before it updates the GUI
This blocks the main thread from receiving the return value
which makes the GUI unresponsive.

If the calling app is running this function on its UI thread then this will make things even more unresponsive.

Some indication of what you have tried would be helpful.

Is this a windows forms application or some other flavour of C#?

It seems the DLL is written by you. Is this correct?

What is calling the DLL and how?

Is ComputeNN dependant upon the results of PrepareData?

Hey Nick,

Thank you for the reply! Yes I am using the System.Windows.Forms to construct the GUI, and the dll is written by me. An external financial application (Metatrader 4) is calling the dll, and yes ComputeNN is dependant on the previous called functions. They are dependend on each other in the sequence (PrepareData, ComputeNN, GUI).

@VIeditorlover: Not a bad solution but that alone will not fix the problem.

What I think is happening is :
NNExportDLL must wait for ComputeNN to finish before it updates the GUI
This blocks the main thread from receiving the return value
which makes the GUI unresponsive.

If the calling app is running this function on its UI thread then this will make things even more unresponsive.

Yes you are indeed correct. PrepareData() must first finish, then ComputeNN() and then the GUI() function must execute to update the GUI. Although this can happen very fast or very slow depending on the neural network typology used (this is a neural network dll).

Finally the neural network output value is returned to Metatrader where further calculations are done. I need the GUI to show me if the data sent from Metatrader is correct, and if the calculations are done correctly.

All you have to is to call all heavy-computing ops to different thread other things become incerdibly slooooooooooooooooooooooow. Main thread is dedicated for drawing window content only. The simplest method is shown in following text, but IAsyncresult can do the trick much better.

Thread t = new Thread
            (
                new ThreadStart(
                    delegate{
                        PrepareData(training_data, training_data_size, training_sets, neural_input);
                        ComputeNN(epochMax, hiddenLayerNeurons);
                    }
                )
            );

Thank you for the reply! I will try this code, I have tried something similar before with no success but I gave each function a separate thread maybe if using one thread for the two functions as you did will work.

UPDATE: What is happening is the GUI is executed before the functions in the delegate is finished. The functions need to finish execution before moving on with the code...

I suspect that Metadata is blocking waiting for your function to return before it can call it again.
It would be better to call the GUI method on a new thread as this allows you to exit the function early.
This will enable Metadata to call your function again while you update your GUI (you will need to keep track of each result set though).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.