Click here to Skip to main content
15,912,320 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi Friends,

I need help in C# coding. I have one dll example test.dll.

Creating two objects for this test.dll.

Sample code:
C#
public void callprocedure()
        {
            
          
            //this delegate will handle thread start event
            ThreadStart ts = new ThreadStart(Line.ProcessingThread);
            //create new thread
            Thread wrkThread = new Thread(ts);
            Line.CurrentThread = wrkThread;
            wrkThread.SetApartmentState(ApartmentState.STA); //default is MTA
            wrkThread.Name = "0"; //for easier tracing
            wrkThread.Start();
        }



// Here the tread method calling.
public void ProcessingThread()
            {
 private test test1;
    
                int iThreadId = CurrentThread.GetHashCode(); //unique thread id
               // here creating the object for test.dll and using as per requirement 
               test1= new test.ADXVoice();
}

// Now second thread starting here
      public void connectcall()
        {
         
// calling method from the second thread
         ThreadStart tscall = new ThreadStart(ProcessingThreadForNextCall);
          Thread calthread = new Thread(tscall);
            Thread CurrentThread = calthread;
            calthread.Start();
  
                }
            }

public void ProcessingThreadForNextCall()
          {
              int iThreadId = CurrentThread.GetHashCode(); //unique thread id
              Trace("In the new thread " + iThreadId + " line = " + iResource);
            
private test test2;
                  test2= new test.ADXVoice()
                 
// Now here in this method i want this as follows

test1.DisconnectCall();// here I am getting the object set null.

}

My comments: test1 is object of first thread function so if i try to access in second thread funtion then getting null exception.

So friends please suggest me how to access the object of that test1 in test2 created method.

But am getting "object reference not correct" Obeviously i will get this error before that test1 is object in firstthread but I am trying to access in secondthread.


How can I solve this issue please any suggestions please.....


Thanks & Regards
Syed Chand Basha
Posted
Updated 9-Nov-14 21:06pm
v4
Comments
Sergey Alexandrovich Kryukov 31-Oct-14 2:09am    
You need to produce some comprehensive code sample which reproduces the problem and can be built immediately. Chances are, this is something really simple.
—SA
gggustafson 31-Oct-14 10:33am    
Agree with Sergey
chand5055 10-Nov-14 1:57am    
Hi Sergey and gggustafson can give any suggestions by this sample code..
Afzaal Ahmad Zeeshan 10-Nov-14 14:57pm    
Reply to their comment to let them know when you've replied to their question or comment.

1 solution

move the following codes to class level rather than scoped at method level.
C#
private test test1;
private test test2;
private Object thisLock = new Object();

Now you can access the test1 or test2 from anywhere, just check if they are null or not. Also use lock sequences.
C#
public void ProcessingThreadForNextCall()
{
        int iThreadId = CurrentThread.GetHashCode(); //unique thread id
        Trace("In the new thread " + iThreadId + " line = " + iResource);
        
        test2= new test.ADXVoice()

        //Now here in this method i want this as follows
        lock (thisLock)
        {
        if(test1 !=null)
        test1.DisconnectCall();// here I am getting the object set null.
        }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900