我正在挖掘多线程,发现一些很好的教程,但我还有一些问题。如何在第二个线程上运行整个类?
我想如何运行一个函数异步,(见这个tutorial)有四个例子来实现这一点。
但是在我开发的应用程序中,我想在一个单独的线程中运行整个类。我在寻找这样的事情:
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'在另一个线程上“运行”,然后从'Program'内调用'RIB.Function1()',你会发生什么? –
我希望这个函数能够在另一个线程上运行,但可能这是愚蠢的想像那样...... – 2pietjuh2