.net c #: создание асинхронного процесса - PullRequest
5 голосов
/ 23 февраля 2010

Я создаю консольное приложение, в котором мне нужно вызвать метод, который будет выполнять список заданий. Но я хочу, чтобы метод не оказывал никакого влияния на основное приложение, он просто выполняет свою работу ... и все.

Ответы [ 2 ]

1 голос
/ 23 февраля 2010

Взгляните на эту статью .

Пример:

using System;
using System.Threading;

public class ServerClass
{
    // The method that will be called when the thread is started.
    public void InstanceMethod()
    {
        Console.WriteLine(
            "ServerClass.InstanceMethod is running on another thread.");

        // Pause for a moment to provide a delay to make 
        // threads more apparent.
        Thread.Sleep(3000);
        Console.WriteLine(
            "The instance method called by the worker thread has ended.");
    }

    public static void StaticMethod()
    {
        Console.WriteLine(
            "ServerClass.StaticMethod is running on another thread.");

        // Pause for a moment to provide a delay to make 
        // threads more apparent.
        Thread.Sleep(5000);
        Console.WriteLine(
            "The static method called by the worker thread has ended.");
    }
}

public class Simple{
    public static int Main(String[] args)
    {
        Console.WriteLine("Thread Simple Sample");

        ServerClass serverObject = new ServerClass();

        // Create the thread object, passing in the 
        // serverObject.InstanceMethod method using a 
        // ThreadStart delegate.
        Thread InstanceCaller = new Thread(
            new ThreadStart(serverObject.InstanceMethod));

        // Start the thread.
        InstanceCaller.Start();

        Console.WriteLine("The Main() thread calls this after " 
            + "starting the new InstanceCaller thread.");

        // Create the thread object, passing in the 
        // serverObject.StaticMethod method using a 
        // ThreadStart delegate.
        Thread StaticCaller = new Thread(
            new ThreadStart(ServerClass.StaticMethod));

        // Start the thread.
        StaticCaller.Start();

        Console.WriteLine("The Main() thread calls this after "
            + "starting the new StaticCaller thread.");

        return 0;
    }
}
1 голос
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...