这个Application.Run()是否正确? [英] Is this Application.Run() correct?

查看:72
本文介绍了这个Application.Run()是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效,但我不知道为什么。


静态无效主要()

{

/ /将任务排队。

ThreadPool.QueueUserWorkItem(新的WaitCallback(ThreadProc),新的

Form1());

Application.Run() ;

}


static void ThreadProc(对象stateInfo)

{

//当没有state对象被传递给QueueUserWorkItem,所以

// stateInfo为null。

Form1 f1 =(Form1)stateInfo; //传递的表格

f1.notifyIcon1.Icon = new

Icon(System.Reflection.Assembly.GetExecutingAssemb ly()。GetManifestResourceStream(" Listener1.connecti ng。 ico"));

clsListening myLis = new clsListening(f1);

myLis.StartListening(f1);

}


我必须这样做,因为我无法通过this到了班级

,因为ThreadProc是静态的,新的WaitCallBack(ThreadProc)

需要一个静态的方法。


我不喜欢什么我不明白线程部分如何在

Application.Run()之前运行。怎么能在运行之前执行代码?


另外,让我看看我是否正确。 ThreadPool.QueueUserWorkItem

将一个实例化的新Form1()发送给ThreadProc。 stateInfo

成为该对象。然后我将该对象分配给Form1类型f1

(转换为Form1,所以我知道什么样的对象(对象stateInfo))

然后我开始工作我的表格。


它有效,但我在这里是否正确?


谢谢你的帮助。

This works, but I don''t know why.

static void Main()
{
// Queue the task.
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc),new
Form1());
Application.Run();
}

static void ThreadProc(Object stateInfo)
{
// When no state object was passed to QueueUserWorkItem, so
// stateInfo is null.
Form1 f1 = (Form1) stateInfo; //the passed form
f1.notifyIcon1.Icon = new
Icon(System.Reflection.Assembly.GetExecutingAssemb ly().GetManifestResourceStream("Listener1.connecti ng.ico"));
clsListening myLis = new clsListening(f1);
myLis.StartListening(f1);
}

I had to do it this way because I could not pass "this" to the class
because ThreadProc was static and new WaitCallBack(ThreadProc)
required a static method.

What I don''t understand is how the thread part can run before the
Application.Run() can. How can it be executing code before it is
running?

Also, let me see if I am correct. The ThreadPool.QueueUserWorkItem
sends the an instantiated new Form1() to the ThreadProc. stateInfo
becomes that object. I then assign that object to the type Form1 f1
(cast to Form1 so I know what kind of object (object stateInfo) is)
and then I get to work with my form.

It works, but am I correct here?

Thank you for helping me.

推荐答案

jm< jo ************* @ yahoo.com>写道:
jm <jo*************@yahoo.com> wrote:
这有效,但我不知道为什么。

static void Main()
//
//排队任务。
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc),new
Form1());
Application.Run();
}
静态void ThreadProc (对象stateInfo)
//
//当没有状态对象传递给QueueUserWorkItem时,所以
// stateInfo为空。
Form1 f1 =(Form1)stateInfo; //传递的表格
f1.notifyIcon1.Icon = new
Icon(System.Reflection.Assembly.GetExecutingAssemb ly()。GetManifestRes
sourceStream(" Listener1.connecting.ico")) ;
clsListening myLis = new clsListening(f1);
myLis.StartListening(f1);
}
我必须这样做,因为我无法通过"这"因为ThreadProc是静态的,而新的WaitCallBack(ThreadProc)需要一个静态的方法。

我不明白的是线程部分如何运行之前
Application.Run()可以。如何在代码运行之前执行代码?


线程池独立于消息队列运行。 Application.Run

只是为给定的表单启动一个消息泵,并等到它关闭。

另外,让我看看是否我是对的。 ThreadPool.QueueUserWorkItem
将实例化的新Form1()发送到ThreadProc。 stateInfo
成为那个对象。然后我将该对象分配给Form1 f1类型
(强制转换为Form1,因此我知道什么样的对象(对象stateInfo))
然后我开始使用我的表单。

它有效,但我在这里是否正确?
This works, but I don''t know why.

static void Main()
{
// Queue the task.
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc),new
Form1());
Application.Run();
}

static void ThreadProc(Object stateInfo)
{
// When no state object was passed to QueueUserWorkItem, so
// stateInfo is null.
Form1 f1 = (Form1) stateInfo; //the passed form
f1.notifyIcon1.Icon = new
Icon(System.Reflection.Assembly.GetExecutingAssemb ly().GetManifestRes
ourceStream("Listener1.connecting.ico"));
clsListening myLis = new clsListening(f1);
myLis.StartListening(f1);
}

