Object initialization in C# thread - unexplained behaviour

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Ricardo Pereira (pereira.ric_at_mail.telepac.pt)
Date: 05/31/04


Date: 31 May 2004 11:13:08 -0700

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



Relevant Pages

  • Re: Object initialization in C# thread - unexplained behaviour
    ... > objA = new SmartcardEVController; ... > private void objA_Connected ... when a method is called on "objA" anywhere else it> just blocks the program execution. ... > By the way, class AnotherObj is a .NET wrapper for a C dll that, in> turn, is a JNI wrapper for a Java class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Repost: Cancel stored procedure using JDBC
    ... I have a Java application that spawns 4 different threads. ... the database stops execution and replies with an error message. ... When JDBC does not receive an IOException, Oracle Net may eventually time out ... The server-side internal driver runs in the ...
    (comp.lang.java.programmer)
  • Re: JVM vs. EXE comaprison
    ... >> Startup time is added to the benchmark, and as such isnt measuring Java ... > The benchmark clearly is measuring Java execution performance. ...
    (comp.lang.java.programmer)
  • Re: changing access modifier of base method
    ... I'm not sure if this counts as a different meaning than your ... quality in the Java resources I've seen:)). ... design reason for making sealed the default. ... many more private members than public. ...
    (microsoft.public.dotnet.languages.csharp)
  • c#, DTS and Threads
    ... I imported DTS into .NET like Microsoft ... That worked fine beside that the gui was locked during execution of the DTS ... private delegate void LabelUpdateDelegate(string sString); ...
    (microsoft.public.sqlserver.programming)