image.png

How to add window closing event to C# console application?

How to add window closing event to C# console application? The company has a console application and wants to process business logic before closing the console application window. For example, if it is closed by mistake, or if the message queue is started to send a message reminder, then closing the window means that the console application is destroyed, and a message notification needs to be sent. At this time, adding the close window event will come in handy. The company has a console application, and wants to process the business logic before closing the console application window. For example, if it is closed by mistake, or if the message queue is started to send a message reminder, then closing the window means that the console application is destroyed, and a message notification needs to be sent. At this time, adding the close window event will come in handy. Many friends ask, is there a way to control the closing and exiting events of the form like WinForm? There is no default, we can write it ourselves. Follow the steps below: 1. Write the following code in the Program class of the [Program.cs] file. Adding a delegate HandlerAppClose is to…

image

WPF realizes the jumping character effect

WPF realizes the jumping character effect This article will introduce a fun animation effect that may not be very practical: jumping characters. In order to improve the reusability of the animation effect and the flexibility of calling, the beating character animation is realized through Behavior. First look at the effect: Technical Points and Implementation Through the PositionStart and PositionCount properties of TextEffect to control the starting position and length of the substring to which the animation effect is applied, and use TranslateTransform Set the movement transformation of the character ordinate to achieve the jumping effect. The main steps are as follows: In the OnAttached method, register the Loaded event, add the TextEffect effect for TextBlock in the Load event, Among them, set PositionCount to 1, and only jump one character at a time. Add the BeginEffect method to start the animation effect, and create a linear animation that controls the vertical movement and transformation of sub-characters. Then according to the length n of the string (excluding null characters), create n keyframes, and set PositionStart in each keyframe to the index of the character to be jumped in the string When the animation property IsEnabled=true and the content of TextBlock change,…

Winform cross-thread access UI

Winform accesses UI across threads

