public KarFile(string fileName, ReportProgressFunction ReportProgress)
        {
            _fileName = fileName;
            _directory = FileFunctions.CreateTempoaryDirectory();
            using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            using (ZipInputStream zip = new ZipInputStream(file))
            {
                while (true)
                {
                    ZipEntry item = zip.GetNextEntry();
                    if (item == null)
                    {
                        break;
                    }

                    string itemDirectory = Path.GetDirectoryName(item.Name);
                    string itemName = Path.GetFileName(item.Name);

                    string outputDirectory = Path.Combine(_directory, itemDirectory);
                    FileFunctions.CreateDirectory(outputDirectory, ReportProgress);

                    string outputName = Path.Combine(outputDirectory, itemName);
                    StreamFunctions.ExportUntilFail(outputName, zip, ReportProgress);
                }

                zip.Close();
            }
        }
Esempio n. 2
0
        public static string DoWork
        (
            [MarshalAs(UnmanagedType.LPStr)] string jobName,
            int iterations,
            int dataSize,
            [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] double[] data,
            ReportProgressFunction reportProgressFunction
        )
        {
            for (int i = 1; i <= iterations; i++)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine($"Beginning work iteration {i}");
                Console.ResetColor();

                // Pause as if doing work
                Thread.Sleep(1000);

                // Call the native callback and write its return value to the console
                var progressResponse = reportProgressFunction(i);
                Console.WriteLine($"Received response [{progressResponse}] from progress function");
            }

            Console.ForegroundColor = ConsoleColor.Green;

            Console.WriteLine($"Work completed");
            Console.ResetColor();

            return($"Data received: {string.Join(", ", data.Select(d => d.ToString()))}");
        }
Esempio n. 3
0
        public static string DoWork(
            [MarshalAs(UnmanagedType.LPStr)] string jobName,
            int iterations,
            ReportProgressFunction reportProgressFunction)
        {
            Console.WriteLine($"Thread ID: {System.Threading.Thread.CurrentThread.ManagedThreadId}");

            for (int i = 1; i <= iterations; i++)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine($"Beginning work iteration {i}");
                Console.ResetColor();

                // Call the native callback and write its return value to the console
                var progressResponse = reportProgressFunction(i);
                Console.WriteLine($"Received response [{progressResponse}] from progress function");
            }

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"Work completed");

            Console.ResetColor();

            return("completed");
        }
Esempio n. 4
0
 public BigImage(string topleft, string bottomright, ReportProgressFunction reportFunction, BackgroundWorker worker)
 {
     this.topleft = topleft;
     this.bottomright = bottomright;
     //gkl changes - changed the delegate variable
     this.outsidereportFunction = reportFunction;
     this.worker = worker;
 }
