English 中文(简体)
只有在那个地方实施最后的read,才能在一段时间上启动。
原标题:Threadstart in a loop in a timer only executing last thread in said loop.

I have a timer executing a loop executing a series of threads. Each thread simply polls a serial device (on unique ports) and writes the data to a MySQL Database for pickup by a web application.

我只想看一看 lo子里的最后一线。

这里是我的时间<代码>Interval方法。

static void glob_timer_Elapsed(object sender, ElapsedEventArgs e)
{
    foreach (object[] row in user_ids)
    {
        Console.WriteLine(row[0].ToString() + ":" + row[1].ToString());
        ThreadStart starter = delegate { Functions.UpdateFor(row[0].ToString(), row[1].ToString()); };
        new Thread(starter).Start();
    }
}

用户_ids is a List<object[]> 载有来自询问的浏览量;因此,row[0] =user ID<>em>,row } =Com Portony/em>

在职能<代码>上:

public static void UpdateFor(string uid, string com_name)
{
    try
    {
        Console.WriteLine("[" + uid + "]- " + DateTime.Now.ToString() + " : " + "Begin read transmission.");

When this is run, in the console I see

 1:COM1 
 2:COM2
 [2]-{DateTime} : Begin read transmission

为什么我看不到《<代码>UpdateFor for uid 1.

注:我还核实了从COM1中拟写给非行的数据。

最佳回答

你们正面临一个封闭问题。 每位代表在每一行程中都提到<条码>目标[] row,因此,他们都以同样的提法执行,即不论增长价值如何。 你们需要简单地分配内部变量,以确保每个代表都能得到独特的参考。

 foreach (object[] row in user_ids)
 {
    var innerReference = row;
    Console.WriteLine(innerReference[0].ToString() + ":" + innerReference[1].ToString());
    ThreadStart starter = delegate { Functions.UpdateFor(innerReference[0].ToString(), innerReference[1].ToString()); };
    new Thread(starter).Start();
 }
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...