R
Ricardo Pereira
Hello all,
I have a C# class (in this example, called A) that, in its
constructor, starts a thread with a method of its own. That thread
will be used to continuously check for one of its object's state and
generate classe's A events "Connected" and "Disconnected".
It looks something like this:
"
public class A
{
private AnotherObject an_obj;
private Thread thr;
...
public event System.EventHandler Connected;
public event System.EventHandler Disconnected;
public A()
{
thr = new Thread(new ThreadStart(this.DoThreadWork));
thr.Start();
while (!thr.IsAlive);
thr.IsBackground = true;
}
private void DoThreadWork()
{
an_obj = new AnotherObject();
while(true)
{
if (an_obj.IsPresent())
if (Connected != null)
Connected(this, EventArgs.Empty);
else
if (Disconnected != null)
Disconnected(this, EventArgs.Empty);
}
}
public void DoSomethingX()
{
an_obj.CallXWork();
}
public int DoSomethingY()
{
return an_obj.CallYWork();
}
}
"
and a form that uses the previous class:
"
public class Form1: System.Windows.Forms.Form
{
private A objA;
...
public Form1()
{
objA = new SmartcardEVController();
objA.Connected += new EventHandler(objA_Connected);
objA.Disconnected += new EventHandler(objA_Disconnected);
}
private void objA_Connected(object sender, EventArgs e)
{
objA.DoSomethingX();
}
private void objA_Disconnected(object sender, EventArgs e)
{
int i = objA.DoSomethingY();
MessageBox.Show("Value: " + i);
}
private void button1_Click(object sender, System.EventArgs e)
{
objA.DoSomethingX();
MessageBox.Show("Done");
}
}
"
The problem is that although every call made to object "objA", inside
the "objA_Connected" or "objA_Disconnected" captured event works
perfectly fine, when a method is called on "objA" anywhere else it
just blocks the program execution. In this example, when button1 is
clicked, the instruction "MessageBox.Show("Done");" won't get called
and the program (Form1) will stop responding.
By the way, class AnotherObj is a .NET wrapper for a C dll that, in
turn, is a JNI wrapper for a Java class. All Java methods are
"synchronized".
I thought that this could be a problem of the Java object but the fact
is that the cycle inside the thread keeps on executing when calls are
made inside "objA_Connected" or "objA_Disconnected" and those calls
execute successfully, no matter how many calls are made to object
"objA".
A strange thing that happens is that the call "objA.DoSomethingX();"
inside the click event makes the corresponding Java method start
executing (using log utilities in Java, I can check that the method
began execution but then stopped for no apparent reason).
The only thing I can now think of is that the thread that is spawned
in the A class has some kind of own memory space that the object that
was initialized inside the thread ("an_obj") can't be safely used
outside the thread. Being the thread that generates the events
"Connected" and "Disconnected", that could explain that calls to
object "objA", inside "objA_Connected" and "objA_Disconnected",
executed well. But that's only an hipothetical thought.
p.s.: I also tried initializing object "an_obj" outside the thread, in
classe's A contructor, but that way the call "an_obj.IsPresent())"
would also block in the C dll (that calls JNI code) for no apparent
reason.
If anyone could give me an explanation of why this is happening and
what I can do to avoid it, I would be grateful, as I can't figure it
out just by myself.
Thank you very much,
Ricardo Pereira
I have a C# class (in this example, called A) that, in its
constructor, starts a thread with a method of its own. That thread
will be used to continuously check for one of its object's state and
generate classe's A events "Connected" and "Disconnected".
It looks something like this:
"
public class A
{
private AnotherObject an_obj;
private Thread thr;
...
public event System.EventHandler Connected;
public event System.EventHandler Disconnected;
public A()
{
thr = new Thread(new ThreadStart(this.DoThreadWork));
thr.Start();
while (!thr.IsAlive);
thr.IsBackground = true;
}
private void DoThreadWork()
{
an_obj = new AnotherObject();
while(true)
{
if (an_obj.IsPresent())
if (Connected != null)
Connected(this, EventArgs.Empty);
else
if (Disconnected != null)
Disconnected(this, EventArgs.Empty);
}
}
public void DoSomethingX()
{
an_obj.CallXWork();
}
public int DoSomethingY()
{
return an_obj.CallYWork();
}
}
"
and a form that uses the previous class:
"
public class Form1: System.Windows.Forms.Form
{
private A objA;
...
public Form1()
{
objA = new SmartcardEVController();
objA.Connected += new EventHandler(objA_Connected);
objA.Disconnected += new EventHandler(objA_Disconnected);
}
private void objA_Connected(object sender, EventArgs e)
{
objA.DoSomethingX();
}
private void objA_Disconnected(object sender, EventArgs e)
{
int i = objA.DoSomethingY();
MessageBox.Show("Value: " + i);
}
private void button1_Click(object sender, System.EventArgs e)
{
objA.DoSomethingX();
MessageBox.Show("Done");
}
}
"
The problem is that although every call made to object "objA", inside
the "objA_Connected" or "objA_Disconnected" captured event works
perfectly fine, when a method is called on "objA" anywhere else it
just blocks the program execution. In this example, when button1 is
clicked, the instruction "MessageBox.Show("Done");" won't get called
and the program (Form1) will stop responding.
By the way, class AnotherObj is a .NET wrapper for a C dll that, in
turn, is a JNI wrapper for a Java class. All Java methods are
"synchronized".
I thought that this could be a problem of the Java object but the fact
is that the cycle inside the thread keeps on executing when calls are
made inside "objA_Connected" or "objA_Disconnected" and those calls
execute successfully, no matter how many calls are made to object
"objA".
A strange thing that happens is that the call "objA.DoSomethingX();"
inside the click event makes the corresponding Java method start
executing (using log utilities in Java, I can check that the method
began execution but then stopped for no apparent reason).
The only thing I can now think of is that the thread that is spawned
in the A class has some kind of own memory space that the object that
was initialized inside the thread ("an_obj") can't be safely used
outside the thread. Being the thread that generates the events
"Connected" and "Disconnected", that could explain that calls to
object "objA", inside "objA_Connected" and "objA_Disconnected",
executed well. But that's only an hipothetical thought.
p.s.: I also tried initializing object "an_obj" outside the thread, in
classe's A contructor, but that way the call "an_obj.IsPresent())"
would also block in the C dll (that calls JNI code) for no apparent
reason.
If anyone could give me an explanation of why this is happening and
what I can do to avoid it, I would be grateful, as I can't figure it
out just by myself.
Thank you very much,
Ricardo Pereira