I had to do it this way because I could not pass "this" to the class
because ThreadProc was static and new WaitCallBack(ThreadProc)
required a static method.

What I don''t understand is how the thread part can run before the
Application.Run() can. How can it be executing code before it is
running?
The thread pool runs separately of the message queue. Application.Run
just starts a message pump for the given form, and waits until it
closes down.
Also, let me see if I am correct. The ThreadPool.QueueUserWorkItem
sends the an instantiated new Form1() to the ThreadProc. stateInfo
becomes that object. I then assign that object to the type Form1 f1
(cast to Form1 so I know what kind of object (object stateInfo) is)
and then I get to work with my form.

It works, but am I correct here?




除了一些术语,这基本上是正确的,是的。


你为什么这样做,出于兴趣?


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该组,请不要给我发邮件



Apart from some terminology, that''s basically right, yes.

Why are you doing it, out of interest?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Jon Skeet [C#MVP]< sk *** @ pobox.com> ;写道:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
我不明白的是,线程部分可以在
Application.Run()之前运行。如何在代码运行之前执行代码?
What I don''t understand is how the thread part can run before the
Application.Run() can. How can it be executing code before it is
running?



线程池独立于消息队列运行。 Application.Run
只是为给定的表单启动一个消息泵,等待它关闭。



The thread pool runs separately of the message queue. Application.Run
just starts a message pump for the given form, and waits until it
closes down.

另外,让我看看我是否正确。 ThreadPool.QueueUserWorkItem
将实例化的新Form1()发送到ThreadProc。 stateInfo
成为那个对象。然后我将该对象分配给Form1 f1类型
(强制转换为Form1,因此我知道什么样的对象(对象stateInfo))
然后我开始使用我的表单。

它有效,但我在这里是否正确?
Also, let me see if I am correct. The ThreadPool.QueueUserWorkItem
sends the an instantiated new Form1() to the ThreadProc. stateInfo
becomes that object. I then assign that object to the type Form1 f1
(cast to Form1 so I know what kind of object (object stateInfo) is)
and then I get to work with my form.

It works, but am I correct here?




< snip>


我'我只是再看一下代码,你正在做什么*不是*

好​​吧 - 你不应该从任何其他线程触摸表单

比创建它的那个。你不应该从线程池线程中更改图标等




-

Jon Skeet - < sk***@pobox.com>
http://www.pobox。 com / ~siget

如果回复小组,请不要给我发邮件



<snip>

I''ve just had another look at the code, and what you''re doing it *not*
all right - you shouldn''t be touching the form from any thread other
than the one which created it. You shouldn''t be changing the icons etc
from a threadpool thread.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Jon Skeet [C#MVP]< ; SK *** @ pobox.com>在消息新闻中写道:< MP ************************ @ msnews.microsoft。 com> ...
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Jon Skeet [C#MVP]< sk *** @ pobox.com>写道:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
我不明白的是,线程部分可以在
Application.Run()之前运行。如何在代码运行之前执行代码?
What I don''t understand is how the thread part can run before the
Application.Run() can. How can it be executing code before it is
running?



线程池独立于消息队列运行。 Application.Run
只是为给定的表单启动一个消息泵,等待它关闭。



The thread pool runs separately of the message queue. Application.Run
just starts a message pump for the given form, and waits until it
closes down.

另外,让我看看我是否正确。 ThreadPool.QueueUserWorkItem
将实例化的新Form1()发送到ThreadProc。 stateInfo
成为那个对象。然后我将该对象分配给Form1 f1类型
(强制转换为Form1,因此我知道什么样的对象(对象stateInfo))
然后我开始使用我的表单。

它有效,但我在这里是否正确?
Also, let me see if I am correct. The ThreadPool.QueueUserWorkItem
sends the an instantiated new Form1() to the ThreadProc. stateInfo
becomes that object. I then assign that object to the type Form1 f1
(cast to Form1 so I know what kind of object (object stateInfo) is)
and then I get to work with my form.

It works, but am I correct here?



< snip>

我刚看了一眼代码,以及你在做什么*不是*
好吧 - 你不应该从创建它的任何其他线程触摸表单。你不应该从线程池线程中更改图标等。



<snip>

I''ve just had another look at the code, and what you''re doing it *not*
all right - you shouldn''t be touching the form from any thread other
than the one which created it. You shouldn''t be changing the icons etc
from a threadpool thread.




我以为我只是在Threadpool中创建它,因为我没有在

Application.Run()。



I thought I only created it in the Threadpool since I didn''t do it in
Application.Run().


这篇关于这个Application.Run()是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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