简体   繁体   English

带有2个不同时间的计时器的Clipboard.clear不起作用C#

[英]Clipboard.clear with timer with 2 different times does not work C#

I'm making a clipboard application where my program must be able to paste both questions from my list and my sums. 我正在制作一个剪贴板应用程序,其中程序必须能够粘贴列表中的问题和金额。 Now I want that if I copy a sum that the result can be pasted for 10 seconds. 现在,我希望如果我复制一笔金额,结果可以粘贴10秒钟。 And that my question from my list can only be pasted for 1 second. 我的清单中的问题只能粘贴1秒钟。 At the moment it only works with the questions from my list. 目前,它仅适用于我列表中的问题。 That timer for that is called DeleteQuestionTimer. 该计时器称为DeleteQuestionTimer. now wants another timer with the name SumDeleteCopyTimer that ensures that the result of the sum that can be pasted into this piece of code for 10 seconds 现在需要另一个名称为SumDeleteCopyTimer计时器,以确保可以将总和的结果粘贴到这段代码中10秒钟

clipboardText = clipboardText.Replace(',', '.');

//de regex voor de operator
var regex = new Regex(@"^(?<lhs>\d+(?:[,.]{1}\d)*)[ ]*(?<operator>[+\-\:x\%])[ ]*(?<rhs>\d+(?:[,.]{1}\d)*)$");
Match match = regex.Match(clipboardText);

if (!match.Success)
    return; // not valid form (a + b)

//de operators
Dictionary<string, Func<double, double, double>> binaryOperators 
= new Dictionary<string, Func<double, double, double>>()
{
    { "+", (a, b) => a + b},
    { "x", (a, b) => a * b },
    { "-", (a, b) => a - b },
    { "%", (a, b) => a / b * 100 },
    { ":", (a, b) => b == 0 ? double.NaN : a / b },
};

var lhs = double.Parse(match.Groups["lhs"].Value, CultureInfo.InvariantCulture);
var rhs = double.Parse(match.Groups["rhs"].Value, CultureInfo.InvariantCulture);
var answer = binaryOperators[match.Groups["operator"].Value](lhs, rhs);

System.Windows.Forms.Clipboard.SetText(answer.ToString());
//Kopieert het resultaat van de som in het clipboard
//De timer zorgt ervoor dat het resultaat van de som naar x aantal seconde niet meer te plakken is
// Laat antwoord zien
ShowNotification(clipboardText, answer.ToString());

so what i want is that the result of the sum can be pasted for 10 seconds and the copied for my questionList can be pasted for 1 second. 所以我想要的是总和的结果可以粘贴10秒,而我的questionList的副本可以粘贴1秒。

This is my full code 这是我的完整代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace it
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //Invoke a clipboard monitor
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);

        private IntPtr _ClipboardViewerNext;

        //Make some global variables so we can access them somewhere else later
        //This will store all Questions and Answers
        //In here will be the Questions and Answers
        List<question> questionList = new List<question>();
        private object result;

        // Demonstrates SetText, ContainsText, and GetText.



        private void Form1_Load(object sender, EventArgs e)
        {
            //Set our application as a clipboard viewer
            _ClipboardViewerNext = SetClipboardViewer(Handle);

            //Add question/answer to list
            question newQuestion = new question("What is the capital of the Netherlands?", "Amsterdam");
            questionList.Add(newQuestion);

        }

        private void GetAnswer(string clipboardText)
        {
            //Loop through all questions and answers//
            foreach (question q in questionList)
            {
                if (q._question.StartsWith(clipboardText) || q._question.EndsWith(clipboardText))
                {
                    ShowNotification(q._question, q._answer);
                    break;
                }
            }
            clipboardText = clipboardText.Replace(',', '.');
            //de regex voor de operator
            var regex = new Regex(@"^(?<lhs>\d+(?:[,.]{1}\d)*)[ ]*(?<operator>[+\-\:x\%])[ ]*(?<rhs>\d+(?:[,.]{1}\d)*)$");
            Match match = regex.Match(clipboardText);
            if (!match.Success)
                return; // not valid form (a + b)
                        //de operators
            Dictionary<string, Func<double, double, double>> binaryOperators = new Dictionary<string, Func<double, double, double>>()
            {

               { "+", (a, b) => a + b},
               { "x", (a, b) => a * b },
               { "-", (a, b) => a - b },
               { "%", (a, b) => a / b * 100 },
               { ":", (a, b) => b == 0 ? double.NaN : a / b },
        };
            var lhs = double.Parse(match.Groups["lhs"].Value, CultureInfo.InvariantCulture);
            var rhs = double.Parse(match.Groups["rhs"].Value, CultureInfo.InvariantCulture);
            var answer = binaryOperators[match.Groups["operator"].Value](lhs, rhs);
            System.Windows.Forms.Clipboard.SetText(answer.ToString());
            //Kopieert het resultaat van de som in het clipboard
            //De timer zorgt ervoor dat het resultaat van de som naar x aantal seconde niet meer te plakken is
            // Laat antwoord zien
            ShowNotification(clipboardText, answer.ToString());
        }

        private void ShowNotification(string question, string answer)
        {
            notifyIcon1.Icon = SystemIcons.Exclamation;
            notifyIcon1.BalloonTipTitle = question;
            notifyIcon1.BalloonTipText = answer;
            notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
            notifyIcon1.ShowBalloonTip(1000);
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            {
                const int WM_DRAWCLIPBOARD = 0x308;
                if (m.Msg == WM_DRAWCLIPBOARD)
                {
                    // Kopieert en kijkt of het overeen komt met de list
                    DeleteQuestionTimer.Enabled = false;
                    var text = Clipboard.GetText(TextDataFormat.UnicodeText);
                    DeleteQuestionTimer.Interval = 5000;
                    DeleteQuestionTimer.Enabled = true;
                    // als je gekopieert hebt reset de clipboard en als je voor 5 minuten niet kopieert sluit het programma zich af
                    if (!string.IsNullOrEmpty(text))
                    {
                        InteractiveTimer.Enabled = false;
                        GetAnswer(Clipboard.GetText(TextDataFormat.UnicodeText));
                        InteractiveTimer.Interval = 300000; // reset the timer
                        InteractiveTimer.Enabled = true;   // and start it again
                    }
                }
            }
        }
        private void InteractiveTimer_Tick(object sender, EventArgs e)
        {
            this.Close();
        }
        //Deze timer zorgt ervoor dat het programa zich naar x aantal minuten zichzelf sluit

        private void DeleteQuestionTimer_Tick(object sender, EventArgs e)
        {
            DeleteQuestionTimer.Enabled = false;
            Clipboard.Clear();
        }
    }
}

"Now I want that if I copy a sum that the result can be pasted for 10 seconds." “现在,我希望如果我复制一笔金额,结果可以粘贴10秒钟。”

There is no way to guarantee that. 没有办法保证这一点。 Ever. 曾经 The Clipboard is a single, shared, global resource. 剪贴板是一个共享的全局资源。 Every single programm can modify it. 每个程序都可以对其进行修改。 It is like a shining example on why to never use global. 这就像一个为什么不使用全局的光辉榜样。 Or shared resources. 或共享资源。 At best you will be playing this game: https://devblogs.microsoft.com/oldnewthing/20110310-00/?p=11253 充其量您将玩这个游戏: https : //devblogs.microsoft.com/oldnewthing/20110310-00/?p=11253

While it is listed with Interprocess Communication approaches , it is clearly not a reliable way. 尽管列出了“ 进程间通信”方法 ,但这显然不是可靠的方法。 You should be looking at any of the other solutions for your IPC needs. 您应该考虑满足IPC需求的任何其他解决方案。 You should only use the Clipboard if the user wants to copy or paste via it. 仅当用户要通过剪贴板复制或粘贴时,才应使用剪贴板。 Not as automatic IPC approach. 不作为自动IPC方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM