定时任务 Wpf.Quartz.Demo.2
定时任务 Wpf.Quartz.Demo.1已经能运行了,本节开始用wpf搭界面。
准备工作:
1.界面选择MahApp.Metro
在App.xaml添加资源
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
<ResourceDictionary>
<System:Double x:Key="WindowTitleFontSize"></System:Double>
<System:Double x:Key="NormalFontSize"></System:Double>
<System:Double x:Key="ContentFontSize"></System:Double>
<System:Double x:Key="FlatButtonFontSize"></System:Double>
<System:Double x:Key="MenuFontSize"></System:Double>
<System:Double x:Key="ContextMenuFontSize"></System:Double>
<System:Double x:Key="StatusBarFontSize"></System:Double>
<System:Double x:Key="ToggleSwitchFontSize"></System:Double>
<System:Double x:Key="ToggleSwitchHeaderFontSize"></System:Double>
<System:Double x:Key="UpperCaseContentFontSize"></System:Double>
<System:Double x:Key="TabItemFontSize"></System:Double>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
App.xaml
其中 <System:Double x:Key="WindowTitleFontSize">12</System:Double> ...为改变整个app的相关字体大小。
2.添加log4net记录异常
安装log4net。
在App.config内配置log4net
<log4net>
<!--按日期分割日志文件 一天一个-->
<appender name="LogFileAppenderByDate" type="log4net.Appender.RollingFileAppender">
<!--是否续写-->
<param name="AppendToFile" value="true" />
<!--最小锁定模型以允许多个进程可以写入同一个文件-->
<param name="LockingModel" value="log4net.Appender.FileAppender.MinimalLock" />
<param name="StaticLogFileName" value="true" />
<!--保存路径-->
<param name="File" value="Log\" />
<param name="DatePattern" value="yyyy-MM-dd.LOG" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%n-----------------------------------------%n时间:%d %n级别:%level %n类名:%c%n文件:%F 第%L行%n日志内容:%m%n-----------------------------------------%n%n" />
</layout>
</appender>
<root>
<!--文件形式记录日志-->
<appender-ref ref="LogFileAppenderByDate" />
</root>
</log4net>
App.config
在App.xaml.cs内添加
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows; [assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Wpf.Quartz
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
public App()
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.Current.DispatcherUnhandledException += Application_DispatcherUnhandledException;
} private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
//记录严重错误
log.Fatal(e.Exception);
e.Handled = true;//使用这一行代码告诉运行时,该异常被处理了,不再作为UnhandledException抛出了。
} void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//记录严重错误
log.Fatal(e.ExceptionObject);
}
}
}
App.xaml.cs
其中 AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.Current.DispatcherUnhandledException += Application_DispatcherUnhandledException; 捕获没有处理的异常,避免程序崩溃。
3.使用MVVM模式,这里不使用mvvmlight,prism等框架,这里写个简单的类ViewModel继承它即可。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Wpf.Quartz.Helper
{
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
BaseViewModel
4.前台主界面

<Controls:MetroWindow x:Class="MyWpf.Quart.Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
Title="QuartzDemo" Height="" Width="" WindowStartupLocation="CenterScreen" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height=""></RowDefinition>
</Grid.RowDefinitions>
<Menu Background = "{DynamicResource AccentColorBrush}">
<MenuItem Header="系统菜单" Background="Transparent">
<MenuItem Header="全部开始" Command="{Binding MeunCommand}" CommandParameter="StartAll"/>
<MenuItem Header="全部停止" Command="{Binding MeunCommand}" CommandParameter="StopAll"/>
</MenuItem>
</Menu>
<DataGrid Grid.Row="" x:Name="table" AutoGenerateColumns="False" CanUserAddRows="False" ItemsSource="{Binding TaskRuns}" SelectedItem="{Binding SelectedRun}" ColumnWidth="Auto">
<DataGrid.ContextMenu>
<ContextMenu >
<MenuItem Header="启动" Command="{Binding JobStartCommand}"
CommandParameter="{Binding SelectedRun}"
/>
<MenuItem Header="停止" Command="{Binding JobStopCommand}"
CommandParameter="{Binding SelectedRun}"
/>
<MenuItem Header="重新启动" Command="{Binding JobReStartCommand}"
CommandParameter="{Binding SelectedRun}"
/>
<MenuItem Header="暂停" Command="{Binding JobPauseCommand}"
CommandParameter="{Binding SelectedRun}"
/>
<MenuItem Header="恢复" Command="{Binding JobResumeCommand}"
CommandParameter="{Binding SelectedRun}"
/>
<MenuItem Header="手动执行一次" Command="{Binding JobRunCommand}"
CommandParameter="{Binding SelectedRun}"
/>
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.Columns>
<DataGridTextColumn Header="编辑" IsReadOnly="True" Binding="{Binding IsEdit}"></DataGridTextColumn>
<DataGridTextColumn Header="名称" IsReadOnly="True" Binding="{Binding DisplayName}"></DataGridTextColumn>
<DataGridTextColumn Header="标识符" IsReadOnly="True" Binding="{Binding Name}"></DataGridTextColumn>
<DataGridTemplateColumn Header="状态">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="描述" IsReadOnly="True" Binding="{Binding Remark}"></DataGridTextColumn>
<DataGridTemplateColumn Header="执行设置" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="txt" ToolTip="点击设置" VerticalAlignment="Center">
<Hyperlink Command="{Binding DataContext.JobSetCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"
CommandParameter="{Binding }">设置 </Hyperlink>
<Run Text="{Binding SettingStr}"></Run>
</TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="开始时间" IsReadOnly="True" Binding="{Binding StartTime,StringFormat=yyyy-MM-dd HH:mm:ss}"></DataGridTextColumn>
<DataGridTextColumn Header="结束时间" IsReadOnly="True" Binding="{Binding EndTime,StringFormat=yyyy-MM-dd HH:mm:ss}"></DataGridTextColumn>
<DataGridTextColumn Header="下次执行时间" IsReadOnly="True" Binding="{Binding NextRunTime,StringFormat=yyyy-MM-dd HH:mm:ss}"></DataGridTextColumn>
<DataGridTemplateColumn Header="详情">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="txt" ToolTip="点击打开" VerticalAlignment="Center">
<Hyperlink Command="{Binding DataContext.JobDetailCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"
CommandParameter="{Binding }">详情</Hyperlink>
</TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<GridSplitter x:Name="gsSplitterr" Grid.Row="" Height="" Background="{DynamicResource AccentColorBrush}" HorizontalAlignment="Stretch" VerticalAlignment="Center" />
<RichTextBox x:Name="rtb" Grid.Row="" IsReadOnly="True" VerticalScrollBarVisibility="Auto">
<RichTextBox.ContextMenu>
<ContextMenu>
<MenuItem Header="剪贴" Command="ApplicationCommands.Cut"/>
<MenuItem Header="复制" Command="ApplicationCommands.Copy"/>
<MenuItem Header="粘贴" Command="ApplicationCommands.Paste"/>
</ContextMenu>
</RichTextBox.ContextMenu>
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value=""/>
<Setter Property="LineHeight" Value=""/>
</Style>
</RichTextBox.Resources>
</RichTextBox>
</Grid>
</Controls:MetroWindow>
MainWindow
好的,至此准备工作已经完成。预计还有两节全部完成。
代码下载:https://pan.baidu.com/s/1Ri_yangO0N0sfC-KyZVwbw
定时任务 Wpf.Quartz.Demo.2的更多相关文章
- 定时任务 Wpf.Quartz.Demo.4
本文继续介绍定时任务 Wpf.Quartz.Demo.3的一些小细节, 代码也请前往第3节下载. 1.RichTextBox右键菜单 <RichTextBox.ContextMenu> ...
- 定时任务 Wpf.Quartz.Demo.1
Quartz 是个开源的作业调度框架. 安装:Install-Package Quartz 官网文档地址:https://www.quartz-scheduler.net/documentation/ ...
- 定时任务 Wpf.Quartz.Demo.5 (升级版)
老规矩:先把全部源码上传,见本文底部. 相对于Demo3的区别,就是能自动加载继承了IJob的任务,任务主体程序分离. 在exe执行文件的同级下建一个MyJobs的文件夹,每次会自动扫描该文件夹下的J ...
- 定时任务 Wpf.Quartz.Demo.3
先把全部源码上传,只是一个Demo,希望大家指点一下不足之处,见本文底部. 1.设置界面 2.详情页面 好了,现在慢慢叙述里面的一些方法. 3.实现拷贝的方法: (1) public static v ...
- [转][JAVA]定时任务之-Quartz使用篇
[BAT][JAVA]定时任务之-Quartz使用篇 定时任务之-Quartz使用篇 Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与 ...
- Spring整合quartz2.2.3总结,quartz动态定时任务,Quartz定时任务集群配置
Spring整合quartz2.2.3总结,quartz动态定时任务,Quartz定时任务集群配置 >>>>>>>>>>>>&g ...
- 震惊!Windows Service服务和定时任务框架quartz之间原来是这种关系……
过场CG: 接到公司领导的文件指示,“小熊”需要在6月底去海外执行一个行动代号为[定时任务]的营救计划,这个计划关系到公司某个项目的生死(数据安全漏洞),作战部拟定两个作战方案: 方案一:使用务定 ...
- SpringBoot定时任务 - 集成quartz实现定时任务(单实例和分布式两种方式)
最为常用定时任务框架是Quartz,并且Spring也集成了Quartz的框架,Quartz不仅支持单实例方式还支持分布式方式.本文主要介绍Quartz,基础的Quartz的集成案例本,以及实现基于数 ...
- Spring 整合 Quartz 实现动态定时任务(附demo)
最近项目中需要用到定时任务的功能,虽然Spring 也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之后,决定整合更为专业的Quartz来实现定时任务功能. 普通定时任务 首先 ...
随机推荐
- linux和普通文本的换行问题
情景一: 普通文本 vim操作换行 :%s#xxx#\n#g 情景二: linux环境换行 vim :%s#xxx#\r#g
- Spark学习笔记-GraphX-1
Spark学习笔记-GraphX-1 标签: SparkGraphGraphX图计算 2014-09-29 13:04 2339人阅读 评论(0) 收藏 举报 分类: Spark(8) 版权声明: ...
- Git工具和GitHub的使用
一.Git工具的安装 1)centos系统下安装 1.1)查看环境 [root@gitlab ~]# rpm -qa centos-release centos-release--4.1708.el7 ...
- IDEA的GUI连接数据库写入SQL语句的问题总结
一.首先是建立游标的对象statement 插入数据excuteUpdate需要的是一个整型的参数,所以建立的对象要是一个int型的数据类型,才可以执行SQL语句excuteQuery是一个字符类型在 ...
- idea运行项目时报Error:java无效的源发行版:1.8
如果你安装的是JDK1.7,而在file->project structure中设置的是language level是8的话,就会出现这个错误提示:无效的源发行版:8. 解决办法:将语言级别改为 ...
- 2019.01.21 NOIP训练 可持久化序列【模板】(可持久化treap)
传送门 题意简述:支持在把某个数插入到某版本的第k个位置,删除某版本第k个数,询问第k个数. 思路:用可持久化treaptreaptreap维护区间第kkk个位置的数是啥就可以了. 代码
- js生成条形码插件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java基础语法学习知识
基础概念 标识符1.由字母,数字,下划线,美元符组成2.首字母不能是数字3.不能是关键字和保留字4.能反映其作用 关键字1.有特定含义2.用于特定地方3.用来命名标识符 常量1.程序执行时值不变的量2 ...
- @RequestBody jackson解析复杂的传入值的一个坑;jackson解析迭代数组;jackson多重数组;jakson数组
一.实际开发的一个问题. 传入一个json数组,数组中还嵌套数组,运用springboot+Jpa框架,@RequestBody注解传入数据 Controller @ApiOperation(valu ...
- fastjson 错误解决方案详情 com.alibaba.fastjson.JSONException: syntax error, expect {, actual EOF, pos 1410
原因: 前端传递的数组过于复杂,倒是出现这种问题,前端采用vue axios,发送请求,后端java接收代码,实现前后端分离 后端就收fastjson接收json,进行业务处理,后端Controlle ...