deadlock (when changing in between tabs)

  • Thread starter Thread starter Luk Vloemans
  • Start date Start date
L

Luk Vloemans

Hey Everyone,


I'm writing software to communicate with a GPS to a PDA.

So far, I've been quite lucky. All the data from the GPS is succefully
being read into my interface, and succesfully parsed.

However, I recently added an extra "settings"-tab to my program to spice
up usability.

The following problem occurs.

When I start the GPS and start playing with the tabs (going back and
foward between the "Main"-tab and the "Settings"-tab) my program hangs
itself up. (it might happen the first time, or after 10 times)

I've got no explenation for this..

I've got one thread updating all the textbox-fields in my "Main"-tab and
one thread reading all data from the PDA's COM-port (GPS). (plus the
main thread)

Should I stop updating the textbox-fields in my "Main"-tab when I check
out my "Settings"-tab ? (I found another tool made for a GPS on a PDA
and that tool didn't stop updating.. so I don't think I should stop
either..)

Anyone got a clue why this deadlock occurs?

PS: If I don't touch the tabs, the program works perfectly.

Thx for any help!

|Luk Vloemans
|IT student
 
Luk,

If you're updating the textbox data directly from a secondary thread, that could
well be your problem as this is not supported in any version of Windows. If
you're using a delegate to actually do the update on the same thread that the
form was created on, you should be ok.
 
When accessing control members from outside of the main thread that created
it, you need to call it via ControlInvoke in a thread safe manner. I have
attached a sample that I wrote for demonstration purpose, please use your
discretion.

Thx
-Ravi

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

public class Form1 : System.Windows.Forms.Form
{
Label lbl = new Label();
Button btn = new Button();
public bool fAlive;
int cHits = 0;
ArrayList m_alth = new ArrayList();

public Form1()
{
InitializeComponent();
}

private void InitializeComponent()
{
this.Text = "Form1";

lbl.Text = "Thread1";
this.Controls.Add(lbl);

btn.Location = new Point(50, 50);
btn.Text = "Start";
btn.Click += new EventHandler(this.OnStop);
this.Controls.Add(btn);
this.Closing += new CancelEventHandler(this.OnClosing);
this.ControlBox = true;
this.MinimizeBox = false;

}

private void OnStop(object sender, EventArgs e)
{
if (fAlive)
{
fAlive = false;
btn.Text = "Start";
m_alth.Clear();
}
else
{
fAlive = true;
btn.Text = "Stop";
Thread th;
for (int i=0; i<1; i++)
{
th = new Thread(new ThreadStart(new ThreadRunner(this, i+1).ThreadRun));
th.Start();
m_alth.Add(th);
}
}
}

private void OnClosing(object sender, CancelEventArgs e)
{
Console.WriteLine("inside dest");

foreach(Object obj in m_alth)
{
Thread th = (Thread)obj;
Console.WriteLine("stopping " + th);
//th.Abort();
}

fAlive = false;
//base.Dispose();
}


public void OnTextChange(object sender, System.EventArgs e)
{
this.cHits++;
this.Text = (cHits) + "Thread - " + sender;
//this.Refresh();
}

static void Main()
{
Application.Run(new Form1());
}
}

public class ThreadRunner
{
Form1 m_frm = null;
public int m_id;

public ThreadRunner(Form1 frm1, int id)
{
m_frm = frm1;
m_id = id;
}

public void ThreadRun()
{
while(m_frm.fAlive)
{
Console.WriteLine("heart beat from: " + m_id);
m_frm.Invoke(new EventHandler(m_frm.OnTextChange));
Thread.Sleep(300);
}
}
}


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top