[C#]关于线程的问题 [英] [C#] Question about threading

查看:111
本文介绍了[C#]关于线程的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先没有代码问题,所以如果你是代码求解器或者忽略这个的东西,我想做点什么,但我不知道它是否有效,为了简单起见,我有一个行代码

first of all there is no code problem, so if you are code solver or something ignore this, i want to do something but i dont know if its efficient, to make it simple, i have one line code

this.dt = new DateTime.Now;



但是我想在线程中制作它,例如




but i want to make it in thread, for example

public void SetDateTime()
{
this.dt = DateTime.Now;
}





我想用线程打电话,





and i want to call it in thread,

Thread d = new Thread(SetDateTime());
d.Start();





i仍将使用此方法,仅当线程完成作业时,它会自动处理自己,因为我会在1000ms的计时器中使用它,这意味着在每一秒,ram中都会有线程保持内存,因此在完成工作后,线程会自行处理吗?有点奇怪的问题,但...



i will still use this method, only IF when the thread finishes the job, it automatically dispose itself, since i will use this in timer on 1000ms, which means on every second, there will be thread holding memory in ram, so after job done, will the thread dispose itself? kinda strange question but...

推荐答案

这个问题没有意义;并且它与线程没有真正的关系,如同制定的。



即使你的帖子只有一个,也没有什么你不能在线程中。 />
此外, dt 不能处置,但会被垃圾收集。垃圾收集和处理是不同的事情;阅读这些主题。



你打电话的线程并不重要。唯一的问题是对象的可达性。如果您对声明 dt 的类的对象保留一些引用,它将保留在内存中。请了解它的工作原理: https://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 [ ^ ]。



当然,如果您在线程之间共享任何对象,例如 dt ,则需要通过互斥来完成此操作机制,线程同步的方式之一

https ://en.wikipedia.org/wiki/Mutual_exclusion [ ^ ],

https://msdn.microsoft.com/en -us / library / ms173179.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx [<一个href =https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx\"target =_ blanktitle =New Window> ^ ]。



-SA
The question does not make sense; and it is not really related to threading, as formulated.

There is nothing you could do not "in thread", even if your thread is only one.
Also, dt cannot be "disposed", but it will be garbage-collected. Garbage collections and disposal are different things; read on these topics.

And it does not matter on what thread you do the call. The only issue is the reachability of objects. If you keep some reference to the object of the class declaring dt, it will be kept in memory. Please learn how it works: https://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

Of course, if you share any object, such as dt, between threads, you need to do this through mutual exclusion mechanism, one of the ways of thread synchronization:
https://en.wikipedia.org/wiki/Mutual_exclusion[^],
https://msdn.microsoft.com/en-us/library/ms173179.aspx[^],
https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx[^].

—SA


这篇关于[C#]关于线程的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