如何在Quartz.net Scheduler中的Execute方法中调用方法

问题描述 投票:0回答:1

我是Quartz.Net的初学者。我试图从我的quartz.net调度作业excute方法中调用一个方法。任何人都可以提供帮助,如果这是正确的方法,或者有更好的方法可用?

UploadFile.cs


public bool UploadFiles(Tweet twts, HttpPostedFileBase imgFile)
        {

            string key = Utils.Twitterkey;
            string secret = Utils.Twittersecret;
            string token = Utils.Twittertoken; 
            string tokenSecret = Utils.TwittertokenSecret;

            string message = twts.tweets;

            string filePath; string imagePath = "";

            HttpPostedFileBase filebase =  new HttpPostedFileWrapper(HttpContext.Current.Request.Files["imgFile"]);

            if (imgFile == null)
            {
                imgFile = filebase;
            }

            if (imgFile.FileName != "")
            {

                filePath = HttpContext.Current.Server.MapPath("~/Images/");

                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                filePath = filePath + Path.GetFileName(imgFile.FileName);
                imgFile.SaveAs(filePath);
                imagePath = Path.Combine(HttpContext.Current.Server.MapPath(@"~/Images/"), filePath);
            }
            //Enter the Image Path if you want to upload image.

            var service = new TweetSharp.TwitterService(key, secret);
            service.AuthenticateWith(token, tokenSecret);

            //this Condition  will check weather you want to upload a image & text or only text 
            if (imagePath.Length > 0)
            {
                using (var stream = new FileStream(imagePath, FileMode.Open))
                {
                    var result = service.SendTweetWithMedia(new SendTweetWithMediaOptions
                    {
                        Status = message,
                        Images = new Dictionary<string, Stream> { { "sos", stream } }
                    });
                }
            }
            else // just message
            {
                var result = service.SendTweet(new SendTweetOptions
                {
                    Status = message
                });
            }

            return true;
        }

JobScheduler.cs

 public class JobScheduler<IDGJob>
        where IDGJob : Quartz.IJob
    {
        public static void Start()
        {

            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
            scheduler.Start();
            IJobDetail job = JobBuilder.Create<IDGJob>().Build();
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("IDGJob", "IDG")
                .WithCronSchedule("0 0 12 1/1 * ? *") 
                .StartAt(DateTime.UtcNow)
                .WithPriority(1)
                .Build();

            scheduler.ScheduleJob(job, trigger);

        }
    }

IDGJob.cs

 public class IDGJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {

           //Call the UploadFiles method here
        }
    }

如何在上述类的Execute方法中在此处调用UploadFiles方法。

c# asp.net-mvc quartz-scheduler
1个回答
0
投票

您的IDGJob类型应进行回调并在Execute()内部调用它>

public class IDGJob : IJob
{
    Action callback;

    public IDGJob(Action action)
    {
        if(action == null)
            throw new ArgumentNullException(nameof(action));

        callback = action;
    }

    public void Execute(IJobExecutionContext context)
    {
        callback();
    }
}

现在使用此类的代码在构造对象并使其执行所需的任何操作时都可以提供回调。

© www.soinside.com 2019 - 2024. All rights reserved.