Beispiel #1
0
        /// <summary>
        /// Handles messages sent back from the web worker
        /// </summary>
        /// <returns>Nothing.</returns>
        /// <param name="arg">The message sent from the web worker</param>
        private void HandleMessage(Worker.DataEvent arg)
        {
            // Deserialize the message sent from the worker
            var msg = (WorkerThreadManager.WebWorkerMessage)arg.Data;

            // Process the message type
            switch (msg.MsgType)
            {
            // Check for a thread start finished message
            case WorkerThreadManager.MessageTypeFinish:
                // Get the WebWorkerFinishMessage data from the message
                var finishMessage = (WorkerThreadManager.WebWorkerFinishMessage)msg.Data;
                // Get the thread start object that this message indicates just finished
                var threadStart = _queuedStarts[finishMessage.ThreadId];
                // Check if this thread start had a result handler
                if (threadStart.OnResult != null)
                {
                    // Yes, call the handler with this thread, the original parameter and the result from the message
                    threadStart.OnResult(this, threadStart.Param, finishMessage.Result);
                }
                else
                {
                    // No, set the internal result to the result from the message
                    Result = finishMessage.Result;
                }
                // Remove this finished thread start from the list of queued thread starts
                _queuedStarts.Remove(threadStart.ThreadId);
                break;

            // Check for a thread start exception message
            case WorkerThreadManager.MessageTypeException:
                // Get the WebWorkerExceptionMessage data from the message
                var exceptionMessage = (WorkerThreadManager.WebWorkerExceptionMessage)msg.Data;
                // Get the thread start object that this message indicates raised an exception
                threadStart = _queuedStarts[exceptionMessage.ThreadId];
                // Remove this finished thread start object from the list of queued threads
                _queuedStarts.Remove(threadStart.ThreadId);
                // Raise an execption indicating that this thread start raised an exception
                throw new Exception("Unhandled exception in thread (" + threadStart.ThreadId + ")");

            // Check for a thread script load exception (Raised while loading the scripts specified in the constructor)
            case WorkerThreadManager.MessageTypeScriptLoadException:
                // Script loading exceptions are unrecoverable and will kill the thread
                Dispose();
                // Raise an exception indicating the file that was loaded that caused the exception
                throw new Exception("There was an exception loading script file " + msg.Data + " while initialising a Web Worker");

            case WorkerThreadManager.MessageTypeMessage:
                if (!IsWebWorker)
                {
                    OnMessageStore?.Invoke(msg.Data);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Beispiel #2
0
        public static void WorkerSpawn_OnMessage(Worker.DataEvent dataEvent)
        {
            GenerateRequest request = JsonConvert.DeserializeObject <GenerateRequest>((string)dataEvent.Data);
            Mesh            m       = App.MakeMesh(request.Width, request.Height, request.Type, request.Difficulty);

            m.PrunedCountProgress += WorkerSpawn_OnPrunedCountProgress;
            counter = 0;
            m.Generate();

            StringBuilder builder = new StringBuilder();

            m.Save(builder);
            PostMessage(builder.ToString());
        }
Beispiel #3
0
        private static void AppWorker_OnMessage(Worker.DataEvent dataEvent)
        {
            string msg = (string)dataEvent.Data;

            if (msg.Length < 5)
            {
                progressBar.Value = int.Parse(msg);
            }
            else
            {
                Mesh current = display.Mesh;
                Mesh newMesh = new Mesh(0, 0, current.MeshType);
                newMesh.LoadFromText(msg.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries));
                display.Mesh = newMesh;
                progressBar.Remove();
                progressBar             = null;
                generateButton.Disabled = false;
                start = DateTime.UtcNow;
            }
        }
Beispiel #4
0
        /// <summary>
        /// Handles messages sent from the main thread
        /// </summary>
        /// <returns>Nothing.</returns>
        /// <param name="arg">The message sent from the main thread</param>
        private static void HandleMessage(Worker.DataEvent arg)
        {
            // Deserialise the message
            var msg = (WebWorkerMessage)arg.Data;

            // Process the message
            switch (msg.MsgType)
            {
            // Check if this is a message to load a script
            case MessageTypeLoadScripts:
                // The data is an array of strings representing the Uris of the scripts to load
                var scripts = msg.Data as string[];
                // Iterate over each script in order and load it in to the web worker
                if (scripts != null)
                {
                    foreach (var s in scripts)
                    {
                        // Try to import the script
                        try
                        {
                            // Import the script
                            ImportScript(s);
                        }
                        catch (Exception)
                        {
                            // An exception occurred trying to load the script
                            // Send a script load exception message back to the main thread
                            _worker.PostMessage(
                                // Create a new message
                                new WebWorkerMessage
                            {
                                // It's a script load exception message
                                MsgType = MessageTypeScriptLoadException,
                                // Set data to the Uri of the script that failed to load
                                Data = s
                            }
                                );
                        }
                    }
                }
                break;

            // Check if this is a thread start message
            case MessageTypeStart:
                // Cast the message data to a WebWorkerStartMessage
                var startData = (WebWorkerStartMessage)msg.Data;
                // Get the function pointer of the thread entry point to call
                var entryPointRef = GetObjectRefFromString(Script.Get <object>("window"), startData.ThreadEntryPoint);
                // Get the param from the message
                var param = startData.ThreadParam;

                // Set the thread id
                _threadId = startData.ThreadId;

                // Try to call the function
                try
                {
                    // Call the function with the parameter, and get the result
                    var result = Script.Write <dynamic>("entryPointRef(param)", entryPointRef, param);

                    // Send the result back to the main thread
                    _worker.PostMessage(
                        // Create a new WebWorkerMessage
                        new WebWorkerMessage
                    {
                        // The message is a finish message
                        MsgType = MessageTypeFinish,
                        // Set the data to a new WebWorkerFinishMessage
                        Data = new WebWorkerFinishMessage
                        {
                            // Set the thread id if this thread start function that just finished
                            ThreadId = startData.ThreadId,
                            // Set the result to the result of the thread start function
                            Result = result
                        }
                    }
                        );
                }
                catch (Exception)
                {
                    // An exception occurred running the thread start function
                    _worker.PostMessage(
                        // Create a new web worker message
                        new WebWorkerMessage
                    {
                        // The message is an exception message
                        MsgType = MessageTypeException,
                        // Set the data to a new WebWorkerExceptionMessage
                        Data = new WebWorkerExceptionMessage
                        {
                            // Set the thread id of the thread start function that raised the exception
                            ThreadId = startData.ThreadId,
                        }
                    }
                        );
                    // Continue raising the exception in this thread so it is printed to the console
                    throw;
                }
                break;

            case MessageTypeMessage:
                OnMessage?.Invoke(msg.Data);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }