定时任务 Wpf.Quartz.Demo.1已经能运行了,本节开始用wpf搭界面。

准备工作:

1.界面选择MahApp.Metro

在App.xaml添加资源

  1. <Application.Resources>
  2. <ResourceDictionary>
  3. <ResourceDictionary.MergedDictionaries>
  4. <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
  5. <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
  6. <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
  7. <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
  8. <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
  9. <ResourceDictionary>
  10. <System:Double x:Key="WindowTitleFontSize"></System:Double>
  11. <System:Double x:Key="NormalFontSize"></System:Double>
  12. <System:Double x:Key="ContentFontSize"></System:Double>
  13. <System:Double x:Key="FlatButtonFontSize"></System:Double>
  14. <System:Double x:Key="MenuFontSize"></System:Double>
  15. <System:Double x:Key="ContextMenuFontSize"></System:Double>
  16. <System:Double x:Key="StatusBarFontSize"></System:Double>
  17. <System:Double x:Key="ToggleSwitchFontSize"></System:Double>
  18. <System:Double x:Key="ToggleSwitchHeaderFontSize"></System:Double>
  19. <System:Double x:Key="UpperCaseContentFontSize"></System:Double>
  20. <System:Double x:Key="TabItemFontSize"></System:Double>
  21. </ResourceDictionary>
  22. </ResourceDictionary.MergedDictionaries>
  23. </ResourceDictionary>
  24. </Application.Resources>

App.xaml

其中   <System:Double x:Key="WindowTitleFontSize">12</System:Double> ...为改变整个app的相关字体大小。

2.添加log4net记录异常

安装log4net。

在App.config内配置log4net

  1. <log4net>
  2. <!--按日期分割日志文件 一天一个-->
  3. <appender name="LogFileAppenderByDate" type="log4net.Appender.RollingFileAppender">
  4. <!--是否续写-->
  5. <param name="AppendToFile" value="true" />
  6. <!--最小锁定模型以允许多个进程可以写入同一个文件-->
  7. <param name="LockingModel" value="log4net.Appender.FileAppender.MinimalLock" />
  8. <param name="StaticLogFileName" value="true" />
  9. <!--保存路径-->
  10. <param name="File" value="Log\" />
  11. <param name="DatePattern" value="yyyy-MM-dd.LOG" />
  12. <param name="StaticLogFileName" value="false" />
  13. <param name="RollingStyle" value="Date" />
  14. <layout type="log4net.Layout.PatternLayout">
  15. <param name="ConversionPattern" value="%n-----------------------------------------%n时间:%d %n级别:%level %n类名:%c%n文件:%F 第%L行%n日志内容:%m%n-----------------------------------------%n%n" />
  16. </layout>
  17. </appender>
  18. <root>
  19. <!--文件形式记录日志-->
  20. <appender-ref ref="LogFileAppenderByDate" />
  21. </root>
  22. </log4net>

App.config

在App.xaml.cs内添加

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8.  
  9. [assembly: log4net.Config.XmlConfigurator(Watch = true)]
  10. namespace Wpf.Quartz
  11. {
  12. /// <summary>
  13. /// App.xaml 的交互逻辑
  14. /// </summary>
  15. public partial class App : Application
  16. {
  17. public App()
  18. {
  19. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  20. Application.Current.DispatcherUnhandledException += Application_DispatcherUnhandledException;
  21. }
  22.  
  23. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  24.  
  25. private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  26. {
  27. //记录严重错误
  28. log.Fatal(e.Exception);
  29. e.Handled = true;//使用这一行代码告诉运行时,该异常被处理了,不再作为UnhandledException抛出了。
  30. }
  31.  
  32. void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  33. {
  34. //记录严重错误
  35. log.Fatal(e.ExceptionObject);
  36. }
  37. }
  38. }

App.xaml.cs

其中  AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
         Application.Current.DispatcherUnhandledException += Application_DispatcherUnhandledException;  捕获没有处理的异常,避免程序崩溃。

