如何使用 Quartz.NET 毫无问题地实例化一个新的 Windows 窗体?

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

我正在使用 Quartz.NET 库在我的 C# 应用程序中创建作业。

我的数据库中有一些寄存器,所以我有一个表,其中包含一个名为“start_date”的列。该作业每 50 秒运行一次,因此我将“start_date”列中的日期与我计算机的日期进行比较,如果日期相等,我想实例化一个带有消息和按钮的新 Windows 窗体。

此刻,新的Windows窗体正在正确打开,但没有显示消息,窗口停止响应。

基本上,在我的代码中我有这样的东西:

FormMessage.cs

public partial class FormMessage : Form
{
    public FormMessage()
    {
        InitializeComponent();
    }

    public FormMessage(double minutes)
    {
        InitializeComponent();

        string message = string.Format("You have {0} minutes!", minutes);
        lblMessage.Text = message ;
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

JobMessage.cs

public class JobMessage: IJob
{
    List<Information> informations;

    public void Execute(IJobExecutionContext context)
    {
        //Class with methods to get registers from database.
        InformationAPI infoAPI = new InformationAPI();

        informations = infoAPI.GetInformations();

        foreach (Information info in informations)
        {
            DateTime computerDateTime = DateTime.Now;
            DateTime infoDateTime = info.StartDate;
            double difference;

            if (DateTime.Compare(computerDateTime, infoDateTime) < 0)
            {
                difference = Math.Round(infoDateTime.Subtract(computerDateTime).TotalMinutes);

                if (difference == 5)
                {
                    FormMessage formMessage = new FormMessage(difference);
                    formMessage.Show();
                }
            }
        }
    }
}

有人知道 FormMessage 窗口停止响应的原因吗?

感谢您的关注!

c# winforms quartz.net
1个回答
0
投票

您可以尝试使用 Quartz Listeners 让它们打开表单以显示数据并将执行保持在作业范围之外:

Action<IJobExecutionContext, JobExecutionException> listenerAction = (c, e) => {
    var dataMap = context.GetJobDetail().GetJobDataMap();
    var difference = dataMap.GetIntValue("difference");

    FormMessage formMessage = new FormMessage(difference);
    formMessage.Show();
}

var listener = new SyncJobListener(listenerAction);

并将侦听器添加到调度程序中:

scheduler.ListenerManager.AddJobListener(listener, 
    GroupMatcher<JobKey>.GroupEquals("GroupName"));

使用这个

SyncJobListener

public class SyncJobListener : IJobListener
{
    private readonly Action<IJobExecutionContext, JobExecutionException> _syncExecuted;

    public string Name { get; private set; }

    public SyncJobListener(
        Action<IJobExecutionContext, JobExecutionException> syncExecuted
        )
    {
        Name = Guid.NewGuid().ToString();

        _syncExecuted = syncExecuted;
    }

    public void JobToBeExecuted(IJobExecutionContext context)
    {
    }

    public void JobExecutionVetoed(IJobExecutionContext context)
    {
    }

    public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
    {
        _syncExecuted(context, jobException);
    }
}

我没有测试过这个所以如果

dataMap
没有任何数据,你将需要允许持久性:

[PersistJobDataAfterExecution]
[DisallowConcurrentExecution]
public class JobMessage: IJob {}
© www.soinside.com 2019 - 2024. All rights reserved.