程序问题,似乎很简单...... [英] Program problem, seems simple...

查看:39
本文介绍了程序问题,似乎很简单......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我是C#和微软IDE的菜鸟,但是有相当长的历史

的编程(有,咳嗽,Delphi)。我正在使用

Visual Studio 2005在C#中编写一个移动应用程序。我认为必须有一些基本的东西,我将丢失
并且可以使用指针正确的方向。


以下是测试程序的简化版本。我正在使用从dll文件(在此处用作MyNameSpace)提供的命名空间

。无论如何,我从内部创建了一个

组件,它有一个回调方法。当我启动

进程(button1_click)时,回调就好了。但是,我不能

似乎更新了MainForm上的任何控件。正如您将看到的,我的

回调方法调用了Update Values。呼叫发生,但

标签永远不会更新。然而,启动Button2_Click,然后将调用

UpdateValues并且标签将被更新。


几乎看起来我的组件引用了另一个
MainForm。我怀疑这将是行为,如果我在创建它或回调方法时没有在'b $ b我的组件中加上''this''。但是,我认为我没那么正确。


无论如何,任何人都有线索?谢谢你的帮助


Jim


===================== ============================= ==========


使用System.Data;

使用System.Drawing;

使用System.Text;

使用System.Windows.Forms;

使用MyNameSpace;


命名空间MyTestApp

{

公共部分类MainForm:表格

{

私人MyClass MyVar;


公共MainForm()

{

InitializeComponent ();

Int Val1,Val2;

this.MyVar = new MyClass();

this.MyVar.OnResponse + = new

MyClass.OnResponseHandler(this.MyResponseHandler);

}


private void button1_Click(object sender,EventArgs e)

{

MyVar.SendRequest();

}


private void button2_Click(object sender,EventArgs e )

{

UpdateValues();

}


private void MyResonseH andler(对象发送者,

nsoftware.IPWorks.SnmpmgrResponseEventArgs e)

{

Val1 = System.Int64.Parse(MyVar.Value1);

Val2 = System.Int64.Parse(MyVar.Value2);

UpdateValues();

}


private void UpdateValues()

{

MessageBox.Show(" Updating Values");

label1.Text = Convert .ToString(Val1);

label2.Text = Convert.ToString(Val2);

}


}

}

解决方案

我整理了它并添加了丢失的代码,它运行正常;你肯定

事件正在解雇吗?


即你应该有类似的东西:


公共活动EventHandler响应;

protected void OnResponse(){

EventHandler handler = Response;

if(handler!= null)handler(this,EventArgs) .Empty);

}

public void SendRequest(){

Value1--; //我的随机更改

Value2 ++;

OnResponse();

}


哦 - 按照惯例,事件的名称没有开; 打开

先于用于触发事件的方法 - 通常是受保护的和虚拟的。


另外 - 你可能想看一下windows-forms绑定和制作MyClass

实现INotifyPropertyChanged;这将允许您将MyClass绑定到

UI,无需任何手动布线,但在更新基础

类时获取UI更新。

Marc


Scooby写道:

好的,我是C#和微软'的小伙子IDE,但有相当长的编程历史(有,咳,德尔福)。我正在使用Visual Studio 2005在C#中编写一个移动应用程序。我认为必须有一些我缺失的基本内容,并且可以在正确的方向上使用指针。

以下是测试程序的简化版本。我正在使用从dll文件(在此处用作MyNameSpace)提供的命名空间。无论如何,我从内部创建了一个
组件,它有一个回调方法。当我启动
进程(button1_click)时,回调就好了。但是,我似乎没有
似乎更新MainForm上的任何控件。正如您将看到的,我的回调方法调用了Update Values。呼叫发生,但
标签永远不会更新。但是,启动Button2_Click,然后将调用UpdateValues并更新标签。

几乎看起来我的组件引用了一个不同的MainForm实例。我怀疑这是行为,如果我在创建它或回调方法时没有
我的组件带有''this''。但是,我认为我做对了。

无论如何,任何人都有线索?谢谢你的帮助

Jim

============================= ===================== ==========



< snippedy-doo-dah> ;


嗨Jim,


您的留言箱是否显示?你是如何调用回调的?你是否获得了关于不被允许执行UI代码的任何例外情况?

另一个线程?


如果你有在某处获得了一些很酷的跨线程异步代码,

设法从UI线程以外的线程调用回调,然后

你遇到了问题。你需要在UI线程的

上下文中调用代码。将''UpdateValues''方法重命名为''SafeUpdateValues''和

将以下代码重命名:


///(未经测试 - 我认为我的语法是正确的)

private delegate void SafeUpdateValuesDelegate();

private void UpdateValues()

{

if(this.InvokeRequired)

this.Invoke(new SafeUpdateValuesDelegate(SafeUpdateValues));

else

SafeUpdateValues();

}

///


您仍然应该调用UpdateValues,但这又会执行

正确上下文中的SafeUpdateValues方法。


希望这会有所帮助,

- Tom Spink


< blockquote>另一个想法;它没有显示在你的代码中,但你使用多个

线程?即任何一个:


{delegate} .BeginInvoke

ThreadPool.QueueUserWorkItem

新主题()

其他任何包含开始......的内容在名称中



如果是这样,事件可能是在非UI线程上触发的;任何尝试

与Form'的控件交谈都会抛出异常,这可能是由调用代码吸收的b / b $ b $ - 特别是如果匹配的话结束 ..."是没有

