c# - How can i update in live real time listBox adding items from another class? -
i have code in 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 program it's getting line:
dostuff(new windowsnap(hwnd, specialcapturing));
but it's never pass line:
if (frm.gettype() == typeof(form1))
and in form1 did:
public void additemtolistbox(object item) { listboxsnap.items.add(item); }
but it's never called additemtolistbox since didn't pass line in class.
my main goal update listbox in form1(listboxsnap) in libe real time once in new class it's adding 1 item:
frmtemp.additemtolistbox(objtoadd);
then show/display in listboxsnap item next item , on. untill did:
this.listboxsnap.items.addrange(windowsnap.getallwindows(true, true).toarray());
but took time since have wait untill items added before showing them in listboxsnap.
windowsnap new class , 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 method it's calling enumwindowscallback want update listboxsnap.
maybe need somehow update listboxsnap in public static windowsnapcollection getallwindows method.
way code me gork. answer question.
since said real time
have assume callback , dostuff
running on non-ui thread. control
must on ui thread. can use control.invoke this.
public static void dostuff(object objtoadd) { foreach (form1 frm in application.openforms.oftype<form1>()) { frm.invoke(() => frm.additemtolistbox(objtoadd)); } }
note oftype<form1>() defined in system.linq
.
Comments
Post a Comment