3.使用MVVM模式,这里不使用mvvmlight,prism等框架,这里写个简单的类ViewModel继承它即可。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Wpf.Quartz.Helper
  9. {
  10. public class BaseViewModel : INotifyPropertyChanged
  11. {
  12. public event PropertyChangedEventHandler PropertyChanged;
  13. public virtual void OnPropertyChanged(string propertyName)
  14. {
  15. if (PropertyChanged != null)
  16. {
  17. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  18. }
  19. }
  20. }
  21. }

BaseViewModel

4.前台主界面

  1. <Controls:MetroWindow x:Class="MyWpf.Quart.Demo.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
  7. xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
  8. mc:Ignorable="d"
  9. Title="QuartzDemo" Height="" Width="" WindowStartupLocation="CenterScreen" >
  10. <Grid>
  11. <Grid.RowDefinitions>
  12. <RowDefinition Height="Auto"></RowDefinition>
  13. <RowDefinition Height="*"></RowDefinition>
  14. <RowDefinition Height="Auto"></RowDefinition>
  15. <RowDefinition Height=""></RowDefinition>
  16. </Grid.RowDefinitions>
  17. <Menu Background = "{DynamicResource AccentColorBrush}">
  18. <MenuItem Header="系统菜单" Background="Transparent">
  19. <MenuItem Header="全部开始" Command="{Binding MeunCommand}" CommandParameter="StartAll"/>
  20. <MenuItem Header="全部停止" Command="{Binding MeunCommand}" CommandParameter="StopAll"/>
  21. </MenuItem>
  22. </Menu>
  23. <DataGrid Grid.Row="" x:Name="table" AutoGenerateColumns="False" CanUserAddRows="False" ItemsSource="{Binding TaskRuns}" SelectedItem="{Binding SelectedRun}" ColumnWidth="Auto">
  24. <DataGrid.ContextMenu>
  25. <ContextMenu >
  26. <MenuItem Header="启动" Command="{Binding JobStartCommand}"
  27. CommandParameter="{Binding SelectedRun}"
  28. />
  29. <MenuItem Header="停止" Command="{Binding JobStopCommand}"
  30. CommandParameter="{Binding SelectedRun}"
  31. />
  32. <MenuItem Header="重新启动" Command="{Binding JobReStartCommand}"
  33. CommandParameter="{Binding SelectedRun}"
  34. />
  35. <MenuItem Header="暂停" Command="{Binding JobPauseCommand}"
  36. CommandParameter="{Binding SelectedRun}"
  37. />
  38. <MenuItem Header="恢复" Command="{Binding JobResumeCommand}"
  39. CommandParameter="{Binding SelectedRun}"
  40. />
  41. <MenuItem Header="手动执行一次" Command="{Binding JobRunCommand}"
  42. CommandParameter="{Binding SelectedRun}"
  43. />
  44. </ContextMenu>
  45. </DataGrid.ContextMenu>
  46. <DataGrid.Columns>
  47. <DataGridTextColumn Header="编辑" IsReadOnly="True" Binding="{Binding IsEdit}"></DataGridTextColumn>
  48. <DataGridTextColumn Header="名称" IsReadOnly="True" Binding="{Binding DisplayName}"></DataGridTextColumn>
  49. <DataGridTextColumn Header="标识符" IsReadOnly="True" Binding="{Binding Name}"></DataGridTextColumn>
  50. <DataGridTemplateColumn Header="状态">
  51. <DataGridTemplateColumn.CellTemplate>
  52. <DataTemplate>
  53. <TextBlock VerticalAlignment="Center"/>
  54. </DataTemplate>
  55. </DataGridTemplateColumn.CellTemplate>
  56. </DataGridTemplateColumn>
  57. <DataGridTextColumn Header="描述" IsReadOnly="True" Binding="{Binding Remark}"></DataGridTextColumn>
  58. <DataGridTemplateColumn Header="执行设置" Width="*">
  59. <DataGridTemplateColumn.CellTemplate>
  60. <DataTemplate>
  61. <StackPanel Orientation="Horizontal">
  62. <TextBlock x:Name="txt" ToolTip="点击设置" VerticalAlignment="Center">
  63. <Hyperlink Command="{Binding DataContext.JobSetCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"
  64. CommandParameter="{Binding }">设置 </Hyperlink>
  65. <Run Text="{Binding SettingStr}"></Run>
  66. </TextBlock>
  67. </StackPanel>
  68. </DataTemplate>
  69. </DataGridTemplateColumn.CellTemplate>
  70. </DataGridTemplateColumn>
  71. <DataGridTextColumn Header="开始时间" IsReadOnly="True" Binding="{Binding StartTime,StringFormat=yyyy-MM-dd HH:mm:ss}"></DataGridTextColumn>
  72. <DataGridTextColumn Header="结束时间" IsReadOnly="True" Binding="{Binding EndTime,StringFormat=yyyy-MM-dd HH:mm:ss}"></DataGridTextColumn>
  73. <DataGridTextColumn Header="下次执行时间" IsReadOnly="True" Binding="{Binding NextRunTime,StringFormat=yyyy-MM-dd HH:mm:ss}"></DataGridTextColumn>
  74. <DataGridTemplateColumn Header="详情">
  75. <DataGridTemplateColumn.CellTemplate>
  76. <DataTemplate>
  77. <StackPanel Orientation="Horizontal">
  78. <TextBlock x:Name="txt" ToolTip="点击打开" VerticalAlignment="Center">
  79. <Hyperlink Command="{Binding DataContext.JobDetailCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"
  80. CommandParameter="{Binding }">详情</Hyperlink>
  81. </TextBlock>
  82. </StackPanel>
  83. </DataTemplate>
  84. </DataGridTemplateColumn.CellTemplate>
  85. </DataGridTemplateColumn>
  86. </DataGrid.Columns>
  87. </DataGrid>
  88. <GridSplitter x:Name="gsSplitterr" Grid.Row="" Height="" Background="{DynamicResource AccentColorBrush}" HorizontalAlignment="Stretch" VerticalAlignment="Center" />
  89. <RichTextBox x:Name="rtb" Grid.Row="" IsReadOnly="True" VerticalScrollBarVisibility="Auto">
  90. <RichTextBox.ContextMenu>
  91. <ContextMenu>
  92. <MenuItem Header="剪贴" Command="ApplicationCommands.Cut"/>                     
  93. <MenuItem Header="复制" Command="ApplicationCommands.Copy"/>                        
  94. <MenuItem Header="粘贴" Command="ApplicationCommands.Paste"/>                        
  95. </ContextMenu>
  96. </RichTextBox.ContextMenu>
  97. <RichTextBox.Resources>
  98. <Style TargetType="{x:Type Paragraph}">
  99. <Setter Property="Margin" Value=""/>
  100. <Setter Property="LineHeight" Value=""/>
  101. </Style>
  102. </RichTextBox.Resources>
  103. </RichTextBox>
  104. </Grid>
  105. </Controls:MetroWindow>

MainWindow

好的,至此准备工作已经完成。预计还有两节全部完成。

代码下载:https://pan.baidu.com/s/1Ri_yangO0N0sfC-KyZVwbw

定时任务 Wpf.Quartz.Demo.2的更多相关文章

  1. 定时任务 Wpf.Quartz.Demo.4

    本文继续介绍定时任务 Wpf.Quartz.Demo.3的一些小细节, 代码也请前往第3节下载. 1.RichTextBox右键菜单 <RichTextBox.ContextMenu>   ...

  2. 定时任务 Wpf.Quartz.Demo.1

    Quartz 是个开源的作业调度框架. 安装:Install-Package Quartz 官网文档地址:https://www.quartz-scheduler.net/documentation/ ...

  3. 定时任务 Wpf.Quartz.Demo.5 (升级版)

    老规矩:先把全部源码上传,见本文底部. 相对于Demo3的区别,就是能自动加载继承了IJob的任务,任务主体程序分离. 在exe执行文件的同级下建一个MyJobs的文件夹,每次会自动扫描该文件夹下的J ...

  4. 定时任务 Wpf.Quartz.Demo.3

    先把全部源码上传,只是一个Demo,希望大家指点一下不足之处,见本文底部. 1.设置界面 2.详情页面 好了,现在慢慢叙述里面的一些方法. 3.实现拷贝的方法: (1) public static v ...

  5. [转][JAVA]定时任务之-Quartz使用篇

    [BAT][JAVA]定时任务之-Quartz使用篇 定时任务之-Quartz使用篇 Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与 ...

  6. Spring整合quartz2.2.3总结,quartz动态定时任务,Quartz定时任务集群配置

    Spring整合quartz2.2.3总结,quartz动态定时任务,Quartz定时任务集群配置 >>>>>>>>>>>>&g ...

  7. 震惊!Windows Service服务和定时任务框架quartz之间原来是这种关系……

    过场CG:   接到公司领导的文件指示,“小熊”需要在6月底去海外执行一个行动代号为[定时任务]的营救计划,这个计划关系到公司某个项目的生死(数据安全漏洞),作战部拟定两个作战方案: 方案一:使用务定 ...

  8. SpringBoot定时任务 - 集成quartz实现定时任务(单实例和分布式两种方式)

    最为常用定时任务框架是Quartz,并且Spring也集成了Quartz的框架,Quartz不仅支持单实例方式还支持分布式方式.本文主要介绍Quartz,基础的Quartz的集成案例本,以及实现基于数 ...

  9. Spring 整合 Quartz 实现动态定时任务(附demo)

    最近项目中需要用到定时任务的功能,虽然Spring 也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之后,决定整合更为专业的Quartz来实现定时任务功能. 普通定时任务 首先 ...

