如何访问一个线程的对象到另一个线程的对象 [英] How to access the object of one thread to object of another thread

查看:93
本文介绍了如何访问一个线程的对象到另一个线程的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我需要C#编码方面的帮助。我有一个DLL示例test.dll。



为此test.dll创建两个对象。



示例代码:

Hi Friends,

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

Creating two objects for this test.dll.

Sample code:

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.

}



我的评论:test1是第一个线程函数的对象,所以如果我尝试访问第二个线程函数然后获得null异常。



所以朋友们请建议我如何访问test2创建方法中test1的对象。



但是得到对象引用不正确显然我会在test1是firstthread中的对象之前得到这个错误但是我我试图访问secondthread。





如何解决这个问题请任何建议.....





谢谢&问候

Syed Chand Basha


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

推荐答案

将以下代码移到类级别而不是方法级别的作用域。

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



现在您可以从任何地方访问test1或test2,只需检查它们是否是是否为空。也使用锁序列。


Now you can access the test1 or test2 from anywhere, just check if they are null or not. Also use lock sequences.

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.
        }
}


这篇关于如何访问一个线程的对象到另一个线程的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