多线程如何传递参数

101 篇文章 0 订阅

C#中的多线程函数是不允许有参数的,  
  如果要实现参数传递,你可以通过类内的其它属性或域的方式把参数传递进要执行的函数体内  
  注意,不要忘记使用lock函数,

 

 线程调用的类的方法不能有返回值和参数。你通过定义新类并设置该类的属性或字段值把参数传入。如果要调用控件等,可以在构造函数里传入。

你可以把新开的线程封装到一个类中,如:  
                  public   class   subClass  
                  {  
                          int   a;  
                          public   subClass(int   intTemp)  
                          {  
                                  this.a   =   intTemp;  
                          }  
                          public   void   Thread1()  
                          {  
                                //code  
                          }  
                  }  
                  public   Form1()  
                  {  
                          InitializeComponent();  
                  }  
                  private   void   button1_Click(object   sender,   EventArgs   e)  
                  {  
                          subClass   a   =   new   subClass(10);//参数10  
                          Thread   t   =   new   Thread(new   ThreadStart(a.Thread1));  
                          t.Start();  
                  }   
   

 

.NET   Framework   2.0里有个新的ParameterizedThreadStart委托,可以用来传一个object的参数  
  下面是摘自MSDN里的例子,我个人还是喜欢用类的老方法  
  using   System;  
  using   System.Threading;  
   
  public   class   Work  
  {  
          public   static   void   Main()  
          {  
                  //   To   start   a   thread   using   a   shared   thread   procedure,   use  
                  //   the   class   name   and   method   name   when   you   create   the    
                  //   ParameterizedThreadStart   delegate.  
                  //  
                  Thread   newThread   =   new   Thread(  
                          new   ParameterizedThreadStart(Work.DoWork));  
                   
                  //   Use   the   overload   of   the   Start   method   that   has   a  
                  //   parameter   of   type   Object.   You   can   create   an   object   that  
                  //   contains   several   pieces   of   data,   or   you   can   pass   any    
                  //   reference   type   or   value   type.   The   following   code   passes  
                  //   the   integer   value   42.  
                  //  
                  newThread.Start(42);  
   
                  //   To   start   a   thread   using   an   instance   method   for   the   thread    
                  //   procedure,   use   the   instance   variable   and   method   name   when    
                  //   you   create   the   ParameterizedThreadStart   delegate.  
                  //  
                  Work   w   =   new   Work();  
                  newThread   =   new   Thread(  
                          new   ParameterizedThreadStart(w.DoMoreWork));  
                   
                  //   Pass   an   object   containing   data   for   the   thread.  
                  //  
                  newThread.Start("The   answer.");  
          }  
     
          public   static   void   DoWork(object   data)  
          {  
                  Console.WriteLine("Static   thread   procedure.   Data='{0}'",  
                          data);  
          }  
   
          public   void   DoMoreWork(object   data)  
          {  
                  Console.WriteLine("Instance   thread   procedure.   Data='{0}'",  
                          data);  
          }  
  }  
   
  /*   This   code   example   produces   the   following   output   (the   order    
        of   the   lines   might   vary):  
   
  Static   thread   procedure.   Data='42'  
  Instance   thread   procedure.   Data='The   answer'  
  */  

在PyQt中,可以使用多线程传递参数。可以通过以下步骤实现: 1. 创建一个继承自`QThread`的自定义线程类,并重写其`run()`方法。在`run()`方法中,可以将需要传递的参数作为线程类的成员变量,并在需要的时候使用。 2. 在主线程中创建自定义线程类的实例,并设置需要传递的参数。 3. 调用自定义线程类的`start()`方法启动线程。 4. 在自定义线程类中,可以通过成员变量的方式访问传递的参数。 下面是一个示例代码,演示了如何在PyQt中使用多线程传递参数: ```python from PyQt5.QtCore import QThread class MyThread(QThread): def __init__(self, param): super().__init__() self.param = param def run(self): # 在这里可以使用self.param访问传递的参数 print("参数:", self.param) # 在主线程中创建自定义线程类的实例,并设置需要传递的参数 param = "Hello World" thread = MyThread(param) # 启动线程 thread.start() ``` 在上面的示例中,我们创建了一个名为`MyThread`的自定义线程类,它接受一个参数`param`。在`run()`方法中,我们可以使用`self.param`访问传递的参数。在主线程中,我们创建了`MyThread`的实例,并设置了参数`"Hello World"`。然后,我们调用`start()`方法启动线程。 通过这种方式,我们可以在PyQt中使用多线程传递参数。 #### 引用[.reference_title] - *1* *2* *3* [Qt/PyQt多线程](https://blog.csdn.net/weixin_42052836/article/details/129686441)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值