2012-07-21 5 views
3

저는 C#에 익숙하지 않습니다.버튼을 사용하여 마우스 클릭 루프 (C#)

RS232를 통해 가정용 망원경 마운트에 코드를 전송하는 프로그램을 작성 중입니다. 나는 순간이

이슈 희망 매우 간단합니다 (나를 위해 그러나 매우 어렵!) 예를 들어

은 마우스 왼쪽 버튼을 누르고 때 루프를 실행하려면, 내가 버튼이 있다고 (232 데이터의 연속적인 스트림이 될 것입니다), 마우스 왼쪽 버튼을 놓으면 루프가 멈추고 다른 코드 행을 실행해야합니다.

나는 진심으로 내가 제공 한 정보가 충분하고 누군가가 (대답이 날 믿어 내가 인터넷을 검색 한!)를 따라

많은 감사를 도와 친절하게도 바랍니다.

답변

3

버튼의 MouseDown 및 MouseUp 이벤트에 후크합니다. MouseDown 이벤트는 스레드를 생성하거나 루프 실행을 시작하기 위해 스레드에 신호를 보내야합니다. MouseUp 이벤트는 루프 실행을 중지하기 위해 스레드에 신호를 보내야합니다. 이 같은

뭔가 :

public class InterruptibleLoop 
{ 
    private volatile bool stopLoop; 
    private Thread loopThread; 

    public void Start() { 
     // If the thread is already running, do nothing. 
     if (loopThread != null) { 
      return; 
     } 

     // Reset the "stop loop" signal. 
     stopLoop = false; 

     // Create and start the new thread. 
     loopThread = new Thread(LoopBody); 
     loopThread.Start(); 
    } 

    public void Stop() { 
     // If the thread is not running, do nothing. 
     if (loopThread == null) { 
      return; 
     } 

     // Signal to the thread that it should stop looping. 
     stopLoop = true; 

     // Wait for the thread to terminate. 
     loopThread.Join(); 

     loopThread = null; 
    } 

    private void LoopBody() { 
     while (!stopLoop) { 
      // Do your work here 
     } 
    } 
} 
+0

이 접근법은 잘 작동하는 것처럼 보입니다. 그리고 그것은 충분히 간단합니다. 마우스를 아래로 내릴 때 스레드를 시작하고 마우스를 위로 올리면 멈 춥니 다. –

+0

나는 마우스 다운시에만 스레드를 멈추지 만 개체가 제대로 처리되거나 나중에 완료 될 때까지 스레드가 계속 살아 있기 때문에 API를 약간 복잡하게 만든다. 이것은 GUI 응용 프로그램이므로 상수 스레드 생성 및 제거가 실제로 성능에 영향을 주어서는 안됩니다. – cdhowie

0

방법 1 : 먼저 당신이 데이터를 보낼 주파수로 설정된 간격 타이머를 만들 수 있습니다. 그리고 틱 이벤트에서 데이터를 보냅니다. 버튼의 마우스 다운 이벤트 및 버튼의 마우스 업 이벤트에 대한 이벤트를 만듭니다. 마우스 다운 이벤트에서 타이머를 시작하십시오. 마우스 업 이벤트에서 타이머를 중지하십시오.

방법 2 : 마우스 다운 이벤트에서 타이머를 시작하는 대신 데이터 전송의 연속 루프를 수행하는 새 스레드를 시작하십시오. 마우스 업 이벤트에서 스레드를 중지하십시오.

+0

사전에 당신의 친절한 답장을 보내 주셔서 감사합니다 :) 나는 일하고보고 할 것입니다. :) – Macko

+0

안녕하세요 모두, 많은 도움을 주신 데 많은 감사를드립니다. 결국 타이머를 사용하기로 결정했는데, 지옥 코드로 아주 추악합니다. 그게 당신을 용서해 줄 수 있도록 나를 용서해주십시오.) 아직도 내 길을 배우고 있지만 좋은 시간을 보내고, 내 스펙트럼과 아미가에 다시 아이를 생각 나게 해. – Macko