Winform accesses UI across threads When developing `winfrom` applications, I often encounter exceptions: `System.InvalidOperationException: “Invalid inter-thread operation: Access control “xxxx” from a thread other than the one that created it. The reason for this exception is the thread that created this UI, and The thread currently accessing this UI will not be the same. In order to prevent thread insecurity, Winform throws an exception for this cross-thread access and prohibits this operation. When developing winfrom applications, I often encounter an exception: System.InvalidOperationException: “Invalid inter-thread operation: Control ‘xxxx’ is accessed from a thread other than the thread that created it. The reason for this exception is that the thread that created this UI will not be the same as the thread currently accessing this UI. In order to prevent thread insecurity, Winform throws an exception for this cross-thread access and prohibits this operation. Solution Use the InvokeRequired attribute to determine whether it is thread-safe. if (richTextBox1.InvokeRequired) { richTextBox1.Invoke(new Action(() => { richTextBox1. AppendText(log); richTextBox1.AppendText(“\r\n”); })); } else { richTextBox1. AppendText(log); richTextBox1.AppendText(“\r\n”); } If richTextBox1 is created in non-main thread or its handle cannot be found, then richTextBox1.InvokeRequired=false returns false, and it will go to the else branch. If you are looking…

Operation filter – use operation filter in MVC to realize JWT authority authentication

Operation filter – use operation filter in MVC to realize JWT authority authentication Preface The previous article shared the authorization filter to implement JWT authentication. The article link: Authorization filter – use authorization filter in MVC to implement JWT authentication. Next, we will use the operation filter to implement yesterday’s JWT authentication. 1. What is an operation filter? ​ Most of the same as the authorization filter, but the timing of execution and the inherited interface are different. The operation filter is called before and after the execution of the Action. Instead of executing the authorization filter as soon as it is opened, like the authorization filter. Action filters implement the IAsyncActionFilter or IActionFilter interface. Second, operation filter implementation Operation filter redefines Filter: Inherit the class Attribute, the interface MyAuthorizeFilterAttribute, and then implement the interface OnActionExecuted method and OnActionExecuting method to provide more processing methods for Action execution. Write custom business logic here when Action is executed. /// /// Authorization filter /// public class MyAuthorizeFilterAttribute : Attribute, IActionFilter { #region IActionFilter action filter implementation /// /// After the Action is executed /// /// public void OnActionExecuted(ActionExecutedContext context) { } /// /// Before the Action is executed /// /// public void…

Resource filter—Using resource filter in MVC to realize not executing Action method body to read cache information and return

Resource filter—use resource filter in MVC to realize not executing Action method body to read cached information and return Preface The last two articles share the filter implementation of JWT for authentication, respectively, through the authorization filter and the operation filter. These two filters are also the most commonly used. Article link: Authorization filter – use authorization filter in MVC to realize JWT permission authentication, operation filter – use operation filter in MVC to realize JWT permission authentication, then I will briefly talk about how to use resource filter in MVC, generally This filter is rarely used in projects. 1. What is a resource filter? Filter is an implementation of the AOP (Aspect-Oriented Programming) idea, which allows us to execute code at a specific stage of the execution pipeline. By using filters, short-circuit requests, cached request results, unified log records, and parameters can be realized. Legality verification, unified exception handling, return value formatting, etc., while making the business code more concise and simple, avoiding a lot of repeated code. So in our filters, most of the filters have the method of starting to execute the action, that is, the ing state, and the method of the ed state triggered after…

Exception filter – use of exception filter in MVC

Exception filter – use of exception filter in MVC 1. What is an exception filter? Exception Filters (Exception Filters) is a mechanism for handling global exceptions in ASP.NET Core. They allow you to catch, handle, and log exceptions as they occur, and provide custom exception handling logic. Exception filters take effect throughout the application and can be used to handle various exceptions. Used to implement common error handling strategies, without before and after events, to handle unhandled exceptions that occur in Razor Page or controller creation, model binding, action filters, or action methods. However, exceptions that occur in resource filters, result filters, or MVC result execution cannot be caught. 2, exception filter implementation Exception filter definition: The definition of exception filter needs to implement the interface IExceptionFilter or IAsyncExceptionFilter, which contains an OnException method or OnExceptionAsync method to handle the exception that occurs, and rewrite this method after inheritance. #region <> /*———————————————— —————- * Created by: code farmer A Liang * Creation time: 2023/8/15 14:02:01 * Version: V1.0.0 * describe: * * ————————————————– ————— * Modified by: * time: * Modify the description: * * Version: V1.0.1 *————————————————– —————*/ #endregion <> using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using System; namespace MyWebApp. Filter…

Result Filter—Use of Result Filter in MVC project

Result filter—use Result Filter in MVC project 1. What is a result filter? Result filter (ResultFilter) is an AOP idea for processing the executed Action result, applicable to any logic that needs to be directly surrounded by View or formatted. Result filters can replace or change Action results. Execute before and after the execution of IActionResult, use it to control the execution result of the Action, such as: format the result, etc. Note that it will only run after the Action method completes successfully. It is rarely used in general projects. Execution order: 1. Execute the constructor in the controller and instantiate the controller 2. Execute the specific Action method 3. Execute the ResultFilter.OnResultExecuting method 4. Render the view or process the result 5. Execute the ResultFilter.OnResultExecuted method Second, result filter implementation Result Filter Definition: The different result filter implements the IAsyncResultFilter or IResultFilter interface. When implementing IAsyncResultFilter, rewrite the OnResultExecutionAsync method, implement IResultFilter, and rewrite the OnResultExecuting and OnResultExecuted methods. /// /// result filter /// public class MyResultFilter : Attribute, IResultFilter { #region IResultFilter /// ///before execution /// /// public void OnResultExecuting(ResultExecutingContext context) { if (context. Result is JsonResult) { JsonResult result = (JsonResult)context.Result; //Return the result and re-encapsulate…

Unity editor resource import processing function OnPreprocessTexture: in-depth analysis and practical cases

Unity editor resource import processing function OnPreprocessTexture: in-depth analysis and practical cases Unity editor resource import processing function OnPreprocessTexture usage Click on the cover to jump to the download page Introduction In Unity, we can use the editor resource import processing function (OnPreprocessTexture) to customize the import process of texture resources. This function is inherited from the AssetPostprocessor class. By rewriting this function, we can perform some custom operations before importing texture resources. Inherit AssetPostprocessor First, we need to create a script that inherits from AssetPostprocessor. This script will be used to handle the import process of texture assets. Here is a sample code: using UnityEditor; using UnityEngine; public class TexturePostprocessor : AssetPostprocessor { void OnPreprocessTexture() { // Write custom texture import processing logic here } } In this example, we create a script called TexturePostprocessor and override the OnPreprocessTexture function. Custom texture import processing logic In the OnPreprocessTexture function, we can write custom texture import processing logic. Here are five sample codes showing different usages: 1. Modify texture import settings void OnPreprocessTexture() { TextureImporter textureImporter = (TextureImporter)assetImporter; textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spritePixelsPerUnit = 100; } In this example, we set the texture’s type to Sprite and the number of pixels…

C# uses Refit to realize automatic renewal of JWT

C# uses Refit to realize JWT automatic renewal Foreword The author has developed a set of desktop applications with C/S architecture before, using JWT as the login authentication and authorization of users. The only problem encountered is what should I do if the JWT expires? Imagine that when a user is doing business operations, suddenly jumps to the login interface inexplicably because the Token expires, is it a very speechless thing? Of course, the author has also thought about it: why not set the validity period of JWT as long as possible (assuming 24 hours), the user always has to log out of the system after get off work every day, ha ha! This is obviously a bit opportunistic, and it also violates the security design of JWT. It seems that we will find another way. Design ideas Later, the author’s approach is: when the client initiates an Http request each time, first determine whether the local Token exists: 1. If it does not exist, first initiate a login verification request to the server to obtain the Token. 2. If it already exists, check whether the Token is about to expire. If so, re-initiate login verification to update the Token,…

Analysis and generation of C# QRCode two-dimensional code

Analysis and generation of C# QRCode two-dimensional code Knowing a QR code image, how to generate an identical image? Recently, there is a project that needs to use QRCode. I only did Datamatrix format before, and I thought it should be similar, so I followed the gourd and took out my old OnBarcode class library, modified it, and generated it. A brand-new QRCode was compared with the QR code picture provided by the customer. Although the scanned content was the same, the pattern was obviously different, so I realized that the matter was not simple. The original picture is as follows: First of all, the first thing to suspect is the parameter problem. After looking at the properties of OnBarcode.Barcode.QRCode, the suspicious parameters include DataMode, ECL, Version, etc. After all, these should be common parameters. The biggest possibility is ECL, which is the error correction level of the QR code. Generally, there are four levels of L/M/Q/H, which correspond to 7%/15%/25%/30%. If you insert a logo into the code, you need to increase the error correction level. So I tried it, modified different ECLs, and the corresponding output patterns were different, but still did not generate the pattern I wanted.…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索