-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Update scheduling-threads.md added an example code #40739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a56d525
Update scheduling-threads.md added an example code
victoravt 7349855
Update docs/standard/threading/scheduling-threads.md
victoravt 0cccde6
Update code snippet scheduling-threads.md
victoravt 8cb60f5
Code snippet moved to a sepparate file (project)
victoravt b93b6ba
Path fix
victoravt 67b8739
typo fix
victoravt c999ff5
Removed blank spaces
victoravt 6d1b25e
Fixed typo and rephrased couple of sentences.
victoravt 5c0e3e3
Build fix
victoravt 9c2cb4e
Merge branch 'patch-1' of https://github.com/victoravt/docs into patch-1
victoravt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
docs/standard/threading/snippets/scheduling-threads/SchedulingThreads.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| namespace snippets; | ||
|
|
||
| public class SchedulingThreads | ||
| { | ||
| public void RunMultipleThreadsOnDifferentPriorities() | ||
| { | ||
| var threadsList = new List<Thread>(9); | ||
|
|
||
| // Initialize 9 threads. 5 with Highest priority, and the first 4 from Lowest to Normal range. | ||
| for (int i = 0; i < 9; i++) | ||
| { | ||
| var thread = new Thread(() => { new ThreadWithCallback(Callback).Process(); }); | ||
|
|
||
| if (i > 3) | ||
| thread.Priority = ThreadPriority.Highest; | ||
| else | ||
| thread.Priority = (ThreadPriority)i; | ||
|
|
||
| threadsList.Add(thread); | ||
| } | ||
|
|
||
| threadsList.ForEach(thread => thread.Start()); | ||
| } | ||
|
|
||
| public void Callback(ThreadPriority threadPriority) | ||
| { | ||
| Console.WriteLine($"Callback in {threadPriority} priority. \t\t ThreadId: {Thread.CurrentThread.ManagedThreadId}."); | ||
| } | ||
|
|
||
| public class ThreadWithCallback | ||
| { | ||
| public ThreadWithCallback(Action<ThreadPriority> callback) | ||
| { | ||
| this.callback = callback; | ||
| } | ||
|
|
||
| public Action<ThreadPriority> callback; | ||
|
|
||
| public void Process() | ||
| { | ||
| Console.WriteLine($"Entered process in {Thread.CurrentThread.Priority} priority. \t\t ThreadId: {Thread.CurrentThread.ManagedThreadId}."); | ||
| Thread.Sleep(1000); | ||
| Console.WriteLine($"Finished process in {Thread.CurrentThread.Priority} priority. \t\t ThreadId: {Thread.CurrentThread.ManagedThreadId}."); | ||
|
|
||
| if (callback != null) | ||
| { | ||
| callback(Thread.CurrentThread.Priority); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // The example displays the output like the following: | ||
| // Entered process in Highest priority. ThreadId: 9. | ||
| // Entered process in Highest priority. ThreadId: 12. | ||
| // Entered process in Normal priority. ThreadId: 6. | ||
| // Entered process in BelowNormal priority. ThreadId: 5. | ||
| // Entered process in Lowest priority. ThreadId: 4. | ||
| // Entered process in AboveNormal priority. ThreadId: 7. | ||
| // Entered process in Highest priority. ThreadId: 11. | ||
| // Entered process in Highest priority. ThreadId: 10. | ||
| // Entered process in Highest priority. ThreadId: 8. | ||
| // Finished process in Highest priority. ThreadId: 9. | ||
| // Finished process in Highest priority. ThreadId: 12. | ||
| // Finished process in Highest priority. ThreadId: 8. | ||
| // Finished process in Highest priority. ThreadId: 10. | ||
| // Callback in Highest priority. ThreadId: 10. | ||
| // Finished process in AboveNormal priority. ThreadId: 7. | ||
| // Callback in AboveNormal priority. ThreadId: 7. | ||
| // Finished process in Lowest priority. ThreadId: 4. | ||
| // Callback in Lowest priority. ThreadId: 4. | ||
| // Finished process in Normal priority. ThreadId: 6. | ||
| // Callback in Highest priority. ThreadId: 9. | ||
| // Callback in Highest priority. ThreadId: 8. | ||
| // Callback in Highest priority. ThreadId: 12. | ||
| // Finished process in Highest priority. ThreadId: 11. | ||
| // Callback in Highest priority. ThreadId: 11. | ||
| // Callback in Normal priority. ThreadId: 6. | ||
| // Finished process in BelowNormal priority. ThreadId: 5. | ||
| // Callback in BelowNormal priority. ThreadId: 5. | ||
| } |
10 changes: 10 additions & 0 deletions
10
docs/standard/threading/snippets/scheduling-threads/ThreadSnippets.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.