/// <summary> /// Prepares the content part. /// </summary> /// <param name="thread">The thread.</param> /// <param name="since">The since.</param> /// <param name="threadView">if set to <c>true</c> show messages as thread view.</param> /// <returns></returns> private string PrepareContentPart(ThreadDetails thread, DateTimeOffset?since, bool threadView = false) { var mBody = new StringBuilder(); if (threadView) { if (thread.IsFull) { mBody.AppendLine(MessageDetails(thread.Messages.First(), true, threadView)); } else { if (since.HasValue) { mBody.AppendLine(ConversationWarning(since.Value)); } mBody.AppendLine(MessageDetails(thread.Messages.First(), false, threadView)); } thread.Messages.Skip(1).ToList().ForEach(c => mBody.AppendLine(MessageDetails(c, false, threadView))); } else { if (!thread.IsFull && since.HasValue) { mBody.AppendLine(ConversationWarning(since.Value)); } thread.Messages.ForEach(c => mBody.AppendLine(MessageDetails(c, false, threadView))); } return(mBody.ToString()); }
private void DoAccessCheck(IEnumerable <TokenEntry> tokens, ProcessDetails proc_details, NtThread thread, AccessMask access_rights) { var sd = thread.GetSecurityDescriptor(SecurityInformation.AllBasic, false); if (sd.IsSuccess) { foreach (TokenEntry token in tokens) { CheckAccess(token, proc_details, ThreadDetails.FromThread(thread), _thread_type, access_rights, sd.Result); } } else { // Try and open process when under impersonation. foreach (TokenEntry token in tokens) { using (var new_thread = token.Token.RunUnderImpersonate(() => NtThread.Open(thread.ThreadId, ThreadAccessRights.MaximumAllowed, false))) { if (new_thread.IsSuccess && IsAccessGranted(new_thread.Result.GrantedAccessMask, access_rights)) { WriteAccessCheckResult(proc_details, ThreadDetails.FromThread(thread), new_thread.Result.GrantedAccessMask, _thread_type.GenericMapping, String.Empty, token.Information); } } } } }
public static void Scheduler(RunDetails Details) { int Parallelism = Environment.ProcessorCount; ThreadDetails[] thDetails = new ThreadDetails[Parallelism]; /* Compute block sizes. Make sure block sizes are multiples of Ystep (except for last block). */ int StepSize = (Details.DataHeight + Parallelism - 1) / Parallelism; if (Details.Ystep != 0) { StepSize = (StepSize + Details.Ystep - 1) / Details.Ystep * Details.Ystep; } /* Update blocks */ for (int i = 0; i < Parallelism; i++) { thDetails[i] = new ThreadDetails() { CurrentPositionX = 0, CurrentPositionY = i * StepSize, StartPosition = i * StepSize, EndPosition = (i + 1) * StepSize } } ; for (int i = 0; i < Parallelism; i++) { if (thDetails[i].EndPosition > Details.DataHeight) { thDetails[i].EndPosition = Details.DataHeight; } } /* Run in parallel */ Parallel.For(0, Parallelism, (i) => ProcessBlock(Details, ref thDetails[i])); }
static void LockDataNofill(RunDetails RD, ThreadDetails TD, Image Image, ref ImageData Data, bool Readonly) { bool NotInitialized = TD.CurrentPositionY == TD.StartPosition && TD.CurrentPositionX == 0; /* Compute required height and width */ int NWidth = RD.Xstep; int NHeight = RD.Ystep; if (TD.CurrentPositionX + RD.Xstep > Image.Width) { NWidth = (int)Image.Width - TD.CurrentPositionX; } if (TD.CurrentPositionY + RD.Ystep > TD.EndPosition) { NHeight = TD.EndPosition - TD.CurrentPositionY; } /* If window size must be changed */ if (NWidth != Data.Position.Width || NHeight != Data.Position.Height || NotInitialized) { if (!NotInitialized) { Image.ExitLock(Data); } Data = Image.LockData(new System.Drawing.Rectangle(TD.CurrentPositionX, TD.CurrentPositionY, NWidth, NHeight), false, Readonly); } /* Just swap otherwise */ else { Data = Image.SwitchLockData(Data, TD.CurrentPositionX, TD.CurrentPositionY, false, Readonly); } }
// START WORK public void Main() { // Create MaxThreads number of Thread managed details for (int i = 0; i < MaxThreads; i++) { ThreadDetails T = new ThreadDetails(); T.ISRUNNING = false; T.ID = i; TD.Add(T); } // Start the Pool // ThreadPool.QueueUserWorkItem has better performance than Thread obj = new Thread(Process) method // Search internet for more info on this. ProcessPool(); // Wait until all work is done before quiting // Check every 1 sec, if repeat is on, repeat the process for non-running works do { if (RepeatProcess == true) { ProcessPool(); } Thread.Sleep(1000); } while (ThreadsAreRunning() == true); }
private void CheckAccess(TokenEntry token, ProcessDetails process, ThreadDetails thread, NtType type, AccessMask access_rights, SecurityDescriptor sd) { AccessMask granted_access = NtSecurity.GetMaximumAccess(sd, token.Token, type.GenericMapping); if (IsAccessGranted(granted_access, access_rights)) { WriteAccessCheckResult(process, thread, granted_access, type.GenericMapping, sd.ToSddl(), token.Information); } }
private void Update() { if (threadDetailsCollection.Count > 0) { for (int index = 0; index < threadDetailsCollection.Count; index++) { ThreadDetails threadDetails = threadDetailsCollection.Dequeue(); threadDetails.callback(threadDetails.variable); } } }
private void WriteAccessCheckResult(ProcessDetails process, ThreadDetails thread, AccessMask granted_access, GenericMapping generic_mapping, string sddl, TokenInformation token) { if (thread == null) { WriteObject(new ProcessAccessCheckResult(process.Name, process.ImagePath, process.ProcessId, process.SessionId, process.CommandLine, granted_access, _process_type, sddl, token)); } else { WriteObject(new ThreadAccessCheckResult(process.Name, process.ImagePath, thread.ThreadId, thread.Description, process.ProcessId, process.SessionId, process.CommandLine, granted_access, _process_type, sddl, token)); } }
// DO WORK AND END private static void ProcessWork(object callback) { // Pass details to Processing Function ThreadDetails T = (ThreadDetails)callback; // Get some random Sleeptime to simulate work beging done, like processing files, connecting to DBs and quering data, creating/copying files. Random RND = new Random(); int SleepTime = (1000 * RND.Next(1, 20)); // Show the work start and end Console.WriteLine("ID:" + T.ID + ",[" + TD[T.ID].RUNCOUNT + "] START, SLEEPTIME:" + SleepTime); Thread.Sleep(SleepTime); //<--- This is to simulate actual work being done. Console.WriteLine("ID:" + T.ID + ",[" + TD[T.ID].RUNCOUNT + "] END"); TD[T.ID].RUNCOUNT++; TD[T.ID].ISRUNNING = false; }
/// <summary> /// Process a block of data. /// </summary> /// <param name="RunDetails">Thread-common run parameters.</param> /// <param name="ThDetails">Thread-specific run parameters.</param> static void ProcessBlock(RunDetails RunDetails, ref ThreadDetails ThDetails) { /* Initialized inputs and outputs */ ImageData[] Dataset = new ImageData[RunDetails.InputImages.Length]; ImageData OutputData = default(ImageData); /* Check for cases where split produces no data for current CPU. */ if (ThDetails.CurrentPositionY > ThDetails.EndPosition) { return; } /* While there is data to process */ while (ThDetails.CurrentPositionY < ThDetails.EndPosition) { /* Read data and initialize output */ for (int i = 0; i < Dataset.Length; i++) { ReadImageBlock(RunDetails, RunDetails.InputImages[i], ref Dataset[i], ref ThDetails); } if (RunDetails.OutputImage != null) { ProcessOutput(RunDetails, ThDetails, ref OutputData); } CallAlgorithm(RunDetails, Dataset, OutputData); ThDetails.CurrentPositionX += RunDetails.Xstep; if (ThDetails.CurrentPositionX >= RunDetails.DataWidth) { ThDetails.CurrentPositionX = 0; ThDetails.CurrentPositionY += RunDetails.Ystep; } } /* Release inputs and outputs */ for (int i = 0; i < Dataset.Length; i++) { RunDetails.InputImages[i].ExitLock(Dataset[i]); } if (RunDetails.OutputImage != null) { RunDetails.OutputImage.ExitLock(OutputData); } }
/// <summary> /// Initializes and writes data to output image. /// </summary> public static void ProcessOutput(RunDetails RunDetails, ThreadDetails ThDetails, ref ImageData OutputData) { LockDataNofill(RunDetails, ThDetails, RunDetails.OutputImage, ref OutputData, false); }
/// <summary> /// Reads a block of data from the input images. /// </summary> public static void ReadImageBlock(RunDetails RD, Image Selected, ref ImageData Data, ref ThreadDetails TD) { if (!RD.FillZero) { LockDataNofill(RD, TD, Selected, ref Data, true); return; } if (TD.CurrentPositionY == TD.StartPosition && TD.CurrentPositionX == 0) { Data = Selected.LockData(new System.Drawing.Rectangle( TD.CurrentPositionX - RD.InputMargins, TD.CurrentPositionY - RD.InputMargins, RD.Xstep + 2 * RD.InputMargins, RD.Ystep + 2 * RD.InputMargins), RD.FillZero); } else { Data = Selected.SwitchLockData(Data, TD.CurrentPositionX - RD.InputMargins, TD.CurrentPositionY - RD.InputMargins, RD.FillZero); } }
/// <summary> /// Processes the messages. Grouping by threads/Ordering by datetime. /// </summary> /// <param name="details">The details.</param> /// <returns></returns> public static List <ThreadDetails> ProcessMessages(IEnumerable <IMessageDetails> details) { var threads = new List <ThreadDetails>(); // Threads Initial/First Messages var roots = details.Where(c => c.ReplyToId == null).ToList(); roots.ForEach(c => threads.Add(new ThreadDetails() { Messages = new List <IMessageDetails>() { c }, IsFull = string.IsNullOrEmpty(c.ReplyToId), LastMessageCreationTime = c.CreatedDateTime, })); // Related messages which has root message var children = details.Where(c => details.Where(x => x.Id == c.ReplyToId).Any()).ToList(); if (children.Any()) { foreach (var thread in threads) { // Get all relevant child messages for current thread var results = children.Where(c => c.ReplyToId == thread.Messages.First().Id).ToList(); foreach (var m in results) { thread.Messages.Add(m); children.Remove(m); } // Order by created date thread.Messages = thread.Messages.OrderBy(c => c.CreatedDateTime).ToList(); thread.LastMessageCreationTime = thread.Messages.Max(c => c.CreatedDateTime); } } // Child messages which doesn't have root message in current collection var childrenWithoutRoot = details.Where(c => c.ReplyToId != null && !details.Where(x => x.Id == c.ReplyToId).Any()).ToList(); if (childrenWithoutRoot.Any()) { // group messages by ID of root message var groups = childrenWithoutRoot.GroupBy(c => c.ReplyToId); foreach (var group in groups) { var thread = new ThreadDetails() { Messages = group.OrderBy(c => c.CreatedDateTime).ToList(), IsFull = false, LastMessageCreationTime = group.Max(c => c.CreatedDateTime), }; threads.Add(thread); } } threads = threads.OrderBy(c => c.LastMessageCreationTime).ToList(); return(threads); }