新表格没有回应 [英] new form not responding

查看:55
本文介绍了新表格没有回应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我发现此代码存在问题。当我尝试显示newindow时,它的b / b $ b没有响应。它确实创建了新窗口,但根本没有响应。

只是保持空白。我是一个相当新的c#(只有一周的经验),所以它可以

是我不知道的一些基本问题。


请让我知道是否有任何部分不清楚。


命名空间newprogram

{

公共部分类form1:表格

{

public delegate void MessageDelegate(string message);

public static MessageProcessor messageProcessor;

public static Server servermsg = new Server ();

公共SLMessenger()

{

InitializeComponent();

servermsg.onmessage + = new Sender.MsgCallback(ReceiveMessage);

}

私人无效buttonLogin_Click(对象发件人,EventArgs的)

{

backgroundWorkerLogin.RunWorkerAsync();

}

私人无效backgroundWorkerLogin_RunWorkerCompleted(对象发件人,

RunWorkerCompletedEventArgs E)

{

messageProcessor = new MessageProcessor();

}


private void receiveMessage(string message)

{

if(message.Length 0)

{

MessageDelegate M = new

MessageDelegate(messageProcessor.Processmessage);

D(消息);

}

} $ / $

公共类MessageProcessor

{

公共IMWindow neWindow;

public void Processmessage(string message)

{

neWindow = new Form2();

neWindow.Show ();

}

}

}

解决方案

Achilles ____写道:


我发现此代码存在问题。当我尝试显示newindow时,它的b / b $ b没有响应。它确实创建了新窗口,但根本没有响应。

只是保持空白。



我愿意打赌在主线程以外的

线程上引发Server.onmessage事件。由于neWindow,表单获得

在该线程上创建,并且由于该线程没有消息泵,

表单永远不会绘制或执行窗口通常会执行的任何操作。


最好的办法是使用Invoke()来执行实例化表格的代码。例如,在你的receiveMessage()事件

处理程序的某个地方,这样调用:


调用((MethodInvoker)委托()

{messageProcessor.Processmessage();});


您发布的代码有一些其他奇怪的事情:


你似乎有一个BackgroundWorker组件添加到您的表单。

但是,我没有看到DoWork事件处理程序的代码。


在你的receiveMessage中()方法,你创建一个委托实例和

将它存储在一个局部变量M.但是你永远不会使用那个局部变量,

你调用一个方法D ;在你发布的代码中没有定义。


谢谢,

Pete





" Peter Duniho"写道:


Achilles____写道:


我发现此代码存在问题。当我尝试显示newindow时,它的b / b $ b没有响应。它确实创建了新窗口,但根本没有响应。

只是保持空白。



我愿意打赌在主线程以外的

线程上引发Server.onmessage事件。由于neWindow,表单获得

在该线程上创建,并且由于该线程没有消息泵,

表单永远不会绘制或执行窗口通常会执行的任何操作。


最好的办法是使用Invoke()来执行实例化表格的代码。例如,在你的receiveMessage()事件

处理程序的某个地方,这样调用:


调用((MethodInvoker)委托()

{messageProcessor.Processmessage();});


您发布的代码有一些其他奇怪的事情:


你似乎有一个BackgroundWorker组件添加到您的表单。

但是,我没有看到DoWork事件处理程序的代码。


在你的receiveMessage中()方法,你创建一个委托实例和

将它存储在一个局部变量M.但是你永远不会使用那个局部变量,

你调用一个方法D ;在你发布的代码中没有定义。


谢谢,

Pete



是的,你是绝对正确的,Server.message是在另一个线程中创建的。


感谢您提示,我会尝试使用invoke。我使用后台工作者

认为它会使线程安全。猜不是吗?


我只是匆匆复制粘贴片段,请忽略那些错误。它是
应该是DoWork和M。





" Achilles ____"写道:


>


" Peter Duniho"写道:


Achilles____写道:


我发现此代码存在问题。当我尝试显示newindow时,它的b / b $ b没有响应。它确实创建了新窗口,但根本没有响应。

只是保持空白。



我愿意打赌在主线程以外的

线程上引发Server.onmessage事件。由于neWindow,表单获得

在该线程上创建,并且由于该线程没有消息泵,

表单永远不会绘制或执行窗口通常会执行的任何操作。


最好的办法是使用Invoke()来执行实例化表格的代码。例如,在你的receiveMessage()事件

处理程序的某个地方,这样调用:


调用((MethodInvoker)委托()

{messageProcessor.Processmessage();});


您发布的代码有一些其他奇怪的事情:


你似乎有一个BackgroundWorker组件添加到您的表单。

但是,我没有看到DoWork事件处理程序的代码。


在你的receiveMessage中()方法,你创建一个委托实例和

将它存储在一个局部变量M.但是你永远不会使用那个局部变量,

你调用一个方法D ;在你发布的代码中没有定义。


谢谢,

Pete



是的,你是绝对正确的,Server.message是在另一个线程中创建的。


感谢您提示,我会尝试使用invoke。我使用后台工作者

认为它会使线程安全。猜不是吗?


我只是匆匆复制粘贴片段,请忽略那些错误。它是
应该是DoWork和M。



相反,如果我使用委托,它将执行同样的工作对吗?


我必须将服务器消息传递给新表格,猜测调用不会工作

在这种情况下对吗?



Im finding problem with this code. When i try to display the newindow, its
not responding. It does create the new window, but doesnt respond at all.
Just stays blank. I''m fairly new to c#(just a weeks experience), so it could
be some basic issue that im not aware of.

Please let me know if any part is not clear.

namespace newprogram
{
public partial class form1: Form
{
public delegate void MessageDelegate(string message);
public static MessageProcessor messageProcessor;
public static Server servermsg = new Server();
public SLMessenger()
{
InitializeComponent();
servermsg.onmessage += new Sender.MsgCallback(ReceiveMessage);
}
private void buttonLogin_Click(object sender, EventArgs e)
{
backgroundWorkerLogin.RunWorkerAsync();
}
private void backgroundWorkerLogin_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
messageProcessor= new MessageProcessor();
}

private void receiveMessage(string message)
{
if (message.Length 0)
{
MessageDelegate M = new
MessageDelegate(messageProcessor.Processmessage);
D(message);
}
}

}

public class MessageProcessor
{
public IMWindow neWindow;
public void Processmessage(string message)
{
neWindow = new Form2();
neWindow.Show();
}
}
}

