سؤال

I am using following code to initiate Jobs.

 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);
                    }
                }

How I can access Job class from thread later in some other class?

What I mean is that we create Thread like this

 var thread = new Thread(new ThreadStart(instanceJob.ExecuteJob))
                                {
                                    IsBackground = true,
                                    Name = "SID" + index
                                };

So can I use thread somehow to access instanceJob?

Thanks!

هل كانت مفيدة؟

المحلول

Threads aren't really associated with any particular class, and its ThreadStart is not exposed, so given a thread, there isn't really a class context to be extracted.

Instead, what it looks like is that you need to create some dictionary with Thread as key, and your job as value. Then, given a Thread instance you can query the dictionary for the associated job instance.

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);
                }
            }

in other places where you need to get job for a thread do:

job = threads[myThreadInstance]; // Where myThreadInstance is an instance of Thread class...

نصائح أخرى

I would recommend using .NET 4's Task<T> to hold this, instead of using Thread. You can then directly return the Job within the Task<Job>:

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();        
}

This will give you a list of Tasks that will be completed once the job's ExecuteJob method is done, and the Task.Result will be the job that executed.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top