Esempio n. 5
0
        public static void SetupCallBack(ReportProgressFunction reportProgressFunction)
        {
            Console.WriteLine("Setup native Callback");

            try
            {
                //Add our native call back to a list so we can use it later, because this is really a native pointer we cant just set our managed member to this value, we have to add it to a list and then call it from there
                ReportProgressFunc.Add(reportProgressFunction);

                //Fire a test back to native code
                Console.WriteLine("ReportProgressFunc Return: {0} For Test Fire", FireCallback(4, "test", NativeDataType.STRING));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}", ex.Message);
            }
        }
        public static void CreateDirectory(string directory, ReportProgressFunction ReportMessage)
        {
            try
            {
                if (Directory.Exists(directory))
                {
                    return;
                }

                string baseDirectory = Path.GetDirectoryName(directory);
                CreateDirectory(baseDirectory, ReportMessage);

                _ReportMessage(ReportMessage, -1, "Create directory: " + directory);
                Directory.CreateDirectory(directory);
            } catch (Exception error)
            {
                string errorMessage = "Failed create directory \"" + directory + "\"";
                _ReportMessage(ReportMessage, -1, errorMessage);
                throw new Exception(errorMessage, error);
            }
        }
        public static void CopyFile(string sourceFileName, string destinationFileName, ReportProgressFunction ReportMessage)
        {
            try
            {
                string destinationDirectory = Path.GetDirectoryName(destinationFileName);
                CreateDirectory(destinationDirectory, ReportMessage);
                if (FileExists(destinationFileName))
                {
                    _ReportMessage(ReportMessage, -1, "delete: " + destinationFileName);
                    File.Delete(destinationFileName);
                }

                _ReportMessage(ReportMessage, -1, "copy: " + sourceFileName + " -> " + destinationFileName);
                File.Copy(sourceFileName, destinationFileName);
            } catch (Exception error)
            {
                string errorMessage = "Failed copying file \"" + sourceFileName + "\" -> \"" + destinationFileName + "\"";
                _ReportMessage(ReportMessage, -1, errorMessage);
                throw new Exception(errorMessage, error);
            }
        }
        internal static void Install(bool clean, ReportProgressFunction ReportMessage)
        {
            string fileName = Path.Combine(Application.StartupPath, "Install.txt");

            List<string> _pending = new List<string>(File.ReadLines(fileName));
            List<string> _completed = new List<string>();

            _ReportMessage(ReportMessage, 0, "Installing...");

            for (int symbolIndex = 0; symbolIndex < _symbols.Count; symbolIndex++)
            {
                string symbol = _symbols[symbolIndex].Item1;
                string value = _symbols[symbolIndex].Item2;

                _ReportMessage(ReportMessage, -1, "Symbol: " + symbol + " -> " + value);
            }

            while (_pending.Count > 0)
            {
                string line;
                if (clean)
                {
                    line = _pending[0];
                    _pending.RemoveAt(0);
                } else
                {
                    line = _pending[_pending.Count - 1];
                    _pending.RemoveAt(_pending.Count - 1);
                }
                _completed.Add(line);

                RunLine(line, clean, ReportMessage);

                _ReportMessage(ReportMessage, 100 * (_completed.Count) / (_completed.Count + _pending.Count), string.Empty);
            }
            _ReportMessage(ReportMessage, 100, "Done.");
        }
        public static void ExportUntilFail(string fileName, Stream stream, ReportProgressFunction ReportProgress)
        {
            if (stream is System.Net.Sockets.NetworkStream)
            {
                throw new InvalidOperationException("Cannot use " + typeof(System.Net.Sockets.NetworkStream).FullName);
            }

            if (ReportProgress != null)
            {
                ReportProgress(-1, "Export: " + fileName);
            }

            using (FileStream destination = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                // CopyUntilFail(Stream destination, Stream source)
                {
                    if (stream is MemoryStream && stream.Position == stream.Length)
                    {
                        throw new Exception("waiting on a MemoryStream will block execution indefinatly");
                    }

                    int bufferSize = 8192;
                    byte[] buffer = new byte[bufferSize];
                    while (true)
                    {
                        int countRead = stream.Read(buffer, 0, bufferSize);
                        destination.Write(buffer, 0, countRead);
                        if (countRead != bufferSize)
                        {
                            break;
                        }
                    }
                }

                destination.Close();
            }
        }
        public static void DeleteDirectory(string directory, ReportProgressFunction ReportMessage)
        {
            try
            {
                if (!Directory.Exists(directory))
                {
                    return;
                }
                string[] files = Directory.GetFiles(directory);
                if (files.Length > 0)
                {
                    return;
                }

                _ReportMessage(ReportMessage, -1, "delete directory: " + directory);
                Directory.Delete(directory);
                string baseDirectory = Path.GetDirectoryName(directory);
                DeleteDirectory(baseDirectory, ReportMessage);
            } catch { }
        }
 private static void _ReportMessage(ReportProgressFunction ReportMessage, int progress, string message)
 {
     if (ReportMessage != null)
     {
         ReportMessage(progress, message);
     } else if (progress >= 0)
     {
         Console.WriteLine(progress + "%" + message);
     } else if (ExceptionFunctions.ForceVerbose)
     {
         Console.WriteLine(message);
     }
 }
 public static void DeleteFile(string fileName, ReportProgressFunction ReportMessage)
 {
     try
     {
         if (!FileExists(fileName))
         {
             return;
         }
         File.SetAttributes(fileName, FileAttributes.Normal);
         _ReportMessage(ReportMessage, -1, "delete file: " + fileName);
         File.Delete(fileName);
         string destinationDirectory = Path.GetDirectoryName(fileName);
         DeleteDirectory(destinationDirectory, ReportMessage);
     } catch { }
 }
        internal static void RunLine(string _line, bool clean, ReportProgressFunction ReportMessage)
        {
            _ReportMessage(ReportMessage, -1, _line);

            string line = ResloveSymbols(_line);

            if (line != _line)
            {
                    _ReportMessage(ReportMessage, -1, "Changed to: " + line);
            }

            string whiteSpace;
            StringIO.SkipWhiteSpace(out whiteSpace, ref line);
            if (StringIO.TryRead("CopyFiles", ref line))
            {
                string sourceDirectory;
                StringIO.SkipWhiteSpace(out whiteSpace, ref line);
                if (!StringIO.TryReadString(out sourceDirectory, ref line))
                {
                    throw new Exception("could not read line \"" + _line + "\"");
                }

                string destinationDirectory;
                StringIO.SkipWhiteSpace(out whiteSpace, ref line);
                if (!StringIO.TryReadString(out destinationDirectory, ref line))
                {
                    throw new Exception("could not read line \"" + _line + "\"");
                }

                string[] files = Directory.GetFiles(sourceDirectory, "*");
                for (int fileIndex = 0; fileIndex < files.Length; fileIndex++)
                {
                    string sourceFileName = files[fileIndex];
                    string fileName = Path.GetFileName(sourceFileName);
                    string destinationFileName = Path.Combine(destinationDirectory, fileName);

                    if (!clean)
                    {
                        FileFunctions.CopyFile(sourceFileName, destinationFileName, ReportMessage);
                    } else
                    {
                        FileFunctions.DeleteFile(destinationFileName, ReportMessage);
                    }
                }
            } else if (StringIO.TryRead("Copy", ref line))
            {
                StringIO.SkipWhiteSpace(out whiteSpace, ref line);

                string sourceFileName;
                if (!StringIO.TryReadString(out sourceFileName, ref line))
                {
                    throw new Exception("could not read line \"" + _line + "\"");
                }

                string destinationFileName;
                StringIO.SkipWhiteSpace(out whiteSpace, ref line);
                if (!StringIO.TryReadString(out destinationFileName, ref line))
                {
                    throw new Exception("could not read line \"" + _line + "\"");
                }

                if (!clean)
                {
                    FileFunctions.CopyFile(sourceFileName, destinationFileName, ReportMessage);
                } else
                {
                    FileFunctions.DeleteFile(destinationFileName, ReportMessage);
                }
            } else if (string.IsNullOrWhiteSpace(line) || StringIO.TryRead("//", ref line))
            {
                // do nothing
            } else
            {
                throw new Exception("could not read line \"" + _line + "\"");
            }
        }
Esempio n. 14
0
 public TileDownloader(ReportProgressFunction reportFunction, BackgroundWorker worker)
 {
     this.outsidereportFunction = reportFunction;
     this.worker = worker;
 }