1.以下这段代码是在主窗体中的。 private void button1_Click(object sender, EventArgs e)
{
try
{
Class1 c = new Class1();
new Process(c.TestMethod).BeginInvoke(null,null);
}
catch(Exception exp)
{
MessageBox.Show(exp.Message);
}
}
2.这个是在一个新建的类文件中。 public delegate void Process(); public class Class1
{
public void TestMethod()
{
long i = 0;
for (; i < 90000000; i++) ; //拖时间
i = 0;
long j = 100 / i; //会出现异常除0
}
}
如果我用BeginInvoke的话,在TestMethod中出现的异常在主窗体线程中是捕获不到的,大家是怎么做的这部分?而且我发现如果直接运行exe的话,程序没有任何反应,但如果以调试模式运行的话就会出现异常。但无论如何也不能捕获和处理它。
{
try
{
Class1 c = new Class1();
new Process(c.TestMethod).BeginInvoke(null,null);
}
catch(Exception exp)
{
MessageBox.Show(exp.Message);
}
}
2.这个是在一个新建的类文件中。 public delegate void Process(); public class Class1
{
public void TestMethod()
{
long i = 0;
for (; i < 90000000; i++) ; //拖时间
i = 0;
long j = 100 / i; //会出现异常除0
}
}
如果我用BeginInvoke的话,在TestMethod中出现的异常在主窗体线程中是捕获不到的,大家是怎么做的这部分?而且我发现如果直接运行exe的话,程序没有任何反应,但如果以调试模式运行的话就会出现异常。但无论如何也不能捕获和处理它。
解决方案 »
- DataGridView Column格式
- wiform窗体大小自动调整问题
- DataList中使用 HoverMenuExtender无法显示菜单
- 泛型使用报错:未将对象引用设置到对象的实例!望高手请进!
- 在winform 里如何将sql数据库的某个表和其结构导出到access数据库里,同时创建mdb文件
- 我FTP怎么中文都显示的是乱码,怎么解决呀!!!
- Form显示问题!急急急... ...在线等... ...
- 如何判断一个类已经实例化,并获得它的实例(在线等待……)
- 终于装上.net了,可是……
- C#继承窗体的引用文件路径问题
- 如何触发TreeView的TreeNodePopulate事件(各位net高手请进来看看)
- C#怎么从SQL中读出BLOB值。。。谁能教教我啊。。。
public delegate void Process(); public class Class1
{
public void TestMethod()
{
long i = 0;
for (; i < 90000000; i++) ; //拖时间
try
{
i = 0;
long j = 100 / i; //会出现异常除0
}
catch(Exception ex)
{
// Invoke pass to main thread if required here.
}
}
}
Thread.Sleep(0);
// Poll while simulating work.
while(result.IsCompleted == false) {
Thread.Sleep(10);
} // Call EndInvoke to retrieve the results.
string returnValue = caller.EndInvoke(out threadId, result);
try
{
Class1 c = new Class1();
new Process(c.TestMethod).BeginInvoke(null,null);//启动另外一个线程,此函数接着向下执行,不会等待那个线程出现错误
}
catch(Exception exp)
{
MessageBox.Show(exp.Message);
}改为
try{
class1 c = new class1();
Process ps = new Process(c.TestMethod);
IAsyncResult ir = ps.BeginInvoke(null, c);
ps.EndInvoke(ir);
}
catch(Exception ex )
{
MessageBox.Show(ex.ToString());
}
{
Process process = null;
public Form1()
{
InitializeComponent();
Class1 c = new Class1();
process = new Process(c.TestMethod);
} private void button1_Click(object sender, EventArgs e)
{
try
{
process.BeginInvoke(null, null);
}
catch(Exception exp)
{
MessageBox.Show(exp.Message);
}
} private void EndProcess(IAsyncResult ar)
{
try
{
process.EndInvoke(ar);
MessageBox.Show("Task complete!");
}
catch
{
throw;
}
finally
{
this.Invoke(new Process(this.Close));
}
}
}
调试时还会出错。
{
IAsyncResult ir = process.BeginInvoke(null,null);
process.EndInvoke(ir);
}
catch(Exception exp)
{
MessageBox.Show(exp.Message);
}
if (!this.cardTimeBll.Exists(para1.ToString(), Convert.ToDateTime(para4)))
{
if (temp.FCar == string.Empty)
{
temp.FCar = "##";
}
bool isInsert = this.cardTimeBll.Add(temp);
if (!isInsert)
{
temp.FCarState = "##";
}
IAsyncResult iResult = this.listView1.BeginInvoke(new UpdateListViewEventHandler(updateListView), new object[] { temp });
this.listView1.EndInvoke(iResult);
}