简体   繁体   中英

How can i update in live real time listBox adding items from another class?

I have this code in a new class:

private static bool EnumWindowsCallback(IntPtr hWnd, IntPtr lParam)
{
    bool specialCapturing = false;

    if (hWnd == IntPtr.Zero) return false;

    if (!IsWindowVisible(hWnd)) return true;

    if (!countMinimizedWindows)
    {
        if (IsIconic(hWnd)) return true;
    }
    else if (IsIconic(hWnd) && useSpecialCapturing) specialCapturing = true;

    if (GetWindowText(hWnd) == PROGRAMMANAGER) return true;

    windowSnaps.Add(new WindowSnap(hWnd, specialCapturing));
    doStuff(new WindowSnap(hWnd, specialCapturing));

    return true;
}

public static void doStuff(object objtoadd)
{
    foreach (Form frm in Application.OpenForms)
    {
        if (frm.GetType() == typeof(Form1))
        {
            Form1 frmTemp = (Form1)frm;
            frmTemp.addItemToListBox(objtoadd);

        }
    }
}

When running the program it's getting to the line:

doStuff(new WindowSnap(hWnd, specialCapturing));

But it's never pass the line:

if (frm.GetType() == typeof(Form1))

And in form1 i did:

public void addItemToListBox(object item)
{
    listBoxSnap.Items.Add(item);
}

But it's never called the addItemToListBox since it didn't pass the line in the class.

My main goal is to update the listBox in form1(listBoxSnap) in libe real time once in the new class it's adding one item:

frmTemp.addItemToListBox(objtoadd);

Then show/display in the listBoxSnap the item then the next item and so on. Untill now i did:

this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());

But this took time to since it have to wait untill all items added before showing them in the listBoxSnap.

WindowSnap is the new class and GetAllWindows is:

public static WindowSnapCollection GetAllWindows(bool minimized, bool specialCapturring)
{
    windowSnaps = new WindowSnapCollection();
    countMinimizedWindows = minimized;//set minimized flag capture
    useSpecialCapturing = specialCapturring;//set specialcapturing flag
    EnumWindowsCallbackHandler callback = new EnumWindowsCallbackHandler(EnumWindowsCallback);
    EnumWindows(callback, IntPtr.Zero);
    return new WindowSnapCollection(windowSnaps.ToArray(), true);
}

In this method it's calling the EnumWindowsCallback where i want to update the listBoxSnap.

Maybe i need somehow to update the listBoxSnap in the public static WindowSnapCollection GetAllWindows method.

Way too much code for me to gork. But to answer the question.

Since you said real time I have to assume that the callback and DoStuff are running on a non-UI thread. To do anything with a Control you must be on the UI thread. You can use Control.Invoke to do this.

 public static void doStuff(object objtoadd)
 {
    foreach (Form1 frm in Application.OpenForms.OfType<Form1>())
    {
       frm.Invoke(() => frm.addItemToListBox(objtoadd));
    }
  }

Note that OfType<Form1>() is defined in System.Linq .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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