Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:35:09+00:00 2026-06-15T08:35:09+00:00

I Wrote a short program in C# that monitors the clipboard. When a certain

  • 0

I Wrote a short program in C# that monitors the clipboard. When a certain string enters the clipboard, a program must be opened with Process.Start (depending on the string). Everything works fine, but sometimes the application is being opened twice. I Don’t know why this is happening.

namespace ClipboardMonitor {
    public class Form1 : System.Windows.Forms.Form {
        [DllImport("User32.dll")]
        protected static extern int SetClipboardViewer(int hWndNewViewer);
        [DllImport("User32.dll", CharSet=CharSet.Auto)]
        public static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);
        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
        [DllImport("kernel32.dll")]
        static extern IntPtr GetConsoleWindow();
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        const int SW_HIDE = 0;
        const int SW_SHOW = 5;
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        IntPtr nextClipboardViewer;
        private System.ComponentModel.Container components = null;

        public Form1() {
            InitializeComponent();
            nextClipboardViewer = (IntPtr)SetClipboardViewer((int) this.Handle);
            var handle = GetConsoleWindow();
            ShowWindow(handle, SW_HIDE);
        }
        protected override void Dispose( bool disposing ) {
            ChangeClipboardChain(this.Handle, nextClipboardViewer);
            if( disposing ) {
                if (components != null) {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        private void InitializeComponent() {
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            this.Name = "Form1";
        }

        [STAThread]
        static void Main()  {
            Application.Run(new Form1());
        }

        protected override void WndProc(ref System.Windows.Forms.Message m) {
            const int WM_DRAWCLIPBOARD = 0x308;
            const int WM_CHANGECBCHAIN = 0x030D;

            switch(m.Msg) {
                case WM_DRAWCLIPBOARD:
                    DisplayClipboardData();
                    SendMessage(nextClipboardViewer, m.Msg, m.WParam, m.LParam);
                    break;
                case WM_CHANGECBCHAIN:
                    if (m.WParam == nextClipboardViewer)
                        nextClipboardViewer = m.LParam;
                    else
                        SendMessage(nextClipboardViewer, m.Msg, m.WParam, m.LParam);
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }

        void DisplayClipboardData() {
            Thread.Sleep(500);
            try {
                IDataObject iData = new DataObject();  
                iData = Clipboard.GetDataObject();
                 if (iData.GetDataPresent(DataFormats.Text)) {
                    string path = iData.GetData(DataFormats.Text).ToString();
                    string[] words = path.Split('_');
                    if (words[0] == "startLO") {
                            ProcessStartInfo info = new ProcessStartInfo(words[1]);
                            Process p = Process.Start(info);
                    }
                } else {
                    // We doen niets.
                }
            }
            catch(Exception e) {
                MessageBox.Show(e.ToString());
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:35:11+00:00Added an answer on June 15, 2026 at 8:35 am

    One explanation would be that there are multiple clipboard events happening in rapid succession. This is fairly common. You can defend against this with a “settle time” delay. i.e. instead of reacting right away, set a timer or create a thread to handle it “in a little while”. As more events come in, keep deferring the settletime. when the settletime finally expires, the timer or thread is allowed to actually run your program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a very short program that parses a program using python and converts
I wrote this short program which has a tiny GUI. Its supposed to allow
To help me better understand lambda I wrote this short snippet that rotates and
I have a program that I use as an alt-tab replacement. I wrote it
I wrote a short script that never terminates. This script continuously generates output that
I'm attempting to write a short mini-program in Python that plays around with force-based
I recently wrote some code that uses the same unsigned short to store two
I have a problem for a university lab; Write a short program that outputs
I recently wrote a small number-crunching program that basically loops over an N-dimensional grid
Long story short, I have a Java process that reads and writes data to/from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.