C# Thread Probleme mit Variablen

Sicherlich wirst du mich jetzt für sehr dumm halten, ich kann jedoch mit deiner Hilfe nicht so viel anfangen.
An welcher Stelle von den beiden Klassen, muß ich denn jetzt was einfügen?
 
Um es etwas besser zu formulieren, habe ich beide Klassen mal angepaßt:

Hier die SerialKlasse:
Code:
namespace Bootloader
{
	public class SerialClassThread
	{
		public System.IO.Ports.SerialPort serialPort1;
		
		private SerialClassThread serial_thread = new SerialClassThread();
		private Thread Serial_Thread;			
		private string _Serial_tx_send_string;
		private bool _SerialStopRequest = true;
		private bool _SerialTranmitRequest = false;
		
		public bool StopRequest
		{
			set { serial_thread._SerialStopRequest = value; }
		}
		public bool TransmitRequest
		{
			set { serial_thread._SerialTranmitRequest = value; }
		}
		public string SerialStringRequest 
		{
			set { serial_thread._Serial_tx_send_string = value; }
		}

		public void StartThread(string to_open_comport,int to_open_baudrate) 
		{
			if ( Serial_Thread == null || ( Serial_Thread != null && !Serial_Thread.IsAlive ) ) 
			{
				serialPort1 = new SerialPort(to_open_comport, 9600, Parity.None, 8, StopBits.One);
				serialPort1.Open();
				serial_thread._SerialStopRequest = false;
				serial_thread._SerialTranmitRequest = false;
      		
				Serial_Thread = new Thread( new ThreadStart(SerialLoop) );
				Serial_Thread.Start();
				while (!Serial_Thread.IsAlive);
			}
		}
		protected void SerialLoop() 
		{
			while( !serial_thread._SerialStopRequest )
			{
				if(serial_thread._SerialTranmitRequest)
				{
					serialPort1.Write(serial_thread._Serial_tx_send_string);
					serial_thread._SerialTranmitRequest = false;
				}
				Thread.Sleep(10);
			}
			serialPort1.Close();
		}
	}	
}

und hier die TimerKlasse:

Code:
namespace Bootloader
{
	public class TimerClassThread
	{
		public SerialClassThread timer_serial_rx_tx_thd = null;

                private TimerClassThread timer_thread = new TimerClassThread();
		private Thread Timer_Thread;
		private bool _TimerStopRequest = true;	
		
		public bool TimerThreadStopRequest 
		{
			set { _TimerStopRequest = value; }
		}
		public void StartThread() 
		{
			if ( Timer_Thread == null || ( Timer_Thread != null && !Timer_Thread.IsAlive ) ) 
			{
				_TimerStopRequest = false;
				Timer_Thread = new Thread( new ThreadStart(TimerLoop) );
				Timer_Thread.Start();
				while (!Timer_Thread.IsAlive);
			}
		}

		private static Object lockvar = ""; // Lock-Variable
		protected void TimerLoop() 
		{

			while( !_TimerStopRequest )
			{
				
					if(timer_serial_rx_tx_thd != null)
					{
						lock(lockvar)
						{
							timer_serial_rx_tx_thd.SerialStringRequest = "Hallo\r";
							timer_serial_rx_tx_thd.TransmitRequest = true;
						}
					}
					else
					{
						if(timer_serial_rx_tx_thd == null)
						{
							timer_serial_rx_tx_thd.StartThread("COM6",9600);
						}	
					}
				
		       	Thread.Sleep(500);
			}
		}
	}
}

Meine Frage ist: Ich muß doch noch irgendwie "timer_serial_rx_tx_thd" ein Objekt zuweisen.

Starten sollten die beiden Threads nach z.B. einem Tastendruck in der MainForm.
hier die MainForm:
Code:
namespace Bootloader
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public SerialClassThread main_serial_rx_tx_thd = null;
		public TimerClassThread main_timer_thd = null;
	

		public MainForm()
		{
			InitializeComponent();

		}
	
		void Button1Click(object sender, EventArgs e)
		{
			main_serial_rx_tx_thd.StartThread("COM6",9600);
			main_timer_thd.StartThread();
		}
	}
}
 
