Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Cross-Threading Operation Invalid

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
nbourg8
How do I cheat?
Reputation: 0

Joined: 12 Dec 2007
Posts: 7

PostPosted: Thu Dec 27, 2007 4:41 pm    Post subject: Cross-Threading Operation Invalid Reply with quote

I would like to setup a C# program that sets up 3 hotkeys(U,I,O). When U is pressed, it enters a loop which can be paused by I, and application by O. Thus, when U is pressed, the textbox begins changing text and when I is pressed, it pauses. My problem is obvious when you try to debug this code.

Here is my feeble attempt:
Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace WindowsApplication1
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        static Thread mainThread;
        static Thread firstThread;
        static Thread secondThread;
        private const int MYKEYID = 0;      //Ctrl+Shift+U  start
        private const int MYKEYID1 = 1;     //Ctrl+Shift+I  pause
        private const int MYKEYID2 = 2;     //Ctrl+Shift+O  disable threads
        private int stopcode = 0;
        private const bool unl = true;
        public Form1()
        {
            mainThread = Thread.CurrentThread;
            firstThread = new Thread(new ThreadStart(Fun1));
            secondThread = new Thread(new ThreadStart(Fun2));
            firstThread.Start();
            secondThread.Start();
            InitializeComponent();
            RegisterHotKey(this.Handle, MYKEYID, MOD_CONTROL + MOD_SHIFT, Keys.U);
            RegisterHotKey(this.Handle, MYKEYID1, MOD_CONTROL + MOD_SHIFT, Keys.I);
            RegisterHotKey(this.Handle, MYKEYID2, MOD_CONTROL + MOD_SHIFT, Keys.O);
            this.FormClosing += this.Form1_FormClosing;
        }
        public void Fun1()
        {
            int x = 0;
            while (unl)
            {
                if (stopcode == 1)
                {
                    SendKeys.SendWait(". ");
                    Thread.Sleep(1000);
                    textBox1.Text = x.ToString();
                    x++;
                }

            }
        }
        public void Fun2()
        {
            while (unl)
            {
                if (stopcode == 0)
                {
                    Thread.Sleep(10);
                }
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            UnregisterHotKey(this.Handle, MYKEYID);
            UnregisterHotKey(this.Handle, MYKEYID1);
            UnregisterHotKey(this.Handle, MYKEYID2);
            if (firstThread.IsAlive)
                firstThread.Abort();
            if (secondThread.IsAlive)
                secondThread.Abort();
        }
        protected override void WndProc(ref Message m)
        {
            int hotkeyID = (int)m.WParam;
            base.WndProc(ref m);
            if (m.Msg == WM_HOTKEY)
                switch (hotkeyID)
                {
                    case 0:
                        stopcode = 1;
                        break;
                    case 1:
                        stopcode = 0;
                        break;
                    case 2:
                        Application.Exit();
                        break;
                }
        }
        // P/Invoke declarations
        private const int WM_HOTKEY = 0x312;
        private const int MOD_ALT = 1;
        private const int MOD_CONTROL = 2;
        private const int MOD_SHIFT = 4;
        [DllImport("user32.dll")]
        private static extern int RegisterHotKey(IntPtr hWnd, int id, int modifier, Keys vk);
        [DllImport("user32.dll")]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}



The Extension 'zip' was deactivated by an board admin, therefore this Attachment is not displayed.

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites