SoFunction
Updated on 2025-03-07

C# implements the method of calling Thunder download

Thunder Download is a very common download software currently used. This article shows how C# implements the method of calling Thunder Download. The specific methods are as follows:

Currently, this instance code only supports HTTP protocol, and the specific function code is as follows:

using System;
using ;
using ;
using ;
using ;
using ;

namespace ThunderSDK
{
 class Program
 {
 enum enumTaskStatus
 {
  enumTaskStatus_Connect = 0,         // Connection has been established  enumTaskStatus_Download = 2,        // Start downloading  enumTaskStatus_Pause = 10,         // pause  enumTaskStatus_Success = 11,        // Download successfully  enumTaskStatus_Fail = 12,          // Download failed };
 public const int XL_SUCCESS = 0;
 public const int XL_ERROR_FAIL = 0x10000000;

 // Not initialized yet public const int XL_ERROR_UNINITAILIZE = XL_ERROR_FAIL + 1;

 // Not supported protocol, currently only supports HTTP public const int XL_ERROR_UNSPORTED_PROTOCOL = XL_ERROR_FAIL + 2;

 // Initialization of the tray icon failed public const int XL_ERROR_INIT_TASK_TRAY_ICON_FAIL = XL_ERROR_FAIL + 3;

 // Adding the tray icon failed public const int XL_ERROR_ADD_TASK_TRAY_ICON_FAIL = XL_ERROR_FAIL + 4;

 // The pointer is empty public const int XL_ERROR_POINTER_IS_NULL = XL_ERROR_FAIL + 5;

 // The string is an empty string public const int XL_ERROR_STRING_IS_EMPTY = XL_ERROR_FAIL + 6;

 // The incoming path does not contain the file name public const int XL_ERROR_PATH_DONT_INCLUDE_FILENAME = XL_ERROR_FAIL + 7;

 // Failed to create a directory public const int XL_ERROR_CREATE_DIRECTORY_FAIL = XL_ERROR_FAIL + 8;

 // Insufficient memory public const int XL_ERROR_MEMORY_ISNT_ENOUGH = XL_ERROR_FAIL + 9;

 // Incorrect parameters public const int XL_ERROR_INVALID_ARG = XL_ERROR_FAIL + 10;

 // The task does not exist public const int XL_ERROR_TASK_DONT_EXIST = XL_ERROR_FAIL + 11;

 // The file name is illegal public const int XL_ERROR_FILE_NAME_INVALID = XL_ERROR_FAIL + 12;

 // Not implemented public const int XL_ERROR_NOTIMPL = XL_ERROR_FAIL + 13;

 // The number of tasks that have been created reaches the maximum number of tasks, and the task cannot be created. public const int XL_ERROR_TASKNUM_EXCEED_MAXNUM = XL_ERROR_FAIL + 14;

 // The task type is unknown public const int XL_ERROR_INVALID_TASK_TYPE = XL_ERROR_FAIL + 15;

 // The file already exists public const int XL_ERROR_FILE_ALREADY_EXIST = XL_ERROR_FAIL + 16;

 // The file does not exist public const int XL_ERROR_FILE_DONT_EXIST = XL_ERROR_FAIL + 17;

 // Reading cfg file failed public const int XL_ERROR_READ_CFG_FILE_FAIL = XL_ERROR_FAIL + 18;

 // Failed to write to cfg file public const int XL_ERROR_WRITE_CFG_FILE_FAIL = XL_ERROR_FAIL + 19;

 // The task cannot be continued, it may be because the breakpoint relay is not supported, or it may be because the task has failed //Check the task status to determine the cause of the error. public const int XL_ERROR_CANNOT_CONTINUE_TASK = XL_ERROR_FAIL + 20;

 // The task cannot be paused, it may be because the breakpoint relay is not supported, or it may be because the task has failed //Check the task status to determine the cause of the error. public const int XL_ERROR_CANNOT_PAUSE_TASK = XL_ERROR_FAIL + 21;

 // The buffer is too small public const int XL_ERROR_BUFFER_TOO_SMALL = XL_ERROR_FAIL + 22;

 // The thread calling XLInitDownloadEngine has ended before calling XLUninitDownloadEngine. // Initialize the download engine thread, and must remain executed before calling XLUninitDownloadEngine. public const int XL_ERROR_INIT_THREAD_EXIT_TOO_EARLY = XL_ERROR_FAIL + 23;

 [DllImport("", EntryPoint = "XLInitDownloadEngine")]
 public static extern bool XLInitDownloadEngine();
 [DllImport("", EntryPoint = "XLURLDownloadToFile", CharSet = )]
 public static extern int XLURLDownloadToFile(string pszFileName, string pszUrl, string pszRefUrl, ref Int32 lTaskId);
 [DllImport("")]
 public static extern int XLQueryTaskInfo(int lTaskId, ref int plStatus, ref double pullFileSize, ref double pullRecvSize);
 [DllImport("")]
 public static extern int XLPauseTask(int lTaskId, ref int lNewTaskId);
 [DllImport("")]
 public static extern int XLContinueTask(int lTaskId);
 [DllImport("")]
 public static extern int XLContinueTaskFromTdFile(string pszTdFileFullPath, ref int lTaskId);
 [DllImport("")]
 public static extern void XLStopTask(int lTaskId);
 [DllImport("")]
 public static extern bool XLUninitDownloadEngine();
 [DllImport("")]
 public static extern int XLGetErrorMsg(int dwErrorId, string pszBuffer, ref int dwSize);

 static void Main(string[] args)
 {
  if (!XLInitDownloadEngine())
  {
  ("Download engine initialization error");
  return;
  }

  Int32 lTaskId = 0;
  string filename = "d://";
  string url = "/kankan/XMPSetup_3.8.1.";
  string refurl = "";
  int dwRet = XLURLDownloadToFile(filename, url, refurl, ref lTaskId);
  if (XL_SUCCESS != dwRet)
  {
  XLUninitDownloadEngine();
  ("Add a new task failed");
  return;
  }
  ("Start Download");
  do
  {
  (1000);
  double pullFileSize = 0;
  double pullRecvSize = 0;
  int lStatus = -1;
  dwRet = XLQueryTaskInfo(lTaskId, ref lStatus, ref pullFileSize, ref pullRecvSize);
  if (XL_SUCCESS == dwRet)
  {
   if ((int)enumTaskStatus.enumTaskStatus_Success == lStatus)
   {
   ("Download Completed");
   break;
   }
   if (0 != pullFileSize)
   {
   double douProcess = (double)pullRecvSize / (double)pullFileSize;
   douProcess *= 100.0;
   ("Download progress:{0}%", douProcess);
   }
   else
   {
   ("File length is 0");
   }

  }
  } while (XL_SUCCESS == dwRet);
  XLStopTask(lTaskId);
  XLUninitDownloadEngine();
 }
 }
}

I hope that the examples in this article can help you learn C# programming.