Hjælp med Threads i C#

Tags:    c#

I'm building a drag n' drop program that will allow a user to drag n drop an item (like a button) from one control to another.

Currently i have a problem managing threads, or rather parsing data between them.

My problem is this. I need to sample the mouse's x and y coordinates for each control so that it will drop the item at the correct place in the control. The reason im using threading is that once i start dragging an object (button) the sampling of the x and y coordinates pauses. Thus i need to run it in another thread.

My problem is that i can't seem to get _GetLocalMouseCoordinates to run in a thread of its own, i get this error

C:\\Documents and Settings\\My Documents\\Visual Studio Projects\\DragNDrop\\DragDrop.cs(359): Method 'DragNDrop.Form1._GetLocalMouseCoordinates(object, System.Windows.Forms.MouseEventArgs)' does not match delegate 'void System.Threading.ThreadStart()'

The code is below, i have only pasted the "vital" part though. I hope that someone cna help. Thanks alot in advance!

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace DragNDrop
{
///
/// Summary description for Form1.
///
/// Huske/tænke liste
///
/// 1. Threading hvor hurtig opdaterings tid kan formen klare uden at choke?
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel pnlDrag;
private System.Windows.Forms.Panel pnlDrop;
private System.Windows.Forms.Label lblDrag;
private System.Windows.Forms.Label lblDrop;
private System.Windows.Forms.Button btnDrag;
private System.Windows.Forms.Button btnPush;
private System.Windows.Forms.StatusBar statusBarGlobal;
private System.Windows.Forms.StatusBar statusBarLocalDrop;
private System.Windows.Forms.StatusBar statusBarLocalDrag;

// My generated variables etc.

public int iXCoorDrag;
public int iYCoorDrag;
public int iXCoorDrop;
public int iYCoorDrop;
public int iXCoorGlobal;
public int iYCoorGlobal;

public Thread GlobalMouseThread;
public Thread LocalMouseThread;
public bool bStopthread;

///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

public void pnlDrag_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
// GetGlobalMouseCoordinates();

statusBarLocalDrag.Text = "DRAG - Local X: " + e.X.ToString() + " Y: " + e.Y.ToString();
iXCoorDrag = Convert.ToInt32(e.X.ToString());
iYCoorDrag = Convert.ToInt32(e.Y.ToString());
}

private void pnlDrop_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
// GetGlobalMouseCoordinates();

statusBarLocalDrop.Text = "DROP - Local X: " + e.X.ToString() + " Y: " + e.Y.ToString();
iXCoorDrop = Convert.ToInt32(e.X.ToString());
iYCoorDrop = Convert.ToInt32(e.Y.ToString());
}

private void btnDrag_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
// GetGlobalMouseCoordinates();
}

private void btnPush_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
// GetGlobalMouseCoordinates();
}

private void btnDrag_Click(object sender, System.EventArgs e)
{

}

private void btnPush_Click(object sender, System.EventArgs e)
{
Button btnClick = new System.Windows.Forms.Button();
btnClick.Location = new System.Drawing.Point(0, 0);
btnClick.Name = "btnClick";
btnClick.TabIndex = 1;
btnClick.Width = 100;
btnClick.Height = 50;
btnClick.Text = "BUTTON CLICKED";
btnClick.Visible = true;

pnlDrop.Controls.Add(btnClick);
}

private void btnDrag_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right) return;
if (e.Button == MouseButtons.Middle) return;


}

private void btnDrag_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
btnDrag.Location = new System.Drawing.Point(iXCoorDrag, iYCoorDrag);
MessageBox.Show("Moved to: " + iXCoorDrag + ", " + iYCoorDrag);

}

private void pnlDrag_DragLeave(object sender, System.EventArgs e)
{

}

private void pnlDrop_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{

MessageBox.Show("DRAG ENTER");
}

private void pnlDrop_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{

}

private void GetGlobalMouseCoordinates()
{
statusBarGlobal.Text = "Global X: " + Form1.MousePosition.X.ToString() + " Y: " + Form1.MousePosition.Y.ToString();
iXCoorGlobal = Form1.MousePosition.X;
iYCoorGlobal = Form1.MousePosition.Y;
}

private void _GetGlobalMouseCoordinates()
{
bStopthread = true;

while(bStopthread)
{
GetGlobalMouseCoordinates();
Thread.Sleep(100);
}
}

private void _GetLocalMouseCoordinates(object sender, System.Windows.Forms.MouseEventArgs e)
{
bStopthread = true;

while(bStopthread)
{

statusBarLocalDrag.Text = "DRAG - Local X: " + e.X.ToString() + " Y: " + e.Y.ToString();

iXCoorDrag = Convert.ToInt32(e.X.ToString());
iYCoorDrag = Convert.ToInt32(e.Y.ToString());

Thread.Sleep(100);
}
}

public void Form1_Load(object sender, System.EventArgs e)
{
ThreadStart myGlobalStarter = new ThreadStart(this._GetGlobalMouseCoordinates);
GlobalMouseThread = new Thread(myGlobalStarter);
GlobalMouseThread.Start();

ThreadStart myLocalStarter = new ThreadStart(this._GetLocalMouseCoordinates);
LocalMouseThread = new Thread(myLocalStarter);
LocalMouseThread.Start();
}

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
bStopthread = false;
}

catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}



1 svar postet i denne tråd vises herunder
1 indlæg har modtaget i alt 1 karma
Sorter efter stemmer Sorter efter dato
Din _GetMouse... fidus må vist ikke tage parametre med ind i parameter linien... så vidt jeg husker...

(¯`·._.·[Brian Hvarregaard]·._.·´¯)
Praesto et Persto



t