使用串行端口通信发送批量消息

时间:2012-08-14 10:25:10

标签: c# multithreading sms serial-port

我可以通过写

从我的申请中发送消息
port.WriteLine("AT+CMGS=\"" + m.Groups[2].Value + "\"");
port.Write(txt_msgbox.Text + char.ConvertFromUtf32(26));

这很好。但现在我想将批量消息发送到一组数字。我有这个循环来发送消息给一组数字:

foreach (ListViewItem item in bufferedListView1.Items)
{
    string lname = bufferedListView1.Items[i].Text;
    string lno = bufferedListView1.Items[i].SubItems[1].Text;
    string gname = bufferedListView1.Items[i].SubItems[2].Text;
    string line = lname + "@" + lno + "@" + gname;
    if (gname.Contains(sgroup))
    {
        var m = Regex.Match(line, @"([\w]+)@([+\d]+)@([\w]+)");
        if (m.Success)
        {
            port.WriteLine("AT+CMGS=\"" + m.Groups[2].Value + "\"");
            port.Write(txt_msgbox.Text + char.ConvertFromUtf32(26));
            Thread.Sleep(4000);
        }
        sno++;
    }
    i++;
}

这也很有效。但问题在于UI,在相当长的时间内变得反应迟钝。有没有更好的方法呢?

1 个答案:

答案 0 :(得分:3)

使用BackgroundWorker - 它们非常易于使用,并以封装的方式管理线程。这个类的好处是它支持取消,如果你想取消流程并且它支持报告进度,我将在这里展示。最后,关于这个类的好处是,当它报告进度时,它会正确切换线程,确保你没有在单独的线程上写入UX - 但DoWork事件处理程序中的代码仍然可以正确管理一个单独的线程。

您可以执行以下操作。

// in this example this is scoped to the class, but just scope it appropriately
private BackgroundWorker worker = new BackgroundWorker();

......

// because I don't really know anything about how your program is structured I'm not sure
// where you want to place all of the definitions OR where you want to place RunWorkerAsync
// but you want the definitions to happen ONCE and the RunWorkerAsync to be where the USER
// initiates it

// this will allow you to consume the ProgressChanged event
worker.WorkerReportsProgress = true;

// this will allow you to set the CancellationPending property
worker.WorkerSupportsCancellation = true;

worker.DoWork += (o, args) =>
{
    foreach (ListViewItem item in bufferedListView1.Items) 
    { 
        string lname = bufferedListView1.Items[i].Text; 
        string lno = bufferedListView1.Items[i].SubItems[1].Text; 
        string gname = bufferedListView1.Items[i].SubItems[2].Text; 
        string line = lname + "@" + lno + "@" + gname; 
        if (gname.Contains(sgroup)) 
        { 
            var m = Regex.Match(line, @"([\w]+)@([+\d]+)@([\w]+)"); 
            if (m.Success) 
            { 
                port.WriteLine("AT+CMGS=\"" + m.Groups[2].Value + "\""); 
                port.Write(txt_msgbox.Text + char.ConvertFromUtf32(26)); 
                Thread.Sleep(4000); 
            } 
            sno++; 
        } 
        i++; 
    }
}

worker.ProgressChanged += (s, args) =>
{
    // set some progress here, the ProgressChangedEventArgs inherently supports an integer
    // progress via the ProgressPercentage property
}

worker.RunWorkerCompleted += (s, args) =>
{
    // with the RunWorkerCompletedEventArgs class you can check for errors via Error, did
    // cancellation occur via Cancelled, and you can even send a complex result via the Result
    // property - whatever you need
}

// this starts the work in the DoWork event handler
worker.RunWorkerAsync();

以下是Microsoft关于此事的文档的link