关于线程的参数(2.0)、“返回值”、及线程的中止

 

关于线程的参数(2.0)、“返回值”、及线程的中止

1.线程的参数:
有时候会想向辅助线程传递些信息,这里需要用到ParameterizedThreadStart 委托

示例:

        private void btRunThread_Click(object sender, EventArgs e)

        {

            Thread t = new Thread(new ParameterizedThreadStart(this.ThreadRun));

            t.Start(100);

        }

 

        private void ThreadRun(object o)

        {

            this.lbCompleted.Invoke((MethodInvoker)delegate { this.lbCompleted.Text = System.Convert.ToString(o); });

        }

2.通过代理可以大致实现类似功能,示例:

    class Program

    {

        static void Main (string[] args)

        {

            ThreadClass tc = new ThreadClass(new MyDlg(DlgMethod));

            Thread thread = new Thread(new ThreadStart(tc.ThreadRun));

            Console.WriteLine("second thread start");

            thread.Start();

            thread.Join();

            Console.WriteLine("second thread completed");

            Console.Read();       

        }

        private static void DlgMethod(int i)

        {

            Console.WriteLine("Second Thread Result:{0}", i);

        }

    }

 

    public delegate void MyDlg(int i);

 

    class ThreadClass

    {

        private MyDlg myDlg;

 

        public ThreadClass(MyDlg pDlg)

        {

            this.myDlg = pDlg;

        }

 

        public void ThreadRun()

        {

            int total = 0;

            for (int i = 0; i < 100; i++)

            {

                total += i;

            }

 

            if (myDlg != null)

            {

                myDlg(total);

            }

        }

    }


3.线程的中止:

(1).join方法

MSDN注释:在继续执行标准的 COM SendMessage 消息泵处理期间,阻止调用线程,直到某个线程终止为止。

看得一头雾,自己试了一下,似乎线程在调用join方法之后,该线程抢占了所有的cpu时间,直到线程的任务完成。不知道是这是这样?

(2).abort方法

立即中止线程

(3).定义标识量

示例:

    class Program

    {

        private static bool stop;

        static void Main (string[] args)

        {

            stop = false;

 

            Thread t = new Thread(new ThreadStart(ThreadRun));

            t.Start();

            Thread.Sleep(100);

            stop = true;

 

            Console.Read();

        }

 

        static void ThreadRun()

        {

            while (!stop)

            {

                Console.WriteLine("Do Some Work...");

            }

        }

    }

在 FastAPI 中创建线程并传递参数可以使用 Python 中的 threading 模块,示例如下: ```python import threading from fastapi import FastAPI app = FastAPI() def my_function(arg1, arg2): """ 你想要在新线程中执行的函数 """ # 执行你的代码 result = arg1 + arg2 return result @app.get("/") async def root(): # 创建一个新线程并传递参数 thread = threading.Thread(target=my_function, args=(1, 2)) thread.start() thread.join() # 等待线程执行完毕 result = thread.result # 获取线程执行结果 return {"result": result} ``` 在这个示例中,我们定义了一个名为 `my_function` 的函数,它接受两个参数,并返回它们的和。在 FastAPI 中,我们在路由函数上使用 `@app.get("/")` 装饰器,这表示这个路由函数将处理 GET 请求。在路由函数内部,我们使用 `threading.Thread()` 方法创建一个新的线程,并传递参数 `arg1` 和 `arg2` 给 `my_function` 函数。然后我们使用 `thread.start()` 方法启动线程并让它开始执行。 接下来,我们使用 `thread.join()` 方法等待线程执行完毕。这个方法会阻塞当前线程,直到被等待的线程执行完毕。最后,我们使用 `thread.result` 属性获取线程执行结果,并将它作为 JSON 响应返回给客户端。 需要注意的是,如果你在使用线程时需要访问数据库或其他 I/O 操作,最好使用异步方式进行,以避免阻塞主线程。为了实现这一点,你可以使用 Python 的 asyncio 库或第三方库如 aiohttp。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值