Hallo,

ich habe den Code etwas modifiziert, so dass es jetzt laufen sollte (sofern ich keinen Bug eingebaut habe :-) ). Die Zuweisung von "timer_serial_rx_tx_th" erfolgt in "StartThread" der Timer-Klasse. Das hättest du aber in meinem letzten Post schon sehen müssen.
Innerhalb von "SerialClassThread" ein Objekt dieser Klasse erzeugen und dann damit zu arbeiten, ist überflüssig.

Gruß
MCoder

C#:
namespace Bootloader
{
	public class SerialClassThread
	{
		public System.IO.Ports.SerialPort serialPort1;

		private Thread Serial_Thread;			
		private string _Serial_tx_send_string;
		private bool _SerialStopRequest = true;
		private bool _SerialTranmitRequest = false;
		
		public bool StopRequest
		{
			set { _SerialStopRequest = value; }
		}
		public bool TransmitRequest
		{
			set { _SerialTranmitRequest = value; }
		}
		public string SerialStringRequest 
		{
			set { _Serial_tx_send_string = value; }
		}

		public void StartThread(string to_open_comport,int to_open_baudrate) 
		{
			if ( Serial_Thread == null || ( Serial_Thread != null && !Serial_Thread.IsAlive ) ) 
			{
				serialPort1 = new SerialPort(to_open_comport, 9600, Parity.None, 8, StopBits.One);
				serialPort1.Open();
				_SerialStopRequest = false;
				_SerialTranmitRequest = false;
      		
				Serial_Thread = new Thread( new ThreadStart(SerialLoop) );
				Serial_Thread.Start();
				while (!Serial_Thread.IsAlive);
			}
		}
		protected void SerialLoop() 
		{
			while( !_SerialStopRequest )
			{
				if(_SerialTranmitRequest)
				{
					serialPort1.Write(_Serial_tx_send_string);
					_SerialTranmitRequest = false;
				}
				Thread.Sleep(10);
			}
			serialPort1.Close();
		}
	}	
}

///////////////////////////////////////////////////////////////////////////////

namespace Bootloader
{
	public class TimerClassThread
	{
		public SerialClassThread timer_serial_rx_tx_thd = null;
        
		private Thread Timer_Thread;
		private bool _TimerStopRequest = true;	
        private static Object lockvar = new Object();  // Lock-Variable
		
		public bool TimerThreadStopRequest 
		{
			set { _TimerStopRequest = value; }
		}
		public void StartThread(SerialClassThread main_serial_rx_tx_thd) 
		{
            timer_serial_rx_tx_thd = main_serial_rx_tx_thd;
        
			if ( Timer_Thread == null || ( Timer_Thread != null && !Timer_Thread.IsAlive ) ) 
			{
				_TimerStopRequest = false;
				Timer_Thread = new Thread( new ThreadStart(TimerLoop) );
				Timer_Thread.Start();
				while (!Timer_Thread.IsAlive);
			}
		}
        
		protected void TimerLoop() 
		{

			while( !_TimerStopRequest )
			{
				
                if(timer_serial_rx_tx_thd != null)
                {
                    lock(lockvar)
                    {
                        timer_serial_rx_tx_thd.SerialStringRequest = "Hallo\r";
                        timer_serial_rx_tx_thd.TransmitRequest = true;
                    }
                }
				
		       	Thread.Sleep(500);
			}
		}
	}
}

///////////////////////////////////////////////////////////////////////////////

namespace Bootloader
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public SerialClassThread main_serial_rx_tx_thd = null;
		public TimerClassThread main_timer_thd = null;
	

		public MainForm()
		{
			InitializeComponent();

		}
	
		void Button1Click(object sender, EventArgs e)
		{
			main_serial_rx_tx_thd.StartThread("COM6",9600);
			main_timer_thd.StartThread(main_serial_rx_tx_thd);
		}
	}
}
 
Zuletzt bearbeitet von einem Moderator:
Danke nochmals für Deine Hilfe.
Habe jetzt alles hinbekommen und es funktioniert auch.
Mußte nur nochmals in Ruhe über Deinen Tip nachdenken.
Vielen Dank nochmals
 
Zurück