帮助我线程。 [英] Help me with threads.

查看:56
本文介绍了帮助我线程。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我的旧脑是否可以处理线程。这段代码看起来很好

合理。


问候,

杰克


使用系统;

使用System.Diagnostics;

使用System.IO;

使用System.Threading;

名称空间TestThreadedConsole

{

///< summary>

/// Class1的摘要说明。

/ //< / summary>

class Class1

{

StreamReader my_cout;

StreamReader my_cerr;

StreamWriter my_cin;

处理myProcess = new Process();

线程thread_cout;

线程thread_cin;

Thread thread_cerr;

Thread thread_loop;

public void TryToExit()

{

while (!myProcess.HasExited)

{

Thread.Sleep(100);

}

if(thread_cout .IsAlive){thread_cout.Abort();}

if(thread_cin.IsAlive){thread_cin.Abort();}

if(thread_cerr.IsAlive){thread_cerr。中止( );}

myProcess.Close();

}

public void method_cout()

{
myProcess.HasExited)/ b
$


}

public void method_cin()

{

while(!myProcess.HasExited)

{

my_cin.WriteLine(Console.ReadLine());

}

}

public void method_cerr()

{

while(!myProcess.HasExited)

{

Console.WriteLine( my_cerr.ReadLine());

}

}

///< summary>

// /应用程序的主要入口点。

///< / summary>

[STAThread]

static void Main(string [ ] args)

{

//// TODO:添加代码在这里开始申请//

///

new Class1();

}

public Class1()

{

ProcessStartInfo myProcessStartInfo =

新进程sStartInfo(QUOT; ConsoleServer.exe" );
myProcessStartInfo.UseShellExecute = false;

myProcessStartInfo.RedirectStandardOutput = true;

myProcessStartInfo.RedirectStandardError = true;

myProcessStartInfo.RedirectStandardInput = true;

//myProcessStartInfo.Arguments="" ;;

myProcess.StartInfo = myProcessStartInfo;

myProcess .Start();

my_cout = myProcess.StandardOutput;

my_cerr = myProcess.StandardError;

my_cin = myProcess.StandardInput;

thread_cout = new Thread(new ThreadStart(this.method_cout));

thread_cout.IsBackground = true;

thread_cin = new Thread(new ThreadStart(this .method_cin));

thread_cin.IsBackground = true;

thread_cerr = new Thread(new ThreadStart(this.method_cerr));

thread_cerr .IsBackground = true;

thread_loop = new Thread(new ThreadStart(this.TryToExit));

thread_cout.Start();

thread_cin .Start();

线程_cerr.Start();

thread_loop.Start();

}

}

}



***通过开发人员指南 http:// www发送.developersdex.com ***

不要只是参加USENET ......获得奖励!

Well I wonder if my old brain can handle threading. Dose this code look
reasonable.

Regards,
Jeff

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace TestThreadedConsole
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
StreamReader my_cout;
StreamReader my_cerr;
StreamWriter my_cin;
Process myProcess = new Process();
Thread thread_cout;
Thread thread_cin;
Thread thread_cerr;
Thread thread_loop;
public void TryToExit()
{
while (!myProcess.HasExited)
{
Thread.Sleep(100);
}
if (thread_cout.IsAlive){thread_cout.Abort();}
if (thread_cin.IsAlive) {thread_cin.Abort();}
if (thread_cerr.IsAlive) {thread_cerr.Abort();}
myProcess.Close();
}
public void method_cout()
{
while(!myProcess.HasExited)
{
Console.WriteLine(my_cout.ReadLine());
}
}
public void method_cin()
{
while(!myProcess.HasExited)
{
my_cin.WriteLine(Console.ReadLine());
}
}
public void method_cerr()
{
while(!myProcess.HasExited)
{
Console.WriteLine(my_cerr.ReadLine());
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//// TODO: Add code to start application here//
///
new Class1();
}
public Class1()
{
ProcessStartInfo myProcessStartInfo =
new ProcessStartInfo("ConsoleServer.exe" );
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.RedirectStandardError= true;
myProcessStartInfo.RedirectStandardInput= true;
//myProcessStartInfo.Arguments= "";
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
my_cout= myProcess.StandardOutput;
my_cerr= myProcess.StandardError;
my_cin= myProcess.StandardInput;
thread_cout= new Thread(new ThreadStart(this.method_cout));
thread_cout.IsBackground= true;
thread_cin= new Thread(new ThreadStart(this.method_cin));
thread_cin.IsBackground= true;
thread_cerr= new Thread(new ThreadStart(this.method_cerr));
thread_cerr.IsBackground= true;
thread_loop= new Thread(new ThreadStart(this.TryToExit));
thread_cout.Start();
thread_cin.Start();
thread_cerr.Start();
thread_loop.Start();
}
}
}


