How do i go about counting how many times a messagebox appears in c#?

三世轮回 提交于 2019-12-14 03:23:56

问题


I am trying to get my program to Count how many times a certain messagebox in my program appears while it is running.

I have looked up how to do this and it seems that I will need to use int count but I'm not sure how to code this for Messageboxes as I can only find count code for strings and arrays.


回答1:


It is better idea to make some static wrapper class for MessageBox class and count it there in some kind of static variable.




回答2:


There are two solutions for this

1- create a class Adapter that wraps MessageBox class, add a counter field to the Adapter and increment it in every call

 class MyMessageBox
 {
 static int counter;

 static void Show(string msg)
 {
   counter++;
   MessageBox.Show(msg);
 }
 }

using this approach you assume that users are using your MessageBox

2- The second solution is AOP
use an Aspect Oriented framework like PostSharp to count Show calls



来源:https://stackoverflow.com/questions/15313794/how-do-i-go-about-counting-how-many-times-a-messagebox-appears-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!