如何在控制台应用程序中同时执行两项任务C# [英] How Do I Do Two Task At One Time In Console Application C#
本文介绍了如何在控制台应用程序中同时执行两项任务C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在控制台应用程序中同时执行两项任务c#
例如: -
i希望在完成其他任务时执行此方法执行其他任务。
how do i do two task at one time in console application c#
for example:-
i want to do some other task with this method being performed while the other task is been completed.
public void task(string[] args)
{
string str = "";
for (;;)
{
for (int i = 0; i < 3; i++)
{
str += ".";
Console.Write("\rPerforming Some Task{0} ", str);
if (i <= 2)
Thread.Sleep(1000);
if (i == 2)
{
Console.Write("\b\b\b ");
str = "";
}
}
}
}
请帮助
please help
推荐答案
在最基本的层面上你可以使用一个新的线程,或者你可以使用BackgroundWorker,这一切都取决于你在主线程和子线程之间需要什么级别的通信和控制。多线程是一个相当深入的主题,因此您可能需要阅读它。多线程的大多数问题涉及同步线程,或者一个线程告诉另一个线程停止运行,当你需要加入另一个线程等时。这是一个非常基本的例子。
At the most basic level you can use a new Thread, or you can use BackgroundWorker, it all depends on what level of communication and control you need between the main thread and the child thread. Multi-threading is a fairly indepth subject so you might need to read up about it. Most problems with multi-threading involve synchronising the threads, or one thread telling another thread to stop running, when you need to "join" the other thread etc. Here is a very basic example though.
public static void DoSomething()
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(500);
Console.WriteLine("DoSomething {0}", i);
}
}
public static void task(string[] args)
{
Thread t = new Thread(new ThreadStart(DoSomething));
t.Start();
string str = "";
for (;;)
{
for (int i = 0; i < 3; i++)
{
str += ".";
Console.Write("\rPerforming Some Task{0} ", str);
if (i <= 2)
Thread.Sleep(1000);
if (i == 2)
{
Console.Write("\b\b\b ");
str = "";
}
}
}
}
这是一个保持所有的解决方案子线程处理
Here is a solution that keeps all processing on child threads
class Program
{
private static bool running = false;
static void Main(string[] args)
{
Thread t = new Thread(new ParameterizedThreadStart(task));
t.Start(args);
// wait for the [RETURN] key to be pressed
Console.ReadLine();
// set flag that tells the other threads to stop
running = false;
// wait for t to finish
t.Join();
}
public static void DoSomething()
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(500);
Console.WriteLine("DoSomething {0}", i);
}
}
public static void task(object a)
{
running = true;
string[] args = (string[])a;
Thread t = new Thread(new ThreadStart(DoSomething));
t.Start();
string str = "";
while (running)
{
for (int i = 0; i < 3; i++)
{
str += ".";
Console.Write("\rPerforming Some Task{0} ", str);
if (i <= 2)
Thread.Sleep(1000);
if (i == 2)
{
Console.Write("\b\b\b ");
str = "";
}
}
}
}
}
要做到这一点,你需要看看线程:这并不简单,特别是对于Console应用程序,许多调用往往会阻塞。
看看BackgroundWorker class [ ^ ] - 这是获取简单方法之一开始。
To do that, you need to look at Threading: and that's not trivial, particularly with Console apps, where many calls tend to be blocking.
Have a look at the BackgroundWorker class[^] - it's one of the simple ways to get started.
阅读本文:使用C#进行多线程编程 [ ^ ]
还: http://stackoverflow.com/questions/1506838/backgroundworker-vs-background-thread [ ^ ]
Read this article: Multithreaded Programming Using C#[^]
also: http://stackoverflow.com/questions/1506838/backgroundworker-vs-background-thread[^]
这篇关于如何在控制台应用程序中同时执行两项任务C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文