*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!

推荐答案

你可能会向帮助这个新闻组的人提出太多要求。


如果你发布了几十行代码而只是问这看起来合理吗 ;,

你不太可能得到答案。你必须提供更多信息:

*你想要达到什么目的?

*你是如何设置的(一般的想法)

*什么不能按预期工作。

那么,你可能会得到一些答案。


布鲁诺。


杰夫路易 < JE ******** @ yahoo.com> écritdansle message de news:

%2 ****************** @ TK2MSFTNGP10.phx.gbl ...
You are probably asking too much from the people who help on this newsgroup.

If you post tens of lines of code and just ask "does this look reasonable",
you are not very likely to get answers. You must give more info:
* what are you trying to achieve?
* how did you set it up (the general idea)
* what is not working as expected.
Then, you may get some answers.

Bruno.

"Jeff Louie" <je********@yahoo.com> a écrit dans le message de news:
%2******************@TK2MSFTNGP10.phx.gbl...
我想知道我的旧脑是否可以处理线程。这段代码看起来很合理。

问候,
Jeff

使用System;
使用System.Diagnostics;
使用System.IO;
使用System.Threading;
命名空间TestThreadedConsole
{
///< summary>
/// Class1的摘要描述。
///< / summary>
类Class1
{StreamReader my_cout;
StreamReader my_cerr;
StreamWriter my_cin;
处理myProcess = new Process ();
线程thread_cout;
线程thread_cin;
线程thread_cerr;
线程thread_loop;
public void TryToExit()
{
while (!myProcess.HasExited)
{
Thread.Sleep(100);
}
if(thread_cout.IsAlive){thread_cout.Abort();}
if (thread_cin.IsAlive){thread_cin.Abort();}
if(thread_cerr.IsAlive){thread_cerr.Abort();}
myProcess.Close();
}
public void method_cout()
{while /(myProcess.HasExited)
{
Console.WriteLine(my_cout.ReadLi) ne());
}
}
public void method_cin()
{while /(myProcess.HasExited)
{
my_cin .WriteLine(Console.ReadLine());
}
}
public void method_cerr()
{while /(myProcess.HasExited)
{
Console.WriteLine(my_cerr.ReadLine());
}
}
///< summary>
///应用程序的主要入口点。
///< / summary>
[STAThread]
static void Main(string [] args)
//
// t TODO:添加代码在这里开始申请//
///
新的Class1();
}
公共Class1()
{
ProcessStartInfo myProcessStartInfo =
new ProcessStartInfo(" ConsoleServer.exe" };
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.RedirectStandardError = true;
myProcessStartInfo.RedirectStandardInput = true;
// myProcessStartInfo。 Arguments ="";
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
my_cout = myProcess.StandardOutput;
my_cerr = myProcess.StandardError;
my_cin = myProcess.StandardInput;
thread_cout = new Thread(new ThreadStart(this.method_cout));
thread_cout.IsBackground = true;
thread_cin = new Thread(new ThreadStart(this.method_cin) ));
thread_cin.IsBackground = true;
thread_cerr = new Thread(new ThreadStart(this.method_cerr));
thread_cerr.IsBackground = true;
thread_loop = new Thread( new ThreadStart(this.TryToExit));
thread_cout.Start();
thread_cin.Start();
thread_cerr.Start();
thread_loop.Start();
}
}
}

