Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在第二个线程上运行整个类?_C#_Multithreading - Fatal编程技术网

C# 如何在第二个线程上运行整个类?

C# 如何在第二个线程上运行整个类?,c#,multithreading,C#,Multithreading,我正在深入研究多线程,找到了一些很好的教程,但我还有一些问题 我计算了如何异步运行一个函数(见下图),这里有四个例子来说明这一点 但是在我正在开发的应用程序中,我希望在一个单独的线程中运行整个类。我在找这样的东西: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; n

我正在深入研究多线程,找到了一些很好的教程,但我还有一些问题

我计算了如何异步运行一个函数(见下图),这里有四个例子来说明这一点

但是在我正在开发的应用程序中,我希望在一个单独的线程中运行整个类。我在找这样的东西:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace multithread_test
{
    class Program
    {
        Program()
        { }
        RunInBackground RIB;

        void StartBackgroundWorker()
        {
            // how do  I get RIB to run in the background?
            RIB = new RunInBackground();
        }

        //somefunction to listen to the CallEventToUpdateGUI
    }


    //This class should run in a different thread than class Program
    class RunInBackground
    {
        void RunInBackground()
        { }

        void Function1()
        {
            //somefunction
        }

        void Function2()
        {
            // somefunction
        }

        void Function3()
        {
            Function1();
        }

        void CallEventToUpdateGUI()
        {
            //call a event to update gui
        }

    }

线程是关于代码的执行,而不是它的定义。你不能这样做。 您只能在另一个线程上运行代码


您也可以在另一个线程上实例化一个类,但顺便说一句,它不是它的定义。

类不在线程中运行。方法可以。方法与其调用程序(通常)在同一线程上运行。如果
RIB
在另一个线程上“运行”,然后您从
程序中调用
RIB.Function1()
,您会期望发生什么?我希望此函数在另一个线程上运行,但这样想可能是愚蠢的……好吧,如果我在单独的线程中调用函数,从这个函数我调用了其他函数,这些函数都在同一个单独的线程上运行?这使得线程更容易。。。但同时,更复杂的是,我将学习更多关于这个主题的知识,尝试一些东西。谢谢