Sunday 15 February 2015

c# - How to exit a process by catching a mouse click? -


I'm new to C #, just starting to write some code. There is something in my mind, but before moving ahead, some help is needed on this matter. How do you get out of the running process by detecting the mouse click? I wrote a few lines but when compiled and running, the mouse click has no effect. Can anyone please help me by looking at me? Here are my lines ...

  using the system; Using System.Collections.Generic; Using System.ComponentModel; Using System.Data; Using System.Drawing; Using System.Linq; Using System.Text; Using System.Windows.Forms; Namespot Graphic_test_1 {Public Partial Category Form 1: Form {Public Fixed Single Lim_x, lim_y; X & amp; // Limitations; Y Public Stable Int Daily = 45; System.Drawing.Pen myPen = New System. Dropping Pen (system, drawing collar blue); // Color color system. Drawing Graphics canvas; Public boolean clik = false; // Start Public String Mistress; Public Form 1 () {Initialization (); } Secure Override Zero OnMouseClick (MouseEventArgs e) {base.OnMouseClick (e); Clik = true; Lim_x = e.x; Lim_y = e.Y; } Private Zero btgo_Click (Object Sender, EventArgs E) // Drawing Start [GO] {canvas = this.CreateGraphics (); Btgo.Enabled = false; MessageBox.Show ("Checking the canvas borders. \ R \ n" + "Click anywhere to stop X position", "see coordination", message box button. OK, message box icon. ; Bitigo. Person = false; Btend.Enabled = false; MyPen.Color = System.Drawing.Color.Red; // color of color myPen.Width = 2; // Pen width system. Drawing.font drift = new system Drawing.fonts ("aerial", 10); // Graphics Font System Drawing Solidbrish MyBrush = New System Drawing Solidbrish (system, drawing collar black); // color for font system Drawing Color background; Background = this.BackColor; Lim_x = 0; // Start {canvas. Drawline (MyPain, 0, 200, Lim_X, 200); Mystring = "Current X =" + lim_x.ToString (); Mybrush.Color = System.Drawing.Color.Black; Canvas.drustring (mestring, drops, mice, 351, 334); System.Threading.Thread.Sleep (dly); Mybrush.Color = Background; // Use background color canvas. Filtrategle (mrbush, new rectangle (350, 333, 500, 353)); If (click) {mybrush.color = background; // Use background color canvas. Filtrategle (mrbush, new rectangle (350, 333, 500, 353)); Mystring = "Current X =" + lim_x.ToString (); Mybrush.Color = System.Drawing.Color.Black; Canvas.drustring (mestring, drops, mice, 351, 334); MessageBox.Show ("Last Position in X" - lim_x.ToString (), "Mouse click detected", MessageBoxButtons.OK, MessageBoxIcon.Stop); break; } Other lim_x ++; } While (lim_x <611); MyPen.Dispose (); // Release the pen Marybrush. Dizzy (); // Leave the brush canvas. Dies: (); // release canvas btend.Enabled = true; Btend.Focus (); } Private Zero btend_Click (Object Sender, EventArgs E) // Excluding Program [END] {Removal}; // program ends}}   

}

if I understand well, so you are trying to stop the ongoing process (such as BTOG click event).

To do this, you should use an individual thread to execute the process.

Why thread?

Your application will run two separate processes:

  1. Main threads
  2. second thread < / Code>

    Therefore, it is possible to identify the "mouse click" when you are running the second thread .

    Example I have a button and a label . I want to click on button1 , create and start thread it will loop thread 10000 time and Label1 will modify the text. But when I click on the label, the loop will close:

      using the system; Using System.Drawing; Using System.Windows.Forms; Using System.Threading; Public Partial Class Test: Form {thread thread; Public examination () {Initial group (); } Zero button 1_Click (object sender, event events e) {thread = new thread (new threadstart (start-thread)); Thread.Start (); } Private Zero StartThread () {for (int i = 0; i & lt; 1000000; i ++) {Thread.Sleep (1000); Label1.Invoke ((MethodInvoker) ((= = gt; Label1.Text = i.ToString ())); // See more info section, I will post a link about it }} Zero Label 1_Click (Object Sender, Event Argus E) {thread. Ebert (); }}   

    More info:

    1. You should read about how to close a thread properly .

    2. Enter the method.

No comments:

Post a Comment