***通过De发送velopersdex http://www.developersdex.com ***
唐''只是参加USENET ...获得奖励!
Well I wonder if my old brain can handle threading. Dose this code look
reasonable.

Regards,
Jeff

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace TestThreadedConsole
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
StreamReader my_cout;
StreamReader my_cerr;
StreamWriter my_cin;
Process myProcess = new Process();
Thread thread_cout;
Thread thread_cin;
Thread thread_cerr;
Thread thread_loop;
public void TryToExit()
{
while (!myProcess.HasExited)
{
Thread.Sleep(100);
}
if (thread_cout.IsAlive){thread_cout.Abort();}
if (thread_cin.IsAlive) {thread_cin.Abort();}
if (thread_cerr.IsAlive) {thread_cerr.Abort();}
myProcess.Close();
}
public void method_cout()
{
while(!myProcess.HasExited)
{
Console.WriteLine(my_cout.ReadLine());
}
}
public void method_cin()
{
while(!myProcess.HasExited)
{
my_cin.WriteLine(Console.ReadLine());
}
}
public void method_cerr()
{
while(!myProcess.HasExited)
{
Console.WriteLine(my_cerr.ReadLine());
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//// TODO: Add code to start application here//
///
new Class1();
}
public Class1()
{
ProcessStartInfo myProcessStartInfo =
new ProcessStartInfo("ConsoleServer.exe" );
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.RedirectStandardError= true;
myProcessStartInfo.RedirectStandardInput= true;
//myProcessStartInfo.Arguments= "";
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
my_cout= myProcess.StandardOutput;
my_cerr= myProcess.StandardError;
my_cin= myProcess.StandardInput;
thread_cout= new Thread(new ThreadStart(this.method_cout));
thread_cout.IsBackground= true;
thread_cin= new Thread(new ThreadStart(this.method_cin));
thread_cin.IsBackground= true;
thread_cerr= new Thread(new ThreadStart(this.method_cerr));
thread_cerr.IsBackground= true;
thread_loop= new Thread(new ThreadStart(this.TryToExit));
thread_cout.Start();
thread_cin.Start();
thread_cerr.Start();
thread_loop.Start();
}
}
}


*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!



Jeff Louie写道:
Jeff Louie wrote:
我想知道我的老了大脑可以处理线程。这个代码的剂量
看起来合理。
Well I wonder if my old brain can handle threading. Dose this code look reasonable.




看起来合理,但是:


1.为什么要放主Class1'的构造函数中的代码??

2.为什么不将method_cin(),method_cerr()和method_cout()合并为一个

函数?

3.我记得有一些方法来设置当前输入,输出和错误,

这意味着你可以从子进程重定向到/或输出到

直接输入/输出(如果您不想进行任何修改)。



It seems reasonable, but:

1.Why put the main code in Class1''s constructor??
2.Why not merge method_cin(), method_cerr(), and method_cout() into one
function?
3.I remember there is some method to set the current in, out, and err,
which means that you may redirect in/out from the child process to the
current in/out directly (if you don''t want to make any modification).


Aquila ... Inline。
Aquila... Inline.
1.为什么将主代码放在Class1的构造函数中?<
实际上,我有相反的问题。为什么样本将代码放在

main中,因为不能保证main中的代码会执行
?如果Class1是从GUI调用的多线程Model类,则不会调用Class1中的
main。 (我几乎只使用main进行单元

测试。)2。为什么不将method_cin(),method_cerr()和method_cout()合并为一个
1.Why put the main code in Class1''s constructor??< Actually, I have the opposite question. Why do samples put the code in
main since there is no guarantee that the code in main will ever
execute? If Class1 is a multithreaded Model class called from a GUI,
main in Class1 will not be called. (I pretty much just use main for unit
testing.)2.Why not merge method_cin(), method_cerr(), and method_cout() into one



功能?<

我相信这些功能可能会陷入僵局。


问候,

Jeff

***通过开发人员指南 http://www.developersdex.com 发送***

不要只是参加USENET ......获得奖励!


function?<
I believe these functions can deadlock.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


这篇关于帮助我线程。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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