C# 如何从线程获取类?
我正在使用以下代码启动作业C# 如何从线程获取类?,c#,multithreading,reflection,C#,Multithreading,Reflection,我正在使用以下代码启动作业 List<Thread> threads = new List<Thread>(); List<Job> foundedJobs = new List<Job>(); public void StartAllJobs() { try { if
List<Thread> threads = new List<Thread>();
List<Job> foundedJobs = new List<Job>();
public void StartAllJobs()
{
try
{
if (CanExecuteJobs == false) return;
var jobs = GetAllTypesImplementingInterface(typeof(Job)); // get all job implementations of this assembly.
var enumerable = jobs as Type[] ?? jobs.ToArray();
if (jobs != null && enumerable.Any()) // execute each job
{
Job instanceJob = null;
var index = 0;
foreach (var job in enumerable)
{
if (IsRealClass(job)) // only instantiate the job its implementation is "real"
{
try
{
instanceJob = (Job)Activator.CreateInstance(job); // instantiate job by reflection
foundedJobs.Add(instanceJob);
var thread = new Thread(new ThreadStart(instanceJob.ExecuteJob))
{
IsBackground = true,
Name = "SID" + index
}; // create thread for this job execution method
thread.Start();// start thread executing the job
threads.Add(thread);
index++;
}
catch (Exception ex)
{
App.Logger.Error(ex);
}
}
}
}
}
catch (Exception ex)
{
App.Logger.Error(ex);
}
}
因此,我可以使用线程
以某种方式访问实例作业
谢谢 线程实际上与任何特定的类都没有关联,而且它的
ThreadStart
也没有公开,因此给定一个线程,实际上就没有要提取的类上下文
相反,您需要创建一些字典,其中线程
作为键,而作业
作为值。然后,给定一个线程
实例,您可以在字典中查询关联的作业
实例
Dictionary<Thread, Job> threads = new Dictionary<Thread, Job>();
List<Job> foundedJobs = new List<Job>();
public void StartAllJobs()
{
try
{
if (CanExecuteJobs == false) return;
var jobs = GetAllTypesImplementingInterface(typeof(Job)); // get all job implementations of this assembly.
var enumerable = jobs as Type[] ?? jobs.ToArray();
if (jobs != null && enumerable.Any()) // execute each job
{
Job instanceJob = null;
var index = 0;
foreach (var job in enumerable)
{
if (IsRealClass(job)) // only instantiate the job its implementation is "real"
{
try
{
instanceJob = (Job)Activator.CreateInstance(job); // instantiate job by reflection
foundedJobs.Add(instanceJob);
var thread = new Thread(new ThreadStart(instanceJob.ExecuteJob))
{
IsBackground = true,
Name = "SID" + index
}; // create thread for this job execution method
thread.Start();// start thread executing the job
threads.Add(thread, job);
index++;
}
catch (Exception ex)
{
App.Logger.Error(ex);
}
}
}
}
}
catch (Exception ex)
{
App.Logger.Error(ex);
}
}
我建议使用.NET4的
任务
来保存这个,而不是使用线程。然后,您可以直接返回任务中的作业
:
public List StartAllJobs()
{
if(CanExecuteJobs==false)返回;
var jobs=getAllTypeSimplementInterface(typeof(Job));//获取此程序集的所有作业实现。
返回作业。其中(IsRealClass)
.Select(作业=>(作业)激活器.CreateInstance(作业))
.Select(作业=>Task.Factory.StartNew(()=>{job.ExecuteJob();返回作业;}))
.ToList();
}
这将为您提供一个任务列表,一旦作业的ExecuteJob
方法完成,Task.Result
将是执行的作业。您将能够从instanceJob访问线程。这不是你的意思吗?@Crono Do you我可以List foundedJobs=new List()代码>获取工作任务,对吗?使用List threads=new List()就足够了吗代码>我已经在代码中了?@ClarkKent否,列表不是关联容器。它所能告诉你的只是一个特定的线程是否在那里(或者你放在那里的所有线程)。它没有一种将线程与其他实体相关联的方法。看起来你的想法就是我需要的。你能提供一些例子吗?我完全同意你的方法,但在这种情况下,有很多事情需要改变。无论如何+1,谢谢你!
Dictionary<Thread, Job> threads = new Dictionary<Thread, Job>();
List<Job> foundedJobs = new List<Job>();
public void StartAllJobs()
{
try
{
if (CanExecuteJobs == false) return;
var jobs = GetAllTypesImplementingInterface(typeof(Job)); // get all job implementations of this assembly.
var enumerable = jobs as Type[] ?? jobs.ToArray();
if (jobs != null && enumerable.Any()) // execute each job
{
Job instanceJob = null;
var index = 0;
foreach (var job in enumerable)
{
if (IsRealClass(job)) // only instantiate the job its implementation is "real"
{
try
{
instanceJob = (Job)Activator.CreateInstance(job); // instantiate job by reflection
foundedJobs.Add(instanceJob);
var thread = new Thread(new ThreadStart(instanceJob.ExecuteJob))
{
IsBackground = true,
Name = "SID" + index
}; // create thread for this job execution method
thread.Start();// start thread executing the job
threads.Add(thread, job);
index++;
}
catch (Exception ex)
{
App.Logger.Error(ex);
}
}
}
}
}
catch (Exception ex)
{
App.Logger.Error(ex);
}
}
job = threads[myThreadInstance]; // Where myThreadInstance is an instance of Thread class...
public List<Task<Job>> StartAllJobs()
{
if (CanExecuteJobs == false) return;
var jobs = GetAllTypesImplementingInterface(typeof(Job)); // get all job implementations of this assembly.
return jobs.Where(IsRealClass)
.Select(job => (Job)Activator.CreateInstance(job))
.Select(job => Task.Factory.StartNew(() => { job.ExecuteJob(); return job; }))
.ToList();
}