被调用(这本身就代表了泄漏 - 除了

Control.EndInvoke)。


如果是这种情况,回帖后我和(或某人)会告诉你如何在表格的'事件处理程序中切换线程。


Marc


Okay, I''m a noob at C# and microsoft''s IDE, but have a fairly long history
of programming (with, cough, Delphi). I''m writing a mobile app in C# with
Visual Studio 2005. I think there must be something fundamental that I''m
missing and could use a pointer in the right direction.

Below is a simplified version of a test program. I am using a namespace
provided from a dll file (used as MyNameSpace here). Anyway, I create a
component from within, which has a callback method. When I initiate the
process (button1_click), the callback happens just fine. However, I can not
seem to update any of the controls on the MainForm. As you will see, my
callback method has a call to Update Values. The call happens, but the
labels never get updated. However, initiating Button2_Click, will then call
UpdateValues and the labels will be updated.

It almost seems like my component is referencing a different instance of
MainForm. I would suspect this would be the behavior if I had not preceeded
my component with ''this'' when creating it or the callback method. But, I
thought I got that right.

Anyway, anyone have an clues? Thanks for the help

Jim

================================================== ==========

using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyNameSpace;

namespace MyTestApp
{
public partial class MainForm : Form
{
private MyClass MyVar;

public MainForm()
{
InitializeComponent();
Int Val1, Val2;
this.MyVar = new MyClass();
this.MyVar.OnResponse += new
MyClass.OnResponseHandler(this.MyResponseHandler);
}

private void button1_Click(object sender, EventArgs e)
{
MyVar.SendRequest();
}

private void button2_Click(object sender, EventArgs e)
{
UpdateValues();
}

private void MyResonseHandler(object sender,
nsoftware.IPWorks.SnmpmgrResponseEventArgs e)
{
Val1 = System.Int64.Parse(MyVar.Value1);
Val2 = System.Int64.Parse(MyVar.Value2);
UpdateValues();
}

private void UpdateValues()
{
MessageBox.Show("Updating Values");
label1.Text = Convert.ToString(Val1);
label2.Text = Convert.ToString(Val2);
}

}
}

解决方案

I tidied it up and added the missing code, and it works fine; are you sure
that the event is firing?

i.e. you should have something like:

public event EventHandler Response;
protected void OnResponse() {
EventHandler handler = Response;
if (handler != null) handler(this, EventArgs.Empty);
}
public void SendRequest() {
Value1--; // my random changes
Value2++;
OnResponse();
}

Oh - any by convention the event is named without the "On"; the "On"
preceeds the method used to fire the event - often protected and virtual.

Also - you might want to look at windows-forms bindings and making MyClass
implement INotifyPropertyChanged; this would allow you to tie MyClass to the
UI without any manual wiring required, yet get UI updates as the underlying
class is updated.

Marc


Scooby wrote:

Okay, I''m a noob at C# and microsoft''s IDE, but have a fairly long history
of programming (with, cough, Delphi). I''m writing a mobile app in C# with
Visual Studio 2005. I think there must be something fundamental that I''m
missing and could use a pointer in the right direction.

Below is a simplified version of a test program. I am using a namespace
provided from a dll file (used as MyNameSpace here). Anyway, I create a
component from within, which has a callback method. When I initiate the
process (button1_click), the callback happens just fine. However, I can
not
seem to update any of the controls on the MainForm. As you will see, my
callback method has a call to Update Values. The call happens, but the
labels never get updated. However, initiating Button2_Click, will then
call UpdateValues and the labels will be updated.

It almost seems like my component is referencing a different instance of
MainForm. I would suspect this would be the behavior if I had not
preceeded
my component with ''this'' when creating it or the callback method. But, I
thought I got that right.

Anyway, anyone have an clues? Thanks for the help

Jim

================================================== ==========


<snippedy-doo-dah>

Hi Jim,

Does your messagebox get shown? How are you invoking the callback? Are you
getting any exceptions about not being allowed to execute UI code from
another thread?

If you''ve got some cool cross-thread asynchronous code somewhere that
manages to invoke the callback from a thread other than the UI thread, then
you run into a problem. You''ll need to invoke the code in the context of
the UI thread. Rename your ''UpdateValues'' method to ''SafeUpdateValues'' and
put the following code before it:

/// (Untested - I think my syntax is correct)
private delegate void SafeUpdateValuesDelegate ( );
private void UpdateValues ( )
{
if ( this.InvokeRequired )
this.Invoke( new SafeUpdateValuesDelegate( SafeUpdateValues ) );
else
SafeUpdateValues();
}
///

You should still make a call to UpdateValues, but that in turn will execute
the SafeUpdateValues method in the correct context.

Hope this helps,
-- Tom Spink


One other thought; it isn''t shown in your code, but are you using multiple
threads? i.e. any of:

{delegate}.BeginInvoke
ThreadPool.QueueUserWorkItem
new Thread()
Anything else with "Begin..." in the name
?
If so, it it likely that the event is firing on a non-UI thread; any attempt
to talk to the Form''s controls will then throw an exception, which may be
being absorbed by the calling code - especially if a matching "End..." isn''t
being called (which in itself represents a leak - with the exception of
Control.EndInvoke).

If this is the case, post back and me (or somebody) will show you how to
switch threads in the form''s event handler.

Marc


这篇关于程序问题,似乎很简单......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