Multithreading question

mikecarter

New member
Joined
Apr 16, 2008
Messages
4
Programming Experience
3-5
Hi All,

I am pretty new to multithreading and need a little help

I am writing a program which reads the colour of multiple pixels on the screen.

I want to read the colour of these pixels in the background and when the thread has finished reading the colour and the colour has changed, to report the colour (raise an event or something like that) and then start the process again (perhaps with a small delay, maybe 100ms).

So in summary:
1. Background process starts
2. reads the pixel colour from x, y coordinates
3. checks if the colour has changed
4. if so, reports the new colour and restarts process
5. if no change, just restart the process

I current have the code for detecting screen colour in a module:

VB.NET:
Option Strict On

Module modColourDetection
    'Win32 API declarations 
    Private Declare Function GetPixel Lib "gdi32.dll" (ByVal hdc As Microsoft.Win32.SafeHandles.SafeFileHandle, ByVal nXPos As Int32, ByVal nYPos As Int32) As Int32
    Private Declare Function GetDC Lib "user32.dll" (ByVal hWnd As IntPtr) As Microsoft.Win32.SafeHandles.SafeFileHandle
    Private Declare Function ReleaseDC Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal hdc As Microsoft.Win32.SafeHandles.SafeFileHandle) As Microsoft.Win32.SafeHandles.SafeFileHandle
    Private Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Microsoft.Win32.SafeHandles.SafeFileHandle) As Boolean

    Public m_hdc As Microsoft.Win32.SafeHandles.SafeFileHandle = Nothing

    Public Structure ColorName
        Public Color As Color
        Public Name As String
        Public Distance As Integer
    End Structure

    Public Function getScreenColour(ByVal x As Integer, ByVal y As Integer) As Color
        Dim res As Color = Nothing

        m_hdc = GetDC(IntPtr.Zero)

        res = ColorTranslator.FromWin32(GetPixel(m_hdc, x, y))

        ReleaseDC(IntPtr.Zero, m_hdc)
        CloseHandle(m_hdc)

        Return res
    End Function

    Public Function FindNearestKnown(ByVal c As Color) As ColorName
        Dim colBest As ColorName

        colBest.Name = Nothing
        colBest.Color = Nothing
        colBest.Distance = Nothing

        For Each colName As String In [Enum].GetNames(GetType(KnownColor))
            Dim colKnown As Color = Color.FromName(colName)
            Dim colDist As Integer = Nothing

            colDist = Math.Abs(CInt(c.R) - colKnown.R) + Math.Abs(CInt(c.G) - colKnown.G) + Math.Abs(CInt(c.B) - colKnown.B)

            If colBest.Name Is Nothing OrElse colDist < colBest.Distance Then
                colBest.Color = colKnown
                colBest.Name = colName
                colBest.Distance = colDist
            End If

            'dispose
            colName = Nothing
            colKnown = Nothing
            colDist = Nothing
        Next

        Return colBest
    End Function
End Module

And the following uses the pixel information and updates background colours and label text accordingly:

VB.NET:
    Private Sub GetColour(ByVal xPos As Integer, ByVal yPos As Integer)
        Dim colColour As Color = Nothing
        Dim colNearestColour As ColorName = Nothing

        Do
            colColour = getScreenColour(xPos, yPos)
            colNearestColour = FindNearestKnown(colColour)

            'update
            pbColour.BackColor = colColour
            lblColour.Text = colColour.Name.ToString
            pbNearestColour.BackColor = colNearestColour.Color
            lblNearestColour.Text = colNearestColour.Name

            System.Threading.Thread.Sleep(50)
        Loop
    End Sub


Any help would be greatly appreciated!

Thanks, :D
Mike
 
Back
Top