简体   繁体   中英

AutoResetEvent on C# and calling a Form

I am starting 2 different threads on a C# program. The first is a form which should sniff the clipboard and the second is starting a java Program which is writing into the clipboard. The calling looks like this:

        new Thread(() => StartClipListening()).Start();
        new Thread(() => executeScripts(path2Sikuli, path2Scripts, SikVars)).Start();
        SuccFailEvent.WaitOne();
        SuccFailEvent.Reset();

The second thread is working fine, but the first is being called and write after that closed before even starting the Clipboard sniffing. It looks like this:

    private void StartClipListening()
    {
        var clites = new CBForm();
        clites.Start_Lintening(this);
        clites.Show();
    }

In the Form I am doing the following stuff:

public void Start_Lintening(TradingExecution trex)
    {
        this.trex = trex;
        //this.are = are;
        AddClipboardFormatListener(this.Handle);
    }

    const int WM_CLIPBOARDUPDATE = 0x31D;
    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_CLIPBOARDUPDATE:
                IDataObject iData = Clipboard.GetDataObject();
                if (iData.GetDataPresent(DataFormats.Text))
                {
                    label1.Text = (string)iData.GetData(DataFormats.Text);
                    trex.ClipboardMonitor_OnClipboardChange (label1.Text);
                    Stop_listening();
                    this.Close(); 
                }
                break;


            default:
                base.WndProc(ref m);
                break;
        }
    }

    public void Stop_listening()
    {
        RemoveClipboardFormatListener(this.Handle);
    }

and the called method is this one:

        public void ClipboardMonitor_OnClipboardChange(string data)
    {          
        var ClipboardText = data;
        string[] dataInSlices = ClipboardText.Split(';');
        try
        {
            Clipboard.Clear();
        }
        catch (Exception)
        {
        }

        if (dataInSlices.Count() > 1)
        {
            if (dataInSlices[1] == "Success")
            {
                logger.Info("Sccess executing Sikuli");
                SuccessExecute = true;
                FailureStep = 0;
                Failureval = dataInSlices[2];
                SuccFailEvent.Set();
            }
            else if (dataInSlices[1] == "Failure")
            {
                logger.Info("Failure executing Sikuli");
                try
                {
                    FailureStep = Int32.Parse(dataInSlices[0]);
                }
                catch (Exception)
                {

                }
                SuccessExecute = false;
                Failureval = dataInSlices[2];
                SuccFailEvent.Set();
            }
        }
    }

Thanks in advance for your help!

i've solved it now on the following way:

    private void StartClipListening()
    {
        var clites = new CBForm();
        clites.Start_Lintening(this);
        clites.Show();
    }
    private void StarttwoTasks(string path2Sikuli, string path2Scripts, SikuliVariables SikVars)
    {
        StartClipListening();           
        new Thread(() => executeScripts(path2Sikuli, path2Scripts, SikVars)).Start();
        new Thread(() => waitforthat###()).Start();
    }
    private void waitforthat###()
    {
        SuccFailEvent.WaitOne();
        SuccFailEvent.Reset();
    }

Now i have a polling problem but thats another story. Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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