事件处理程序功能会忽略某些功能 [英] Event handler function ignores some functions
问题描述
我已经成功运行了Form2.Show(); objApp.Quit()在调试模式下运行,并在button1.Hide()处设置一个断点;然后按F10继续执行以下两个函数,但是当我运行它时,只需执行button1.Hide()并忽略其他两个函数.甚至没有错误或异常.请帮助
i have successfully ran Form2.Show(); objApp.Quit() functions in debug mode and putting a break point at button1.Hide();then proceeding by F10 the following two functions execute successfully but when i run it just execute button1.Hide() and ignores the other two. not even an error or exception. plz Help
public partial class Form1 : Form
{
PowerPoint.Application objApp;
PowerPoint.Presentations objPresSet;
PowerPoint._Presentation objPres;
PowerPoint.Slides objSlides;
PowerPoint._Slide objSlide;
public Form1()
{
InitializeComponent();
UserActivityHook actHook;
objApp = new PowerPoint.Application();
actHook = new UserActivityHook(); // crate an instance with global hooks
// hang on events
actHook.KeyPress += new KeyPressEventHandler(MyKeyPress);
objPresSet = objApp.Presentations;
objPres = objPresSet.Add(MsoTriState.msoTrue);
objSlides = objPres.Slides;
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new Form1());
}
// UserActivityHook actHook;
public void MyKeyPress(object sender, KeyPressEventArgs e)
{
Form2 Form2 = new Form2();
button1.Hide(); //executes normally
Form2.Show(); //no action
objApp.Quit(); // no action
}
private void button1_Click(object sender, EventArgs e)
{
Form2 Form2 = new Form2();
Form2.Show();
}
}
我已经成功运行了Form2.Show(); objApp.Quit()在调试模式下运行,并在button1.Hide()处设置一个断点;然后按F10继续执行以下两个函数,但是当我运行它时,只需执行button1.Hide()并忽略其他两个函数.甚至没有错误或异常. plz帮助
i have successfully ran Form2.Show(); objApp.Quit() functions in debug mode and putting a break point at button1.Hide();then proceeding by F10 the following two functions execute successfully but when i run it just execute button1.Hide() and ignores the other two. not even an error or exception. plz Help
推荐答案
该异常可能被UserActivityHook
类捕获.尝试将try/catch块放入MyKeyPress
:
The exception is probably caught byUserActivityHook
class. Try to put a try/catch block insideMyKeyPress
:
public void MyKeyPress(object sender, KeyPressEventArgs e)
{
try
{
Form2 Form2 = new Form2();
button1.Hide(); //executes normally
Form2.Show(); //no action
objApp.Quit(); // no action
}
catch (Exception exception)
{
//check here what is going on
...
}
}
-----------------------------
一个例外是由于应用程序正在调度输入同步调用,因此无法进行传出呼叫",我在Google上进行了搜索,发现它与com对象有关,但我无法理解错误是.
我想您不应该在COM处理程序中进行任何UI调用.尝试这样的事情:
-----------------------------
the exception is "An outgoing call cannot be made since the application is dispatching an input-synchronous call" i googled it and figured out it has something to do with com objects in general but i couldn''t understand where exactly the error is.
I suppose you shoudln''t do any UI calls from within the COM handler. Try something like that:
public void MyKeyPress(object sender, KeyPressEventArgs e)
{
BeginInvoke((Action)delegate()
{
Form2 Form2 = new Form2();
button1.Hide();
Form2.Show();
objApp.Quit();
});
}
为什么要在表单类中显示Main()
方法?
您需要执行Olivier建议的操作,同时学习如何使用调试器(Visual Studio附带).
Why are you showing theMain()
method in the form class?
You need to do what Olivier sugegsted, and at the same time, learn how to use the debugger (it comes with Visual Studio).
这篇关于事件处理程序功能会忽略某些功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!