English 中文(简体)
静态多读物中的 In
原标题:Invoke in static multithreading instance

试图做的是,在目前形式的某种位置上取得一种颜色。 但是,我称之为这一方法的时机已经成熟。 当我提出申请时,我有了一个错误:

Cross-thread operation not valid: Control Form1 accessed from a thread other than the thread it was created on.

阅读法:

Thread drawThread;
drawThread = new Thread(drawBikes);

《刑法》:

public void drawBikes()
{
    MessageBox.Show("Bike "+bike.color.ToString()+": "+Form1.ActiveForm.GetPixelColor(bike.location.X, bike.location.Y).ToString());
}

这里是“GetPixelColor”方法(在一个单独的固定类别中):

public static class ControlExts
{
    public static Color GetPixelColor(this Control c, int x, int y)
    {
        var screenCoords = c.PointToScreen(new Point(x, y));
        return Win32.GetPixelColor(screenCoords.X, screenCoords.Y);
    }
}

我在什么地方叫Invoke?

最佳回答

你们需要把Invoke从正在与德国金融情报局互动的任何其他深层中叫出来。 就你而言,“Bikes”(Peoples)正在试图更新国际律师联合会。 引证:

    public void drawBikes()
    {
        if (InvokeRequired)
        {
            this.Invoke(new MethodInvoker(drawBikes));
            return;
        }
        // code below will always be on the UI thread
        MessageBox.Show("Bike "+bike.color.ToString()+": "+Form1.ActiveForm.GetPixelColor(bike.location.X, bike.location.Y).ToString());

    }
问题回答

http://msdn.microsoft.com/en-us/library/system.windows.forms. Control.begininvoke.aspx”rel=“nofollow”

类似于

            public static class ControlExts
            {
                      public static Color GetPixelColor(this Control c, int x, int y)
                      {
                         this.BeginInvoke(new Action(() =>
                          {
                          var screenCoords = c.PointToScreen(new Point(x,y));
                          return Win32.GetPixelColor(screenCoords.X, screenCoords.Y);
                         }));

                       }
           }




相关问题
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. ...

热门标签