protected IEnumerator <ActionInfo> ResolveEnumerator() { var threadParameters = new ThreadStartParams() { Args = this.args, ReturnValue = new Ref <TReturnData>(), Target = this.target }; var thread = this.ResolveThread(threadParameters); while (thread.IsAlive) { yield return(new ActionInfo(ActionInfoType.WaitForAsyncOperation, null)); } if (threadParameters.ThrownException != null) { yield return(new ActionInfo(ActionInfoType.Exception, threadParameters.ThrownException)); } else { this.returnValue.Value = threadParameters.ReturnValue.Value; yield return(new ActionInfo(ActionInfoType.Effect, this)); } }
private Thread ResolveThread(ThreadStartParams parameters) { var thread = new Thread(ThreadStart); thread.Start(parameters); return(thread); }
/// <summary> /// /// </summary> protected ThreadServiceBase() { var str = ""; if (ThreadStartParams != null) { str = "_" + ThreadStartParams.ToString(); } ServiceName = this.GetType().Name + str; Stoping += OnClosing; Started += OnStarted; }
void OpenMap() { using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.Multiselect = true; ofd.Filter = "GSS files (*.gss)|*.gss"; if (ofd.ShowDialog() == DialogResult.OK) { string file = ofd.FileName; string dir = System.IO.Path.GetDirectoryName(file); dir += "\\data"; string name = System.IO.Directory.GetParent(dir).Name; string desc_file = name + "_map_desc.xml"; string desc_file_path = dir + "\\" + desc_file; const string filename_tag = "_color_"; const string filename_ext = ".dds"; string filename_part = dir + "\\" + name + filename_tag; using (System.IO.FileStream fstream = new System.IO.FileStream(desc_file_path, System.IO.FileMode.Open)) { System.Xml.XmlDocument doc = new XmlDocument(); { doc.Load(fstream); XmlNode common_node = doc.FirstChild.NextSibling.FirstChild; XmlNode node_size_x = common_node.Attributes.GetNamedItem("map_size_x"); XmlNode node_size_z = common_node.Attributes.GetNamedItem("map_size_z"); int size_x = System.Convert.ToInt32(node_size_x.Value); int size_z = System.Convert.ToInt32(node_size_z.Value); ThreadStartParams thread_params = new ThreadStartParams(); thread_params.form = this; thread_params.files = new ArrayList(); grid.GridDimentions = new Size(size_x, size_z); for (int x = 0; x < size_x; x++) { for (int y = 0; y < size_z; y++) { string image_name = filename_part + x.ToString() + "_" + y.ToString() + filename_ext; FileInfo file_info = new FileInfo(); file_info.file_name = image_name; file_info.x = x; file_info.y = y; thread_params.files.Add(file_info); } } Thread thread = new Thread( new ParameterizedThreadStart(ImageGrid.Form1.DoImageLoadingWork)); thread.Start(thread_params); } } } } }
public static int Main(string[] args) { parseStatupArgs(args); Console.WriteLine("Run from command line with /? to view startup args!"); if (!Directory.Exists("Download")) { Directory.CreateDirectory("Download"); } else { current_download_directory_kb_size_ = (int)(calculateDirectoryFileSize(new DirectoryInfo("Download")) / 1024); } // each thread will process the full-depth of a single starting character // as we can force a start string, we need to find out where we are starting so each thread can process a unique string int start_character_index = 0; if (start_string_.Length > 0) { for (var i = 0; i < CONSTANTS.IMGUR_URL_CHARACTERS.Length; ++i) { if (start_string_[0] == CONSTANTS.IMGUR_URL_CHARACTERS[i]) { start_character_index = i; break; } } } // in order to easily support start strings, we must ensure there always is one if (start_string_.Length > num_search_characters_) { start_string_ = start_string_.Substring(0, num_search_characters_); } else { var num_add_chars = num_search_characters_ - start_string_.Length; for (var i = 0; i < num_add_chars; ++i) { start_string_ += CONSTANTS.DEFAULT_CHAR; } } // if there aren't enough characters left due to the start string, cap the number of threads to that var num_threads = Math.Min(CONSTANTS.NUM_THREADS, CONSTANTS.IMGUR_URL_CHARACTERS.Length - start_character_index); Console.WriteLine("Starting at " + start_string_ + " with " + num_threads + " threads! Enter 'help' to view console commands!"); var download_threads = new Thread[num_threads]; for (var i = 0; i < num_threads; ++i) { var thread_string = string.Concat(CONSTANTS.IMGUR_URL_CHARACTERS[start_character_index + i]); if (start_string_.Length > 0) { thread_string += start_string_.Substring(1); // skip first character, as this is taken into consideration by the start index } var thread_params = new ThreadStartParams(thread_string, num_threads); download_threads[i] = new Thread(threadDownload); download_threads[i].Start(thread_params); } while (true) { // Console.ReadLine() will block until a command is entered var command = Console.ReadLine(); switch (command) { case "pause": { if (downloading_paused_ == 1) { break; } Interlocked.Exchange(ref downloading_paused_, 1); Console.WriteLine("PAUSED! Enter 'resume' to continue downloading!"); break; } case "resume": { if (downloading_paused_ == 0) { break; } current_download_directory_kb_size_ = (int)(calculateDirectoryFileSize(new DirectoryInfo("Download")) / 1024); Interlocked.Exchange(ref downloading_paused_, 0); Console.WriteLine("RESUMED!"); break; } default: { Console.WriteLine("pause - Pauses the application"); Console.WriteLine("resume - Resumes the application"); break; } } } }
void imageThreadWorker(int taskId, object o) { ThreadStartParams threadStartParams = (ThreadStartParams)o; if (_cnt != threadStartParams.CNT) { return; } List <byte> bufferImage = new List <byte>(); byte[] bufferTmp = new byte[BUFFER_SIZE]; try { _isRunning = true; using (var tcpClient = new TcpClient()) { IAsyncResult ar = tcpClient.BeginConnect(threadStartParams.IPAddress, threadStartParams.Port, null, null); if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(3), false)) { throw new TimeoutException(); } tcpClient.EndConnect(ar); tcpClient.ReceiveBufferSize = BUFFER_SIZE; tcpClient.ReceiveTimeout = 5000; tcpClient.SendTimeout = 3000; if (_cnt != threadStartParams.CNT) { return; } if (OnStart != null) { OnStart(); } using (NetworkStream ns = tcpClient.GetStream()) while (_cnt == threadStartParams.CNT && tcpClient.Connected) { if (_sendUpdate) { _sendUpdate = false; ns.Write(_positions, 0, _positions.Length); } int read = ns.Read(bufferTmp, 0, BUFFER_SIZE); if (read == 0) { throw new Exception("Client disconnected"); } bufferImage.AddRange(bufferTmp.Take(read)); LOOP_AGAIN: int foundStart = -1; if (bufferImage.Count < TAG_EZIMAGE.Length) { continue; } for (int p = 0; p < bufferImage.Count - TAG_EZIMAGE.Length; p++) { if (bufferImage[p] == TAG_EZIMAGE[0] && bufferImage[p + 1] == TAG_EZIMAGE[1] && bufferImage[p + 2] == TAG_EZIMAGE[2] && bufferImage[p + 3] == TAG_EZIMAGE[3] && bufferImage[p + 4] == TAG_EZIMAGE[4]) { foundStart = p; break; } } if (foundStart == -1) { continue; } if (foundStart > 0) { bufferImage.RemoveRange(0, foundStart); } if (bufferImage.Count < TAG_EZIMAGE.Length + sizeof(UInt32)) { continue; } int imageSize = (int)BitConverter.ToUInt32(bufferImage.GetRange(TAG_EZIMAGE.Length, sizeof(UInt32)).ToArray(), 0); if (bufferImage.Count <= imageSize + TAG_EZIMAGE.Length + sizeof(UInt32)) { continue; } bufferImage.RemoveRange(0, TAG_EZIMAGE.Length + sizeof(UInt32)); _imageSize = imageSize; try { if (OnImageDataReady != null) { OnImageDataReady(bufferImage.GetRange(0, imageSize).ToArray()); } } catch (Exception ex) { if (OnLog != null) { OnLog(DateTime.Now, string.Format("ezbv4 camera image render error: {0}", ex)); } } bufferImage.RemoveRange(0, imageSize); // If there's at least 5kb of data in the buffer, loop again and see if there's another image in the buffer // Without doing this, the there's a chance the buffer will fill with future images and we'll never catch up because image data is only processed when an image is available if (bufferImage.Count > 5000) { goto LOOP_AGAIN; } } } } catch (Exception ex) { if (OnLog != null) { OnLog(DateTime.Now, string.Format("EZ-B v4 Camera Error: {0}", ex)); } } finally { _isRunning = false; if (OnStop != null) { OnStop(); } } }