随机推荐

  1. Viewer.js 是一款强大的 jQuery 图像浏览插件。

    https://blog.csdn.net/qq_29132907/article/details/80136023 一.效果图  二.代码<!DOCTYPE html><html ...

  2. SHELL脚本取系统当前年月日问题 (去0)

    1. #!/bin/bash tmonth=`date +%m`tyear=`date +%y`tday=`date +%d`day=`expr $tday + 0`month=`expr $tmon ...

  3. Softmax && Cross-entropy Error

    softmax 函数,被称为 归一化指数函数,是sigmoid函数的推广. 它将向量等比压缩到[0, 1]之间,所有元素和为1. 图解: Example: softmax([1, 2, 3, 4, 1 ...

  4. 2018.12.17 bzoj4802: 欧拉函数(Pollard-rho)

    传送门 Pollard−rhoPollard-rhoPollard−rho模板题. 题意简述:求ϕ(n),n≤1e18\phi(n),n\le 1e18ϕ(n),n≤1e18 先把nnn用Pollar ...

  5. 简单实现java线程池

    使用多线程以及线程池的意义无需多说,要想掌握线程池,最好的方法还是自己手动去实现. 一.实现思路      (网络盗图) 二.实现代码 1.线程池类 package com.ty.thread; im ...

  6. Win7 VS2013环境编译CGAL-4.7

    看到有人在QQ空间感叹编译CGAL配置折腾了一天时间,自己也想试试,虽然并不打算用,但感觉这库也挺有名的,想必日后用得着,于是着手试着编译. 首先是看一下官网的windows下配置说明 http:// ...

  7. math.net 拟合

    参考:http://blog.csdn.net/ztmsimon/article/details/50524392 在论坛中总看到有人在说Math.NET Iridium,查了一下,现在被整合到Mat ...

  8. 详细解读 :java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed,Java报错之Connection is read-only.

    问题分析: 实际开发项目中,进行insert的时候,产生这个问题是Spring框架的一个安全权限保护方法,对于方法调用的事物保护,一般配置如下: <!-- 事务管理 属性 --> < ...

  9. BZOJ 4407 于神之怒加强版 (莫比乌斯反演 + 分块)

    4407: 于神之怒加强版 Time Limit: 80 Sec  Memory Limit: 512 MBSubmit: 1067  Solved: 494[Submit][Status][Disc ...

  10. Arria10_emif

    DDR3 由排(Rank),体(Bank),行(Row),列(Column)组成的四维结构. Arria10是第一批支持ddr4的altera Arria10与老器件相比的新结构 (1)  更多的硬( ...