// Threads functions private void ThreadsInitialization() { try { _sortersThreads = new Thread[_formModel.SelectedSorters.Length]; _threadParameters = new ThreadParameters[_formModel.SelectedSorters.Length]; for (int index = 0; index < _formModel.SelectedSorters.Length; index++) { _sortersThreads[index] = new Thread(new ParameterizedThreadStart(StartSorting)); _sortersThreads[index].IsBackground = true; _sortersThreads[index].Name = _formModel.SelectedSorters[index].ToString() + index.ToString(); _threadParameters[index] = new ThreadParameters( Utils.ConvertToOneDimensionalArray(_formModel.ArrayForSorting), (Sorter)_formModel.SelectedSorters[index]); } } catch (NullReferenceException) { if (_formModel.SelectedSorters != null && _formModel.ArrayForSorting != null) { throw; } } }
void ThreadFunction() { File.Copy("trie.dat", "out.tmp", true); //File.Copy("modified.csv", "modified.tmp", true); csvfile = File.CreateText("modified.tmp");// AppendText("modified.tmp"); ThreadReader = new BinaryReader(File.OpenRead("out.tmp")); ThreadReader.BaseStream.Position = 8; bw = new BinaryWriter(File.OpenWrite("out.dat")); bw.Write(totalWordCount); bw.Write((int)0); SavedAt = new Dictionary <int, int>(); word tmpWord = new word(this); tmpWord.grandChildren = new List <int>(); while (ThreadReader.BaseStream.Position < ThreadReader.BaseStream.Length) { tmpWord.Load(ThreadReader); if (!ThreadParameters.Contains(tmpWord.m_index)) { SavedAt[tmpWord.m_index] = (int)bw.BaseStream.Position; tmpWord.Save(bw, csvfile, false); } } int x = GetSavedIndex(BaseWord, bw, csvfile, true); csvfile.Close(); bw.BaseStream.Position = 4; bw.Write(x); bw.Close(); ThreadReader.Close(); ThreadParameters.Clear(); }
private void Core(object objectArgs) { ThreadParameters args = objectArgs as ThreadParameters; if (args == null) { throw new ArgumentNullException(); } int sliceWidth = this.Width / Environment.ProcessorCount; Bitmap slice = new Bitmap(sliceWidth, this.Height); System.Drawing.Graphics g = Graphics.FromImage(slice); VideoHelper v = new VideoHelper(this.InputPath); for (int i = args.StartPoint; i < args.EndPoint; i++) { Bitmap frame = v.GetFrameFromVideo(((double)i) / (double)this.TotalIterations); g.DrawImage(frame, (i - args.StartPoint) * this.BarWidth, 0, this.BarWidth, this.Height); if (i % 10 == 0) { //once every 10 iteration should not be too much //but is enough to keep the memory footprint as low as possible //(only cosmetic change ?) GC.Collect(); } completedIterations++; if (this.ProgressChanged != null) { this.ProgressChanged(this, new ProgressCHangedEventHandler(this.Progression)); } } v.Dispose(); ThreadedSlices.Add(args.ThreadId, slice); }
}//method private void DoDownloadResumeAsync(object threadParameters) { try { ThreadParameters p = threadParameters as ThreadParameters; this.DownloadResume(p.LocalDirectory, p.LocalFilename, p.RemoteDirectory, p.RemoteFilename); } catch { } }//method
public void DownloadResumeAsync(string localDirectory, string localFilename, string remoteDirectory, string remoteFileName) { ThreadParameters parameters = new ThreadParameters(localDirectory, localFilename, remoteDirectory, remoteFileName); ParameterizedThreadStart pThreadStart = new ParameterizedThreadStart(this.DoDownloadResumeAsync); _thread = new Thread(pThreadStart); _thread.Name = "DownloadThread"; _thread.IsBackground = true; _thread.Priority = ThreadPriority.Normal; _thread.Start(parameters); }//method
public void ThreadProc(object pThreadParameters) { ThreadParameters tp1 = (ThreadParameters)pThreadParameters; ApplyThread( tp1.Input, tp1.width, tp1.Height, tp1.Output, tp1.ThreadIndex); }
private void DoUploadResumeAsync(object threadParameters) { try { ThreadParameters p = threadParameters as ThreadParameters; this.UploadResume(p.LocalDirectory, p.LocalFilename, p.RemoteDirectory, p.RemoteFilename); } catch (Exception ex) { ClientHelper.TraceException("DoUploadResumeAsync", "断点续传失败", ex.Message); } }//method
static private void ShowDialogThread(Object param) { ThreadParameters dlgParam = (ThreadParameters)param; m_dlg = new MessageDlg(); m_dlg.Message = dlgParam.message; m_dlg.Title = dlgParam.title; m_dlg.ParentWindow = dlgParam.hParent; if (dlgParam.handler != null) { m_dlg.CancelPressed += dlgParam.handler; } Application.Run(m_dlg); }
} // method /// <summary> /// Method uploads file to ftpServer | method does not block calling thread /// | file exists on FtpServer => method overrides file /// | directory does not exist on FtpServer => directory is created /// </summary> /// <param name="localDirectory">directory on localhost</param> /// <param name="localFilename">filename on localhost</param> /// <param name="remoteDirectory">directory on ftpServer</param> /// <param name="remoteFileName">filename on ftpServer</param> public void UploadAsync(string localDirectory, string localFilename, string remoteDirectory, string remoteFileName) { try { ThreadParameters parameters = new ThreadParameters(localDirectory, localFilename, remoteDirectory, remoteFileName); ParameterizedThreadStart pThreadStart = new ParameterizedThreadStart(this.DoUploadAsync); _thread = new Thread(pThreadStart); _thread.Name = "UploadThread"; _thread.IsBackground = true; _thread.Priority = ThreadPriority.Normal; _thread.Start(parameters); } catch (Exception ex) { ClientHelper.TraceException("UploadAsync", "异步上传失败", ex.Message); } }// method
public Color[,] Apply(Color[,] input) { int width = input.GetLength(0); int height = input.GetLength(1); WorkAmount = width * height; Color[,] result = new Color[width, height]; Action <Color[, ], int, int, Color[, ], int> a = ApplyThread; List <Task> Tasks = new List <Task>(); for (int ThreadIndex = 1; ThreadIndex <= NumberOfThreads; ThreadIndex++) { ThreadParameters ThreadParameters1 = new ThreadParameters(input, width, height, result, ThreadIndex); ThreadPool.QueueUserWorkItem(ThreadProc, ThreadParameters1); } Thread.Sleep(100); return(result); }
/// <summary> /// Displays a message box in front of the specified object and with the specified text and caption. /// </summary> /// <param name="hParent">An implementation of IWin32Window that will parent the modelles dialog box.</param> /// <param name="message">The text to display in the message box.</param> /// <param name="title">The text to display in the title bar of the message box.</param> /// <param name="handler">"cancel button pressed" event handler</param> static public bool ShowMessageDlg(IWin32Window hParent, String message, String title, OnCancelHandler handler) { if (m_dlg != null) { return(false); } m_initEvent = new AutoResetEvent(false); ThreadParameters dlgParam = new ThreadParameters(); dlgParam.message = message; dlgParam.title = title; dlgParam.hParent = hParent.Handle; dlgParam.handler = handler; Thread thread = new Thread(ShowDialogThread); thread.IsBackground = true; thread.Start(dlgParam); m_initEvent.WaitOne(); m_initEvent.Close(); m_initEvent = null; return(true); }
private static void StartSorting(object obj) { ThreadParameters threadParameters = (ThreadParameters)obj; threadParameters.SortMethod.Sort(threadParameters.ArrayForSorting); }
private void crawlerToolStripMenuItem_Click(object sender, EventArgs e) { // Start Crawling. // time = new System.Threading.Timer(startcrawling()); //BackCrawler.GetCrawler d = new GetCrawler(); //d.startCrawling(); // foreach (ToolStripMenuItem item in startCrawlingToolStripMenuItem.DropDown.Items) // { // item.Text = (sender as ToolStripMenuItem).Text; if (IsCrawlProcessRunning == true) { SetProgress(false); IsCrawlProcessRunning = false; } SetProgress(true); IsCrawlProcessRunning = true; CrawlFirstPage crawldata = new CrawlFirstPage(); if ((sender as ToolStripMenuItem).Text == "League") { DataTable dt = crawldata.GetSports("2"); string link = dt.Rows[0]["link"].ToString(); int sportid = Convert.ToInt32(dt.Rows[0]["sportid"].ToString()); if (sportid == 2) { crawldata.CrawlLeagues(link); } } else if ((sender as ToolStripMenuItem).Text == "WorldMarket") { DataTable dt = crawldata.GetSports("2"); string link = dt.Rows[0]["link"].ToString(); int sportid = Convert.ToInt32(dt.Rows[0]["sportid"].ToString()); if (sportid == 2) { crawldata.CrawlWorldMarkets(); } } else if ((sender as ToolStripMenuItem).Text == "Soccer") { DataTable dt = crawldata.GetSports((sender as ToolStripMenuItem).Name.ToString()); string link = dt.Rows[0]["link"].ToString(); int sportid = Convert.ToInt32(dt.Rows[0]["sportid"].ToString()); // soocer // crawldata.CrawlLeagues(link); // crawldata.CrawlWorldMarkets(); DataSet ds = crawldata.GetLeague(); for (int j = 0; j < ds.Tables[0].Rows.Count; j++) { string matchlink = Convert.ToString(ds.Tables[0].Rows[j]["link"]); int sport_id = Convert.ToInt32(ds.Tables[0].Rows[j]["sportid"]); long leagueid = Convert.ToInt32(ds.Tables[0].Rows[j]["leagueid"]); ThreadParameters t = new ThreadParameters(); t.URL = matchlink; t.SportID = sport_id; t.LeagueID = leagueid; crawldata.CrawlMyPage(t); } } else if ((sender as ToolStripMenuItem).Text == "GaaFootball") { DataTable dt = crawldata.GetSports((sender as ToolStripMenuItem).Name.ToString()); string link = dt.Rows[0]["link"].ToString(); int sportid = Convert.ToInt32(dt.Rows[0]["sportid"].ToString()); // GAA Football ThreadParameters tp = new ThreadParameters(); tp.URL = link; tp.SportID = sportid; if (sportid == 12) { crawldata.CrawlMyPage(tp); } } else if ((sender as ToolStripMenuItem).Text == "GaaHurling") { DataTable dt = crawldata.GetSports((sender as ToolStripMenuItem).Name.ToString()); string link = dt.Rows[0]["link"].ToString(); int sportid = Convert.ToInt32(dt.Rows[0]["sportid"].ToString()); ThreadParameters tp = new ThreadParameters(); tp.URL = link; tp.SportID = sportid; // GAA Hurling. if (sportid == 28) { crawldata.CrawlMyPage(tp); } } else if ((sender as ToolStripMenuItem).Text == "Golf") { DataTable dt = crawldata.GetSports((sender as ToolStripMenuItem).Name.ToString()); string link = dt.Rows[0]["link"].ToString(); int sportid = Convert.ToInt32(dt.Rows[0]["sportid"].ToString()); // Golf if (sportid == 29) { crawldata.CrawlGolfTurnament(link, sportid.ToString()); } } else if ((sender as ToolStripMenuItem).Text == "GolfMarketName") { crawldata.CrawlGolfBettingMarketName(); } else if ((sender as ToolStripMenuItem).Text == "GolfMarkets") { crawldata.CrawlGolfBettingMarket(); } else if ((sender as ToolStripMenuItem).Text == "BettingMarket") { CrawlAllMarkets crawl = new CrawlAllMarkets(); crawl.CrawlBettingLinks(); } else if ((sender as ToolStripMenuItem).Text == "MarketResult") { crawldata.CrawlEachMatchResult(); } // Thread d = new Thread(); SetProgress(false); IsCrawlProcessRunning = false; // } }
public void GenerateMovieBarCode() { if (locked) { throw new InvalidOperationException(); } locked = true; completedIterations = 0; //multithread : on calcul séparement x bitmap qu'on réassemble à la fin //VideoHelper v = new VideoHelper(videoPath); try { Bitmap finalBitmap = new Bitmap(this.Width, this.Height); ThreadedSlices = new Dictionary <int, Bitmap>(); #if DEBUG DateTime start = DateTime.Now; #endif List <System.Threading.Thread> threads = new List <System.Threading.Thread>(); //distribute work load for (int i = 0; i < Environment.ProcessorCount; i++) { /* * 125 images * 3 threads: * 0-41 * 41-82 * 82-123 * remains : 123-125 */ ThreadParameters args = new ThreadParameters() { ThreadId = i, StartPoint = i * (this.TotalIterations / Environment.ProcessorCount), EndPoint = (i + 1) * (this.TotalIterations / Environment.ProcessorCount), }; System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Core)); t.Name = string.Format("Core {0}", i + 1); threads.Add(t); t.Start(args); } int remaining = this.TotalIterations % Environment.ProcessorCount; if (remaining > 0) { //start a last thread for the remaining frames ThreadParameters args = new ThreadParameters() { ThreadId = Environment.ProcessorCount, StartPoint = Environment.ProcessorCount * (this.TotalIterations / Environment.ProcessorCount), EndPoint = Environment.ProcessorCount * (this.TotalIterations / Environment.ProcessorCount) + remaining, }; System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Core)); t.Name = string.Format("Core {0}", Environment.ProcessorCount + 1); threads.Add(t); t.Start(args); } //wait for each thread to complete foreach (var item in threads) { item.Join(); } System.Drawing.Graphics g = Graphics.FromImage(finalBitmap); foreach (var slice in ThreadedSlices) { if (slice.Key == Environment.ProcessorCount) { //remaining g.DrawImage(slice.Value, new Point(Environment.ProcessorCount * (this.TotalIterations / Environment.ProcessorCount) + remaining, 0)); } else { g.DrawImage(slice.Value, new Point(slice.Key * (this.TotalIterations / Environment.ProcessorCount), 0)); } #if DEBUG slice.Value.Save(string.Format(@"C:\{0:000}.jpg", slice.Key)); #endif } #if DEBUG DateTime end = DateTime.Now; var total = end - start; Console.WriteLine(total); #endif System.Drawing.Imaging.ImageFormat format; switch (System.IO.Path.GetExtension(this.OutputPath).Trim(".".ToCharArray()).ToLowerInvariant()) { case "bmp": format = System.Drawing.Imaging.ImageFormat.Bmp; break; case "jpg": case "jpeg": format = System.Drawing.Imaging.ImageFormat.Jpeg; break; case "gif": format = System.Drawing.Imaging.ImageFormat.Gif; break; case "png": format = System.Drawing.Imaging.ImageFormat.Png; break; default: format = System.Drawing.Imaging.ImageFormat.Png; break; } finalBitmap.Save(this.OutputPath, format); if (GenerationComplete != null) { GenerationComplete(this, new EventArgs()); } } catch (Exception) { throw; } finally { locked = false; } }