or maybe how should approach if u have any information just post it here . how to deal with corss heading variable ex. any information u have could be useful :)
|
C#
how to implement multitheading in c# ? |
|
Jan 5, 2021 05:03 AM
(This post was last modified: Jan 5, 2021 05:04 AM by xHeaven.)
A very basic approach would be something like this.
PHP Code
Can you explain what you are trying to achieve? It almost sounds like you are interested in synchronized access to specific resources from multiple threads? Doing things like accessing a static dictionary from multiple threads can have dire consequences if you aren't synchronizing the writes to that dictionary across threads.
This is very common in web application where different requests to the same application is actually executing on different threads with no explicit code written to access the information from separate threads.
What do you mean by corss heading?
You mean CORS (Cross-Origin Resource Sharing), because if that's the cause, I don't see any correlation with your "current multi-threading" problem. I might be wrong so correction is very welcome. Mind sharing your ideas or code if possible so we can understand your problem. Regards, Rmdy
in .NET, you have maybe 6 ways to spawn a thread. Despite that, it all depends on your use-case in terms of code design.
Some cases might need a BackgroundWorker, some could just simply use the Thread class and of course the new TPL which abstracts a thread with useful features. Back in the early days of .NET, you had BeginInvoke in 1.1 until 2.0 came out with other features.
Spawning a new thread could certainly be a solution, but its not a very good one if you do a lot of async stuff because creating a new thread is fairly expensive.
You should probably use async await, it is easy to use and efficient. It uses a thread pool behind the scenes, so you are not spawning a new thread for every task. https://docs.microsoft.com/en-us/dotnet/...pts/async/
Jan 6, 2021 05:56 PM
(This post was last modified: Jan 6, 2021 05:57 PM by StarZ.)
(Jan 5, 2021 07:42 AM)Rmdy Wrote: What do you mean by corss heading? Probably meant cross-thread referencing of objects. An example would be trying to access UI elements from a thread other than the UI thread. In which case, I'd lean towards using Events. fuck. ![]() Not trying to be a dick, It just comes naturally.
I thinkt that Tasks are much more relevant when it comes to parallel programming than threads.
-Tasks do not create own their own OS thread, but are executed by a task scheduler -Tasks allow you to find out when they have finished -can be used in sync/async programming i.e. continue with Y, when X is finished In conclusion tasks are much more efficient and have a stronger API in comparison to threads.
Feb 13, 2021 10:44 PM
(This post was last modified: Feb 13, 2021 10:47 PM by uint32.)
you should much rather use tpl pattern (async/await)
here's an example: Code public void Main()this snippet will finish executing after 5000ms, and not 6000ms if the code was synchronous there are many advantages to using tasks over threads: - tasks are managed by the threadpool which avoids the slow process creating/destroying threads which is also inefficient at the os level - code ends up being much cleaner and functional references: task class: https://docs.microsoft.com/en-us/dotnet/...ew=net-5.0 async/await: https://docs.microsoft.com/en-us/dotnet/...pts/async/ |
|
|



![[Image: B37mjKj.png]](https://i.imgur.com/B37mjKj.png)

![[Image: ZjBA6fR.gif]](https://i.imgur.com/ZjBA6fR.gif)



![[Image: wyEbCMi.png]](https://i.imgur.com/wyEbCMi.png)
![[Image: 1gIrPt9.png]](https://i.imgur.com/1gIrPt9.png)