2 Replies - 1117 Views - Last Post: 31 August 2009 - 05:07 PM Rate Topic: -----

#1 i0va9n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-May 09

Opening a form inside a Thread

Post icon  Posted 31 August 2009 - 03:09 AM

gud day,

i hav my application(client) that hav a thread where in it accepts all the incoming message from a server. after the thread accepts the message. it will open a form that will display the message. but it seems having trouble after i open a form. I cannot connect to my client any more. seems like I cannot establish a connection from my client any more.

The thread looks fine if i don't open any form inside the thread. Is there's a way in opening a form inside a thread and still i can establish my connection with my client.

thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Opening a form inside a Thread

#2 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Opening a form inside a Thread

Posted 31 August 2009 - 05:14 AM

Post any code you have writen
Was This Post Helpful? 0
  • +
  • -

#3 i0va9n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-May 09

Re: Opening a form inside a Thread

Posted 31 August 2009 - 05:07 PM

View Postdebjit625, on 31 Aug, 2009 - 04:14 AM, said:

Post any code you have writen



At first, When i recieve a keyword i can open a form within the thread after to open the form it seems my connection to my server have lost. can somebody help me with this.


Load form
		private void Menu_Load(object sender, EventArgs e)
		{
			strRecieve = "";
			this.Refresh();
			t = new Thread(new ThreadStart(recieveloop));
			t.Start();
		}




Threading
		public void recieveloop()
		{
			if (BluetoothRadio.IsSupported)
			{
				if (BluetoothRadio.PrimaryRadio.Mode != RadioMode.Discoverable)
				{
					DialogResult result = MessageBox.Show("This application needs the bluetooth to be turned on, do you want to continue?", "Bluetooth", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
					if (result == DialogResult.OK)
					{
						BluetoothRadio.PrimaryRadio.Mode = RadioMode.Discoverable;
					}
				}
				else
				{




					listener = new BluetoothListener(service);
					try
					{
						listener.Start();
					}
					catch (SocketException se)
					{
						MessageBox.Show("Error: " + se);
					}
					strRecieve = receiveMessage(Max);
					while (listening)
					{
						if (strRecieve != "")
							strRecieve = receiveMessage(Max);
					}
				}


			}
			else
			{
				MessageBox.Show("Bluetooth Not Found on the Device");
			}

		}


		private string receiveMessage(int bufferlen)
		{
			int bytesRead = 0;
			byte[] Buffer = new byte[Max];

			try
			{
				bluetoothclient = listener.AcceptBluetoothClient();
				stream = bluetoothclient.GetStream();
				bytesRead = stream.Read(Buffer, 0, bufferlen);
				str = System.Text.Encoding.Unicode.GetString(Buffer, 0, bytesRead);

			}
			catch (Exception ex)
			{
				MessageBox.Show("Error: "+ex.Message);
			}
			finally
			{
				if (stream != null)
				{
					stream.Close();
					stream.Dispose();
				}
				if (bluetoothclient != null)
				{
					bluetoothclient.Close();
					bluetoothclient.Dispose();
				}

			}
			str = str.ToLower();
			if (str != "")
			{
				MessageBox.Show("Keyword Recieve from Voice Server: " + str);
				KeywordDetect();
			}
			return str;
		}




Opening of form

		public void KeywordDetect()
		{
			DialogResult result = MessageBox.Show("Keyword Recieved from Voice Server: " + str + System.Environment.NewLine + "Do you want to continue to open " + str + "", "Voice Server", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
			if (result == DialogResult.OK)
			{
				if (str == "police" || str == "hospital" || str == "fire")
				{

					Emergency callfromvoice = new Emergency();
					callfromvoice.ShowDialog();
					callfromvoice.voice = str.Trim();
					this.Hide();
					this.Dispose();

				}
				else if (str == "bluetooth")
				{
					Bluetooth sendbt = new Bluetooth();
					sendbt.ShowDialog();
					this.Hide();
					this.Dispose();
				}
				else if (str == "")
					MessageBox.Show("Empty Keyword");
				else
				{

					bool check = SearchFirst();
					if (check)
					{
						FirstAid openproced = new FirstAid();
						openproced.ProcedHolder = str;
						openproced.ShowDialog();
						this.Hide();
						this.Dispose();
					}
					else
						MessageBox.Show("No such Keyword Identified");

				}
			}
		}


This post has been edited by i0va9n: 31 August 2009 - 06:17 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1