解决方案

Achilles____ wrote:

Im finding problem with this code. When i try to display the newindow, its
not responding. It does create the new window, but doesnt respond at all.
Just stays blank.

I am willing to bet that the Server.onmessage event is raised on a
thread other than your main thread. Since the "neWindow" form gets
created on that thread, and since that thread has no message pump, the
form never gets to draw or do anything else that a window normally would do.

Your best bet is to use Invoke() to execute the code that instantiates
the form. For example, somewhere in your receiveMessage() event
handler, call it like this:

Invoke((MethodInvoker)delegate()
{ messageProcessor.Processmessage(); });

There are some other odd things about the code you posted:

You appear to have a BackgroundWorker component added to your form.
However, I don''t see the code for the DoWork event handler.

In your receiveMessage() method, you create a delegate instance and
store it in a local variable M. But you never use that local variable,
and you call a method "D" that isn''t defined in the code you posted.

Thanks,
Pete




"Peter Duniho" wrote:

Achilles____ wrote:

Im finding problem with this code. When i try to display the newindow, its
not responding. It does create the new window, but doesnt respond at all.
Just stays blank.


I am willing to bet that the Server.onmessage event is raised on a
thread other than your main thread. Since the "neWindow" form gets
created on that thread, and since that thread has no message pump, the
form never gets to draw or do anything else that a window normally would do.

Your best bet is to use Invoke() to execute the code that instantiates
the form. For example, somewhere in your receiveMessage() event
handler, call it like this:

Invoke((MethodInvoker)delegate()
{ messageProcessor.Processmessage(); });

There are some other odd things about the code you posted:

You appear to have a BackgroundWorker component added to your form.
However, I don''t see the code for the DoWork event handler.

In your receiveMessage() method, you create a delegate instance and
store it in a local variable M. But you never use that local variable,
and you call a method "D" that isn''t defined in the code you posted.

Thanks,
Pete

Yes you are absolutely right, Server.message is created in another thread.

Thanks for the tip, I''ll try to use invoke. I used the background worker
thinking that it will make the thread safe. Guess not?

I just copy pasted the fragments in hurry, pls ignore those mistakes. It
should have been DoWork and "M".




"Achilles____" wrote:

>

"Peter Duniho" wrote:

Achilles____ wrote:

Im finding problem with this code. When i try to display the newindow, its
not responding. It does create the new window, but doesnt respond at all.
Just stays blank.

I am willing to bet that the Server.onmessage event is raised on a
thread other than your main thread. Since the "neWindow" form gets
created on that thread, and since that thread has no message pump, the
form never gets to draw or do anything else that a window normally would do.

Your best bet is to use Invoke() to execute the code that instantiates
the form. For example, somewhere in your receiveMessage() event
handler, call it like this:

Invoke((MethodInvoker)delegate()
{ messageProcessor.Processmessage(); });

There are some other odd things about the code you posted:

You appear to have a BackgroundWorker component added to your form.
However, I don''t see the code for the DoWork event handler.

In your receiveMessage() method, you create a delegate instance and
store it in a local variable M. But you never use that local variable,
and you call a method "D" that isn''t defined in the code you posted.

Thanks,
Pete


Yes you are absolutely right, Server.message is created in another thread.

Thanks for the tip, I''ll try to use invoke. I used the background worker
thinking that it will make the thread safe. Guess not?

I just copy pasted the fragments in hurry, pls ignore those mistakes. It
should have been DoWork and "M".

Instead if i use a delegate, it will do the same job right?

I have to pass the server messages to the new form, guess invoke wont work
in that case right?


这篇关于新表格没有回应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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