简体   繁体   English

我如何在循环C#中使用背景工

[英]how do i use background Worker in this for loop c#

How do I use a background worker in this for loop? 如何在此for循环中使用后台工作者?

int tmax = 10;
int xmax = newbitmap.Width;
int ymax = newbitmap.Height;
for (int t = 0; t <= tmax; t += 1)
{
  for (int x = 0; x < xmax; x++)
  {
    for (int y = 0; y < ymax; y++)
    {
      if ((x / xmax) > (t / tmax))
      {
        Color originalco = newbitmap2.GetPixel(x, y);
        outp.SetPixel(x, y, originalco);
      }
      else
      {
        Color originalco3 = newbitmap.GetPixel(x, y); ;
        outp.SetPixel(x, y, originalco3);
      }

    }

    pictureBox1.Image = outp;
  }
}

This loop is a wipe transition from right to left, but it doesn't display the transition. 此循环是从右到左的划像转场,但不显示转场。

That because the backgroundWorker works in a different thread. 那是因为backgroundWorker在不同的线程中工作。 You can use backgroundworker.ReportProgress(0, outp) 您可以使用backgroundworker.ReportProgress(0, outp)

So: You need to register to the event BackgroundWorker.ProgressChanged from the events window in Vistual Studio, or with this line: 因此:您需要从Vistual Studio的事件窗口中注册到事件BackgroundWorker.ProgressChanged ,或使用以下代码行:

backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

The method: 方法:

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        var outp = (Bitmap)e.UserState;
        prictureBox.Image = outp;
    }

Your code sould be then: 您的代码应为:

        int tmax = 10;
        int xmax = newbitmap.Width;
        int ymax = newbitmap.Height;
        for (int t = 0; t <= tmax; t += 1)
        {

            for (int x = 0; x < xmax; x++)
            {
                for (int y = 0; y < ymax; y++)
                {
                    if ((x / xmax) > (t / tmax))
                    {
                        Color originalco = newbitmap2.GetPixel(x, y);
                        outp.SetPixel(x, y, originalco);
                    }
                    else
                    {
                        Color originalco3 = newbitmap.GetPixel(x, y); ;
                        outp.SetPixel(x, y, originalco3);
                    }

                }

                backgroundWorker1.ReportProgress(t, outp);

            }
        }

First, you should use direct pixel manipulation as described here: http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx 首先,您应该按照此处所述使用直接像素操作: http : //msdn.microsoft.com/zh-cn/library/5ey6h79d.aspx

Then, you should use an array as lookup for all your threads which line has already been drawn and which hasn't. 然后,您应该使用一个数组作为所有线程的查找对象,该线程已画线而未画线。 The threads look for a new line in this array and then draw it. 线程在此数组中寻找新行,然后绘制它。 But remember to lock the lookup array! 但是请记住锁定查找数组!

Your Do work method would look something like this: - 您的工作方法将如下所示:-

private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
     Bitmap newbitmap = (Bitmap)e.Argument;

     int tmax = 10;
     int xmax = newbitmap.Width;
     int ymax = newbitmap.Height;
     for (int t = 0; t <= tmax; t += 1)
     {

         for (int x = 0; x < xmax; x++)
         {
              for (int y = 0; y < ymax; y++)
              {
                   if ((x / xmax) > (t / tmax))
                   {
                       Color originalco = newbitmap2.GetPixel(x, y);
                       outp.SetPixel(x, y, originalco);
                   }
                   else
                   {
                       Color originalco3 = newbitmap.GetPixel(x, y); ;
                       outp.SetPixel(x, y, originalco3);
                   }

               }
               pictureBox1.Image = outp;
         }
      }
      bgWorker.ReportProgress(0,outp);
}

Then when your worker reports progress, it would raise the following event where you can safely update the UI: 然后,当您的工作人员报告进度时,它将引发以下事件,您可以在其中安全地更新UI:

private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    //UPDATE YOUR UI HERE
}

You can use the ReportProgress method of Background Worker to update the UI. 您可以使用Background Worker的ReportProgress方法来更新UI。

Read more 阅读更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM