using System.Threading; Thread thread1 = new Thread(new ThreadStart(setDatagridview11)); Thread thread2 = new Thread(new ThreadStart(setDatagridview22)); thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join(); 方法有参数 怎么写
时间: 2023-08-14 14:06:32 浏览: 114
如果 `setDatagridview11` 和 `setDatagridview22` 方法需要参数,可以使用 `ParameterizedThreadStart` 委托来启动线程,并将参数传递给方法。
例如,如果 `setDatagridview11` 方法需要一个名为 `data` 的字符串参数,可以这样启动线程:
```
string data = "这是传递给 setDatagridview11 方法的参数";
Thread thread1 = new Thread(new ParameterizedThreadStart(setDatagridview11));
thread1.Start(data);
```
在 `setDatagridview11` 方法中,需要将参数转换为正确的类型:
```
private void setDatagridview11(object data)
{
string dataString = (string)data;
// 使用传递进来的参数
}
```
同样的,对于 `setDatagridview22` 方法也可以这样处理。
相关问题
在thread1和thread2中创建多个线程 参考using System.Threading; Thread thread1 = new Thread(new ThreadStart(setDatagridview11)); Thread thread2 = new Thread(new ThreadStart(setDatagridview22)); thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join();
如果需要在 thread1 和 thread2 中创建多个线程,可以在 setDatagridview11 和 setDatagridview22 方法中使用 Thread 类来创建新线程。然后使用 Start 方法来启动新线程,并使用 Join 方法等待它们完成。
下面是一个示例代码:
```
using System;
using System.Threading;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main thread started.");
Thread thread1 = new Thread(new ThreadStart(setDatagridview11));
Thread thread2 = new Thread(new ThreadStart(setDatagridview22));
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
Console.WriteLine("Main thread finished.");
Console.ReadKey();
}
static void setDatagridview11()
{
Console.WriteLine("setDatagridview11 started.");
for (int i = 0; i < 5; i++)
{
Thread childThread = new Thread(new ParameterizedThreadStart(ChildThreadFunc));
childThread.Start(i);
}
Console.WriteLine("setDatagridview11 finished.");
}
static void setDatagridview22()
{
Console.WriteLine("setDatagridview22 started.");
for (int i = 0; i < 5; i++)
{
Thread childThread = new Thread(new ParameterizedThreadStart(ChildThreadFunc));
childThread.Start(i + 5);
}
Console.WriteLine("setDatagridview22 finished.");
}
static void ChildThreadFunc(object index)
{
Console.WriteLine("Child thread {0} started.", index);
Thread.Sleep(1000);
Console.WriteLine("Child thread {0} finished.", index);
}
}
```
在这个示例中,setDatagridview11 和 setDatagridview22 方法分别创建了 5 个新线程,并在其中调用了 ChildThreadFunc 方法。在 Main 方法中,启动 thread1 和 thread2 线程,并使用 Join 方法等待它们完成。运行程序后,你将会看到如下输出:
```
Main thread started.
setDatagridview11 started.
setDatagridview22 started.
Child thread 0 started.
Child thread 5 started.
Child thread 1 started.
Child thread 6 started.
Child thread 2 started.
Child thread 7 started.
Child thread 3 started.
Child thread 8 started.
Child thread 4 started.
Child thread 9 started.
Child thread 0 finished.
Child thread 5 finished.
Child thread 1 finished.
Child thread 6 finished.
Child thread 2 finished.
Child thread 7 finished.
Child thread 3 finished.
Child thread 8 finished.
Child thread 4 finished.
Child thread 9 finished.
setDatagridview11 finished.
setDatagridview22 finished.
Main thread finished.
```
System.Threading.SynchronizationLockException HResult=0x80131518 Message=Object synchronization method was called from an unsynchronized block of code. Source=System.Private.CoreLib StackTrace: 在 System.Threading.Monitor.Exit(Object obj) 在 CMOSImageSensor.T2001.GetSsrMode() 在 D:\dmoco\work_Project\AE_DVS_HDR\T2001AutoExposureControl\T2001_mass-storage-tool_using_net6\CMOSImageSensor\T2001\T2001.cs 中: 第 433 行 在 CMOSImageSensor.T2001.GetMaxIntegrationVblk() 在 D:\dmoco\work_Project\AE_DVS_HDR\T2001AutoExposureControl\T2001_mass-storage-tool_using_net6\CMOSImageSensor\T2001\T2001.cs 中: 第 610 行 在 raw_data_mass_storage.AE_mode.AE_DVS_Form_Test.AE_Function() 在 D:\dmoco\work_Project\AE_DVS_HDR\T2001AutoExposureControl\T2001_mass-storage-tool_using_net6\raw-data-mass-storage\AE_mode\AE_DVS_Form_Test.cs 中: 第 256 行 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) --- 上一位置中堆栈跟踪的末尾 --- 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Threading.ThreadHelper.ThreadStart()
这是一个线程同步异常。异常信息指出,在类 CMOSImageSensor.T2001 中的 GetSsrMode() 方法的第 433 行和 GetMaxIntegrationVblk() 方法的第 610 行调用了同步对象方法,但这些方法是在未同步的代码块中调用的。这可能是由于在调用这些方法时没有正确使用锁导致的。建议检查这些方法的调用代码,确保在调用时正确使用锁。
阅读全文