c# - What should I do to freeze parent's thread when custom MessageBox is launched? -
i've created custom messagebox using messageprompt
coding4fun toolkit. problem occurs when run resetdata_click
. expected after launching complexmessage.show
rest of code inside resetdata_click
stops executing while complexmessage
open. occurred different. code executed @ once , doesn't matter user chose in complexmessage
because
if (complexmessage.result)...
is executed. should make complexmessage
act system.windos.messagebox
? means when messagebox
called parent's thread waiting user's decision.
private void resetdata_click(object sender, routedeventargs e) { complexmessage.show("you delete data", "are sure?", true); if (complexmessage.result) { datacontrol.datafilereset(); } } public class complexmessage { private static messageprompt messageprompt; private static bool messageresult; public static void show(string message, string title, bool vibrate) { if (!(!(messageprompt == null) && messageprompt.isopen)) { messageprompt = new messageprompt { title = title, message = message }; messageprompt.completed += new eventhandler<popupeventargs<string, popupresult>>(messageprompt_completed); messageprompt.iscancelvisible = true; messageprompt.show(); if (vibrate) { tools.vibratemessage(); } } } static void messageprompt_completed(object sender, popupeventargs<string, popupresult> e) { if (!e.popupresult.equals(popupresult.cancelled)) { messageresult = true; } else { messageresult = false; } ((messageprompt)sender).completed -= messageprompt_completed; } public static bool result { { return messageresult; } } }
since displaying messagebox click event, running on ui thread, don't want freeze.
one option make complexmessage expose static event, fires in messageprompt_completed.
then in resetdata_click subscribe event prior calling complexmessage.show, , in event handler, depending on result, call datacontrol.datafilereset, , unsubscribe.
an alternative rethink making members of complexmessage static, , instead pass "action<bool> callback" parameter show method, store away in private member, , invoke callback in messageprompt_completed.
Comments
Post a Comment