0
namespace Scope_Project_Ver_2 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent();    
      // *** Output data timer *** 
      otimer.Interval = 50; 
      // otimer.Interval = isendFreq; 
      otimer.Tick += new EventHandler(otimer_Tick); 
      // *** Input data timer *** 
      // itimer.Interval = 601;      <- to be unchecked 
      // itimer.Tick += new EventHandler(itimer_Tick); <- to be unchecked 
     } 

     public int b1,b2,b3,b4,b5; 
     public string sb1, sb2, sb3, sb4, sb5; 
     public int ivorSpeed; 
     public string svorSpeed; 
     public int ihorSpeed; 
     public string shorSpeed; 
     public int isendFreq; 


     private void sendDataB_Click(object sender, MouseEventArgs e) 
     { 
      if (sendDataCB.Checked) 
      { 
       sendDataCB.Checked = false; 
       if (otimer.Enabled) 
        otimer.Stop(); 
      } 
      else 
      { 
       sendDataCB.Checked = true; 
       if (!otimer.Enabled) 
        otimer.Start(); 
      } 
     } 


     void otimer_Tick(object sender, EventArgs e) 
     { 
      SerialPort port = new SerialPort(
      "COM1", 9600, Parity.None, 8, StopBits.One); 
      port.Open(); 
      port.Write("Q"); //       Q 
      port.Write(sb1); //       1 
      port.Write(sb2); //       2 
      // Binary stuff // Ver Speed Binary  3   
      byte[] bverbinary = new byte[1]; 
      byte verbinary = 0; 
      verbinary = Byte.Parse(svorSpeed); 
      bverbinary[0] = verbinary; 
      port.Write(bverbinary, 0, bverbinary.Length); 
      // End of Binary stuff for Ver Speed 
      // Binary stuff // Hor Speed Binary   4 
      byte[] bhorbinary = new byte[1]; 
      byte horbinary = 0; 
      horbinary = Byte.Parse(shorSpeed); 
      bhorbinary[0] = horbinary; 
      port.Write(bhorbinary, 0, bhorbinary.Length); 
      port.Write(sb5); // Movement    5 
      port.Close(); 
     } 


     private void vorSpeed_ValueChanged(object sender, EventArgs e) 
     { 
      // MessageBox.Show((this.vorSpeed.Value).ToString()); 
      this.Text = "You changed the Vertical Speed to " + vorSpeed.Value; 
      ivorSpeed = (int)vorSpeed.Value; 
      svorSpeed = ivorSpeed.ToString(); 
     } 

     private void horSpeed_ValueChanged(object sender, EventArgs e) 
     { 
      // MessageBox.Show((this.horSpeed.Value).ToString()); 
      this.Text = "You changed the Horizontal Speed to " + horSpeed.Value; 
      ihorSpeed = (int)horSpeed.Value; 
      shorSpeed = ihorSpeed.ToString(); 
     } 

     private void scopeUp_MouseDown(object sender, MouseEventArgs e) // Scope Up On 
     { 
      b1 = 2; 
      b2 = 0; 
      b5 = 1; 
      sb1 = b1.ToString(); 
      sb2 = b2.ToString(); 
      sb3 = b3.ToString(); 
      sb4 = b4.ToString(); 
      sb5 = b5.ToString(); 
     } 

     private void scopeUp_MouseUp(object sender, MouseEventArgs e) // Scope Up Off 
     { 

     } 

     private void scopeRight_MouseDown(object sender, MouseEventArgs e) 
     { 
      b1 = 1; 
      b2 = 2; 
      b5 = 1; 
      sb1 = b1.ToString(); 
      sb2 = b2.ToString(); 
      sb3 = b3.ToString(); 
      sb4 = b4.ToString(); 
      sb5 = b5.ToString(); 
     } 

     private void scopeRight_MouseUp(object sender, MouseEventArgs e) 
     { 

     } 

     private void scopeDown_MouseDown(object sender, MouseEventArgs e) 
     { 
      b1 = 2; 
      b2 = 1; 
      b5 = 1; 
      sb1 = b1.ToString(); 
      sb2 = b2.ToString(); 
      sb3 = b3.ToString(); 
      sb4 = b4.ToString(); 
      sb5 = b5.ToString(); 
     } 

     private void scopeDown_MouseUp(object sender, MouseEventArgs e) 
     { 

     } 

     private void scopeLeft_MouseDown(object sender, MouseEventArgs e) 
     { 
      b1 = 0; 
      b2 = 2; 
      b5 = 1; 
      sb1 = b1.ToString(); 
      sb2 = b2.ToString(); 
      sb3 = b3.ToString(); 
      sb4 = b4.ToString(); 
      sb5 = b5.ToString(); 
     } 

     private void scopeLeft_MouseUp(object sender, MouseEventArgs e) 
     { 

     } 

     private void scopeStop_Click(object sender, EventArgs e) 
     { 
      b1 = 0; 
      b2 = 0; 
      b5 = 0; 
      sb1 = b1.ToString(); 
      sb2 = b2.ToString(); 
      sb3 = b3.ToString(); 
      sb4 = b4.ToString(); 
      sb5 = b5.ToString(); 
     } 

     private void sendFreq_ValueChanged(object sender, EventArgs e) 
     { 
      this.Text = "You changed the send Freq to " + sendFreq.Value + " m/s"; 
      isendFreq = (int)sendFreq.Value; 
     } 
    } 
}