c# - Proper way of passing a pointer for P/Invoke function -


Dear, I'm developing an entity that allows a user to copy multiple files in async way with the ability to cancel (And also reporting progress) Obviously the process of copying runs in the second thread, where the thread is separated from where the CopySync was called.

My first implementation filestream. With Bing Reading Read / RWright buffer and reporting progress against the number of buffers used.

Later, for education purposes, I was trying to apply the same content through the Win32 CopyFileEx function. After all, I have stumbled on the following things: This function takes the pulse value an indicator which is considered as the indicator to cancel. According to MSDN, copying this value should be checked several times by Win32 during the operation. When the user cancels the process of copying this value to "false" then it is canceled.

The real problem for me is how can a Boolean value be made, pass it on Win32 and the ability to cancel this copy operation to make this value accessible to the external user is obvious that the user Call to CancelAsync (Object Task Ed), so my question is how to get access to that boolean value in the second thread from my canceled ASCNC implementation.

My first attempt was to identify the async operation and value points for the allocation for the Boolean value memory slot, where when the user calls the "Cancel Sink (Object TaskId)" method, then my Receives square pointer from the dictionary in that allotted memory and writes "1" there.

Tomorrow I have developed another solution that bool local variables in my method of coping and holding the address of that value in a dictionary until a copy operation is complete. This approach can be described in the following lines of code (very simple and obsolete for clarifying only one thought):

  class program {// dictionary operaitons identifiers public dictionary & lt; String, IntPtr & gt; Dict = new dictionary & lt; String, intupetate & gt; (); Static zero main (string [] arg) {program p = new program (); P.StartTheThread (); // start copying action, in my // implementation it will be a thread pool thread) manual resetset i public zero startTheThread () {thread t = new thread (thread task); M = new manual retset (incorrect); T.Start (zero); GC.Collect (); // just to make sure that this kind of solution works :) GC. Compiler (); Mre.WaitOne (); Unsafe // Copy operation {IntPtr ptr = dict ["a"]; Bool * boolPtr = (bool *) PTR; // Getting in the local variable in a reference / second thread (* boolPtr) = false; }} Public Zero Thread Task (Object State) {// Win32 Call Bull will be var = true for CopyFileX in this thread; Unsafe {word ["one"] = (intake) (& amp; var); // fill out a dictionary // mre.Set (with the cancellation identifier); // In fact Win32 CopyFileEx calls will be here (true) {Console.WriteLine ("Dict: {0}", Dixit ["A"]); Console.light line ("var: {0}", var); Console.WriteLine ("============"); Thread.Sleep (1000); In fact, I'm a bit new to P / Ivec and all the unsafe goods so that there is a hesitation in the context of the local value and later in this context, and we can highlight this value in other threads. 

Any other ideas about how to highlight that indicator in boolean to support the copying operation? Ah, so this was the second thread that was almost a better way to complete it,

CopyFileEx () also supports a progress callback. This callback lets you update the UI to show progress. And this allows you to cancel the copy, just return PROGRESS_CANCEL from the callback.

You need to call the Callback Representative Declaration to go to Pinwok.net.


Comments