简体   繁体   English

如何从 C# 线程到 object 哪个成员 function 是该线程的参数?

[英]How do I get from C# thread to the object which member function was the parameter to that thread?

In C# threads are created by passing a member function:在 C# 中,线程是通过传递成员 function 创建的:

class SomeClass {
   public void ThreadFunction() {Thread.Sleep( Infinite ); }
};


SomeClass myObject = new SomeClass();
Thread thread = new Thread( myObject.ThreadFunction );
thread.Start();

Here ThreadFunction() is not static, so I guess the object reference is passed to Thread constructor.这里ThreadFunction()不是 static,所以我猜 object 引用被传递给Thread构造函数。

How can code inside ThreadFunction() get to myObject ? ThreadFunction()中的代码如何到达myObject Do I just use this reference for that?我只是使用this参考吗?

In the exact example you give, simply by accessing this .在您给出的确切示例中,只需访问this

In the general case, you can also do something like在一般情况下,您还可以执行类似的操作

class SomeClass {
   public void ThreadFunction(object param)
   {
     var justAnExample = (string)param;
   }
};


SomeClass myObject = new SomeClass();
Thread thread = new Thread(myObject.ThreadFunction);
thread.Start("parameter");

This allows you to pass a parameter of any type (here, a string ) to the thread function.这允许您将任何类型的参数(此处为string )传递给线程 function。 If you need more than one, then you can always pass in a Tuple or an object[] or any other container of values.如果你需要多个,那么你总是可以传入一个Tuple或一个object[]或任何其他值的容器。

If you go this way, you might want to make ThreadFunction static (this will lose you the option of using this ) as well.如果您以这种方式使用 go,您可能还需要制作ThreadFunction static (这将使您失去使用this的选项)。

Like this:像这样:

class SomeClass {
  public void ThreadFunction(Object obj) 
  {
    SomeClass myObject = (SomeClass)obj;
    Thread.Sleep( Infinite ); 
  }
};


SomeClass myObject = new SomeClass();
Thread thread = new Thread(new ParameterizedThreadStart(myObject.ThreadFunction) );
thread.Start(myObject)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#如何将对象从工作线程传递回主线程? - C# How do I pass object back to main thread from worker thread? c# - 如何从单独线程中的函数获取结果 - How to get the result from a function in a separate thread c# 如何在 C# 中启动线程 - How do I start a thread in C# C# - 循环通过从新线程传递的对象参数 - C# - Looping through object parameter that was passed from new thread 在 c++ dll 中启动一个线程,从一个 C# wpf 开始一个线程,内部需要一个线程 - Starting a thread in a c++ dll, from a C# wpf application, do I need a thread inside a thread? 具有线程返回和参数的C#传递函数 - C# pass function with return and parameter to Thread C#中的Thread.Sleep():它是否休眠了实例化对象的线程,或者我从中调用方法的线程? - Thread.Sleep() in C#: Does it sleep the thread the object was instantiated in, or the thread i am calling the method from? C#如何防止TcpClient对象放置在使用new关键字创建的其他线程中? - C# How do I prevent a TcpClient object from disposing in a different thread i've created with the new keyword? 如何从C#调用具有void * callback和object参数的C ++ Dll中的函数 - How do I call a function in a C++ Dll from C# that has void* callback and object parameter 如何在C#中找到运行我的线程的处理器? - How do I find the processor on which my thread is running in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM