开发者

Createhardlink in managed code

I'm developing an application that creates hundreds of thousands of hardlinks (this is the core feature of the application).

I use the parallel programming features that are now available in dotNET 4.0. This works really well. See example snippits below.

Either:

Parallel.For(from, until, delegate(int i)
{
    j += 1;
    fileIndex = Convert.ToInt32(Math.Round(j * 0.001) + 1);

    //determine the hardlink files; we have to have an unique name for the hardlink for each individual hardlink
    fileName = fiArray[fileIndex].Name; //Path.GetFileNameWithoutExtension(textBoxFile.Text);
    destinationFileName = Path.Combine(textBoxDestination.Text, string.Concat(fileName, "_", i.ToString(), ".txt"));

    fr.CreateHardLink(destinationFileName, fiArray[fileIndex].FullName);
});

Or:

//loop that does the actual work
for (int i = 0; i < nudThreads.Value; i++)
{
    //determine the work package per task
    from = 0 + until + 1;
    until = (i * (Convert.ToInt32(HardLinks / ThreadNo))) + 1;
    var compute = Task.Factory.StartNew(() =>
    {
        token.ThrowIfCancellationRequested(); //uit boek
        return Work(from, until, false);//todo: counter moet nog worden meegenomen
    }, tokenSource.Token);

    tasks.Add(compute);

    var displayResults = compute.ContinueWith(resultTask => UpdateControls(),
                开发者_如何学C             CancellationToken.None,
                             TaskContinuationOptions.OnlyOnRanToCompletion,
                             ui);
    CheckedListBoxFiles.Items.Add(DateTime.Now.ToString() + " : Created the hardlinks for: " + displayResults + " files.");
    Application.DoEvents();

    var displayCancelledTasks = compute.ContinueWith(resultTask => UpdateControls(),
                                CancellationToken.None,
                                   TaskContinuationOptions.OnlyOnCanceled, ui);
    CheckedListBoxFiles.Items.Add(DateTime.Now.ToString() + " : Cancelled a task at: " + displayCancelledTasks + " files.");
    Application.DoEvents();
}

The question i have is this: CreateHardlink is part of Kernel32.dll and hence runs in UNMANAGED code. What i know about the parallel ctp is that parallel tasks have to run in managed code. Is there a managed alternative for createhardlink? Does anyone know how to create a hardlink in managed code and does anyone have any thougths about the parallel programming and using unmanaged code?


There's little point in trying to create hard links in a parallel fashion. This is not a CPU bound operation, it is I/O bound. I would not expect any performance benefit from this approach when compared against the naive serial approach.

Your question about managed and unmanaged code relating to hardlink creation is interesting. You must remember that any I/O access from managed code will at some point call into unmanaged code. The OS is not managed and the only way to create a hardlink is to go through the OS. I think you need to be more precise about exactly what this restriction of the CTP to managed code really means.


If you want to be able to cancel hardlink creation, but don't want multithreading, what I would do is implement a producer/consumer queue - one or more threads add "create a hard link at this path" work items to the queue, and a single thread grabs work items from the queue and creates hard links for them.

This gives you some flexibility over cancelling - you can abort the worker thread if you want to stop all hardlink creation, or if you want to cancel just one item you can find it in the queue and remove it.

Side note from looking at the code you posted - calling Application.DoEvents() is pure, unmitigated evil. Every time you call it, a baby seal clubs itself to death. A much better way to do it is for your UI to queue a work item on the thread pool, then use Dispatcher.Invoke (assuming this is WPF - if it's Winforms I know there's an equivalent but I don't know what it is) to update the UI asynchronously.


Using unmanaged OS calls in parallel is no more a problem than doing it from managed code. Simple as that. :)

Managed code simply means it runs within a managed context (so the CLR can keep track of things). Execution wise it doesn't matter if it is managed or unmanaged.

If I am not remembering it wrong any kernel32-call is atomic, meaning it is thread safe.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