如何同步线程中的 2 个进程以便它们一起运行?

发布于 2024-11-30 13:52:36 字数 672 浏览 0 评论 0原文

我目前有这段代码(感谢这里的帮助)。我需要将第一个 ProcessMessage 创建为线程,并同步运行第二个 ProcessMessage(在当前线程上),然后在单个线程上执行 Join。否则,我将让三个线程有效地完成两件事。我该如何修改它来实现它?我使用的是.NET 3.5

Thread thRegion1 = new Thread(() =>
{
    if (Region1.Trim().Length > 0)
    {
        returnMessage = ProcessTheMessage(string.Format(queueName, Region1));
        Logger.Log(returnMessage);
    }
});

Thread thRegion2 = new Thread(() =>
 {
     if (Region2.Trim().Length > 0)
     {
         returnMessage = ProcessTheMessage(string.Format(queueName, Region2));
         Logger.Log(returnMessage);
     }
 });

thRegion1.Start();
thRegion2.Start();

thRegion1.Join();
thRegion2.Join();

I currently have this code (thanks for the help from here). I need to create the first ProcessMessage as a thread and run the second ProcessMessage synchronously (on the current thread), then perform the Join on the single thread. Otherwise, I'll have three threads doing effectively two things. How do I modify this to accomplish it? I am on .NET 3.5

Thread thRegion1 = new Thread(() =>
{
    if (Region1.Trim().Length > 0)
    {
        returnMessage = ProcessTheMessage(string.Format(queueName, Region1));
        Logger.Log(returnMessage);
    }
});

Thread thRegion2 = new Thread(() =>
 {
     if (Region2.Trim().Length > 0)
     {
         returnMessage = ProcessTheMessage(string.Format(queueName, Region2));
         Logger.Log(returnMessage);
     }
 });

thRegion1.Start();
thRegion2.Start();

thRegion1.Join();
thRegion2.Join();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时浪漫 2024-12-07 13:52:36

您可以这样做:

Thread thRegion1 = new Thread(() =>
        {
            if (shawRegion1.Trim().Length > 0)
            {
                returnMessage = ProcessMessage(string.Format(queueName, 
                                                             shawRegion1));
                Logger.Log(returnMessage);
            }
        });

thRegion1.Start();

if (shawRegion2.Trim().Length > 0)
{
    returnMessage = ProcessMessage(string.Format(queueName, shawRegion2));
    Logger.Log(returnMessage);
}

thRegion1.Join();

这将启动 thRegion1 线程并在当前线程中执行其他部分的工作。该工作完成后,它会对 thRegion1 调用 Join,如果 thRegion1 已完成其工作,该操作将立即返回。

You can do it like this:

Thread thRegion1 = new Thread(() =>
        {
            if (shawRegion1.Trim().Length > 0)
            {
                returnMessage = ProcessMessage(string.Format(queueName, 
                                                             shawRegion1));
                Logger.Log(returnMessage);
            }
        });

thRegion1.Start();

if (shawRegion2.Trim().Length > 0)
{
    returnMessage = ProcessMessage(string.Format(queueName, shawRegion2));
    Logger.Log(returnMessage);
}

thRegion1.Join();

This starts the thRegion1 thread and performs the other part of the work in the current thread. After that work is finished, it calls Join on thRegion1 which will return immediately, if thRegion1 is already finished with its work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文