private Thread updateUiThread; // Thread responsible for refreshing the UI (if skipping frames) #endregion Fields #region Constructors public CAClientWindow() { InitializeComponent(); defaultState = 0; generation = 0; toUpdate = new ConcurrentQueue<Cell[]>(); // Configure a client to connect to the simulation server simClient = new CAServiceClient(); foreach (OperationDescription op in simClient.Endpoint.Contract.Operations) { DataContractSerializerOperationBehavior dataContractBehavior = (DataContractSerializerOperationBehavior)op.Behaviors.Find<DataContractSerializerOperationBehavior>(); if (dataContractBehavior != null) dataContractBehavior.MaxItemsInObjectGraph = Int32.MaxValue; } // Attempt to open a connection to the server try {simClient.Open(); } catch (CommunicationException e) { MessageBox.Show("Error: could not connect to CA Server", "Error", MessageBoxButton.OK, MessageBoxImage.Warning); Application.Current.Shutdown(); } // Initialize grid display RenderOptions.SetBitmapScalingMode(gridImage, BitmapScalingMode.NearestNeighbor); gridSizeX = gridSizeY = 500; getDefaultColors(16); gridBMP = new GridBitmap(gridSizeX, gridSizeY, defaultState, colorList); gridImage.Source = gridBMP.Source; // Start grid-updating threads updateRate = maxUpdateRate; updateGridThread = new Thread(new ThreadStart(gridUpdater)); updateUiThread = new Thread(new ThreadStart(UIUpdater)); updateGridThread.Start(); updateUiThread.Start(); // Load a test CA /*ca = CASerializer.Deserialize("CA - Game of Life.xml"); if (simClient.initCA(ca)) { stepButton.IsEnabled = true; runButton.IsEnabled = true; saveCAMenuItem.IsEnabled = true; }*/ }
/// <summary> /// Initialize the grid display to a new bitmap of the specified size /// </summary> /// <param name="x">The width of the new grid</param> /// <param name="y">The height of the new grid</param> private void initDisplay(int x, int y) { lock (gridBMP) { gridBMP = new GridBitmap(x, y, defaultState, colorList); gridImage.Source = gridBMP.Source; gridBMP.refreshBMP(); } }