internal Worker(ThreadPriority priority) { m_Id = workerId++; m_Thread = new Thread(Run); m_Thread.Priority = priority; }
//--------------------------------------- 2 START SINGLE THREAD OVERLOADS -------------------------------------- //--------------------------------------- 2 START SINGLE THREAD OVERLOADS -------------------------------------- #region 2 START SINGLE THREAD OVERLOADS /// <summary> /// Starts an method running on a new thread. The Thread dies when the method has stopped running. /// You can now make use of the DispatchToMainThread-actions & WaitForNextFrame /// </summary> /// <param name="targetMethod">The method that will be executed by the new thread</param> /// <param name="priority">Thread Priority</param> /// <param name="safeMode">Default TRUE: Executes the targetMethod within a Try-Catch statement and will log any errors back to the MainThread</param> /// <returns>Newly instantiated Thread</returns> public static Thread StartSingleThread(ThreadStart targetMethod, System.Threading.ThreadPriority priority = System.Threading.ThreadPriority.Normal, bool safeMode = true) { return(SingleThreadStarter.StartSingleThread(targetMethod, priority, safeMode)); }
public UDPReceive(int port, System.Threading.ThreadPriority threadPriority) { m_port = port; m_threadPriority = threadPriority; init(); }
public QueueThread(Action<Action> handler, System.Threading.ThreadPriority threadPriority) { if (handler == null) { throw new ArgumentNullException("handler"); } this.handler = handler; this.threadPriority = threadPriority; CreateThread(threadPriority); }
public virtual void Start(System.Threading.ThreadPriority priority = ThreadPriority.Normal) { m_Thread = new System.Threading.Thread(Run); m_Thread.Priority = priority; m_Thread.Start(); }
internal Worker(ThreadPriority priority) { m_Id = workerId++; m_Thread = new Thread(Run); m_Thread.Priority = priority; }
public static Info StartThread(ThreadFunc nThreadStart, System.Threading.ThreadPriority nThreadPriority) { return(StartThread(nThreadStart.Method.Name, nThreadStart, nThreadPriority)); }
const int TimeOutWait = 5 * 1000; //超时等待时间 /// <summary> /// 下载方法(断点续传) /// </summary> /// <param name="url">URL下载地址</param> /// <param name="savePath">Save path保存路径</param> /// <param name="callBack">Call back回调函数</param> public void DownLoad(string url, string savePath, string fileName, long totalLength, Action callBack, System.Threading.ThreadPriority threadPriority = System.Threading.ThreadPriority.Normal) { Logger.Log("loadurl:" + url + " fileName:" + fileName + " savePath:" + savePath + " totalLength:" + totalLength); isStop = false; System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch(); //开启子线程下载,使用匿名方法 thread = new Thread(delegate() { stopWatch.Start(); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } string filePath = savePath + fileName; if (System.IO.File.Exists(filePath)) { System.IO.File.Create(filePath).Dispose(); } //使用流操作文件 FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write); //获取文件现在的长度 long fileLength = fs.Length; //如果没下载完 if (fileLength < totalLength) { //断点续传核心,设置本地文件流的起始位置 fs.Seek(fileLength, SeekOrigin.Begin); HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest; request.ReadWriteTimeout = ReadWriteTimeOut; request.Timeout = TimeOutWait; //断点续传核心,设置远程访问文件流的起始位置 request.AddRange((int)fileLength); Stream stream = request.GetResponse().GetResponseStream(); byte[] buffer = new byte[2048]; //使用流读取内容到buffer中 //注意方法返回值代表读取的实际长度,并不是buffer有多大,stream就会读进去多少 int length = stream.Read(buffer, 0, buffer.Length); while (length > 0) { //如果Unity客户端关闭,停止下载 if (isStop) { break; } //将内容再写入本地文件中 fs.Write(buffer, 0, length); //计算进度 fileLength += length; progress = (float)fileLength / (float)totalLength; //类似尾递归 length = stream.Read(buffer, 0, buffer.Length); //Debug.LogFormat("<color=red>文件:{0} 已下载{1}M,剩余{2}M</color>", fileName, fileLength / 1024 / 1024, (totalLength - fileLength) / 1024 / 1024); } stream.Close(); stream.Dispose(); } else { progress = 1; } stopWatch.Stop(); Debug.Log(fileName + "耗时: " + stopWatch.ElapsedMilliseconds); fs.Close(); fs.Dispose(); //如果下载完毕,执行回调 if (progress >= 1) { isDone = true; UnityEngine.Debug.Log(fileName + " download finished"); if (callBack != null) { callBack(); } thread.Abort(); } }); //开启子线程 thread.IsBackground = true; thread.Priority = threadPriority; thread.Start(); }
private const int m_nTimeOutWait = 5 * 1000; //超时等待时间 public void DownloadFile(string url, string savePath, Action callback, System.Threading.ThreadPriority threadPriority = System.Threading.ThreadPriority.Normal) { C_DebugHelper.Log("C_HttpDownloader DownloadFile url = " + url); System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch(); Progress = 0; DownloadFileLength = 0; m_bIsStop = false; m_Thread = new Thread(delegate() { stopWatch.Start(); //判断保存路径是否存在 if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } string filePath = C_DownloadMgr.StandardDownloadSavePath(url, savePath); //string fileName = C_DownloadMgr.StandardDownloadName(url); //使用流操作文件 FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write); //获取文件现在的长度 DownloadFileLength = fs.Length; //获取下载文件的总长度 long totalLength = C_DownloadMgr.GetLength(url); //Debug.LogFormat("<color=red>文件:{0} 已下载{1}M,剩余{2}M</color>", fileName, fileLength / 1024 / 1024, (totalLength - fileLength) / 1024 / 1024); //如果没下载完 if (DownloadFileLength < totalLength) { //断点续传核心,设置本地文件流的起始位置 fs.Seek(DownloadFileLength, SeekOrigin.Begin); HttpWebRequest request = C_DownloadMgr.GetWebRequest(url); request.ReadWriteTimeout = m_nReadWriteTimeOut; request.Timeout = m_nTimeOutWait; //断点续传核心,设置远程访问文件流的起始位置 request.AddRange((int)DownloadFileLength); Stream stream = request.GetResponse().GetResponseStream(); byte[] buffer = new byte[4096]; //使用流读取内容到buffer中 //注意方法返回值代表读取的实际长度,并不是buffer有多大,stream就会读进去多少 int length = stream.Read(buffer, 0, buffer.Length); while (length > 0) { //如果Unity客户端关闭,停止下载 if (m_bIsStop) { break; } //将内容再写入本地文件中 fs.Write(buffer, 0, length); //计算进度 DownloadFileLength += length; Progress = (float)DownloadFileLength / (float)totalLength; //类似尾递归 length = stream.Read(buffer, 0, buffer.Length); } stream.Close(); stream.Dispose(); } else { Progress = 1; } fs.Close(); stopWatch.Stop(); C_DebugHelper.Log("下载完成,耗时: " + stopWatch.ElapsedMilliseconds); //如果下载完毕,执行回调 if (Progress == 1) { if (callback != null) { callback(); } m_Thread.Abort(); } }); //开启子线程 m_Thread.IsBackground = true; m_Thread.Priority = threadPriority; m_Thread.Start(); }
public void DownLoad(string [] urls, string [] savePaths, string [] fileNames, Action callBack, System.Threading.ThreadPriority threadPriority = System.Threading.ThreadPriority.Normal) { thread = new Thread(delegate() { for (int i = 0; i < urls.Length; i++) { DownLoadThreadFunc(savePaths[i], fileNames[i], urls[i], callBack); } thread.Abort(); }); thread.IsBackground = true; thread.Priority = threadPriority; thread.Start(); }
/// <summary> /// Starts an method running on a new thread. The Thread dies when the method has stopped running. /// You can now make use of the DispatchToMainThread-actions & WaitForNextFrame /// </summary> /// <param name="targetMethod">The method that will be executed by the thread</param> /// <param name="argument">Object to pass to the targetMethod as soon as the Thread is started</param> /// <param name="priority">Thread priority</param> /// <returns>Newly instantiated Thread</returns> public static Thread StartSingleThread(ParameterizedThreadStart targetMethod, object argument, System.Threading.ThreadPriority priority = System.Threading.ThreadPriority.Normal, bool safeMode = true) { Init(); MainThreadWatchdog.Init(); MainThreadDispatcher.Init(); UnityActivityWatchdog.Init(); Thread result = null; if (safeMode) { SafeSingleThreadSession sessionData = new SafeSingleThreadSession(targetMethod); result = new Thread(sessionData.SafeExecte_ParamThreadStart); } else { result = new Thread(targetMethod); } result.Priority = priority; startedThreads.Add(result); result.Start(argument); return(result); }
/// <summary> /// Create a queue and a service thread running in the specified priority /// </summary> /// <param name="name"> /// A <see cref="System.String"/> /// Name of the resource /// </param> /// <param name="size"> /// A <see cref="System.Int32"/> /// size of the job queue /// </param> /// <param name="priority"> /// A <see cref="System.Threading.ThreadPriority"/> /// priority of the thread /// </param> public JobQueue(string name, int size, System.Threading.ThreadPriority priority) : base(name, size) { base.Priority = priority; }
IEnumerator saveT(CamVideo cv, int w, int h) { Queue <byte[]> frameQueue = cv.frameQueue; int savingFrameNumber = cv.savingFrameNumber; while (frameQueue.Count > 0) { // Generate file path string path = persis + "/frame" + savingFrameNumber + ".raw"; File.WriteAllBytes(path, frameQueue.Dequeue()); savingFrameNumber++; } Destroy(cv); List <Frame> frames = new List <Frame>(); Texture2D temp = new Texture2D(w, h, TextureFormat.RGB24, false); temp.hideFlags = HideFlags.HideAndDontSave; temp.wrapMode = TextureWrapMode.Clamp; temp.filterMode = FilterMode.Bilinear; temp.anisoLevel = 0; for (int i = 0; i < savingFrameNumber; i++) { string BMPpath = persis + "/RECORDING_LPM" + "/frame" + i + ".raw"; if (File.Exists(BMPpath)) { temp.LoadRawTextureData(File.ReadAllBytes(BMPpath)); Color32[] colors = temp.GetPixels32(); yield return(null); Frame frame = new Frame() { Width = w, Height = h, Data = colors }; frames.Add(frame); } else { break; } } Flush(temp); yield return(null); l.updateThis(() => { Directory.Delete(persis + "/RECORDING_LPM", true); SeeEverything(); check.interactable = true; view.transform.GetChild(0).gameObject.SetActive(true); Application.targetFrameRate = 24; }); ProGifEncoder encoder = new ProGifEncoder(0, 5); encoder.SetDelay(70); StartCoroutine(SaveVidFile()); ThreadPriority WorkerPriority = ThreadPriority.BelowNormal; // GetSavePath(string saveDir, string album, string filenameFormatted) string name_ = new FilePathName().GetGifFileName(); ProGifWorker worker = new ProGifWorker(WorkerPriority) { m_Encoder = encoder, m_Frames = frames, persisT = persis, m_FilePath = Application.persistentDataPath + "/" + name_ + ".gif", m_OnFileSaved = Saved, m_OnFileSaveProgress = l.setProgress, }; worker.Start(); // _thread.Start(); }
/// <summary> /// Starts an method running on a new thread. The Thread dies when the method has stopped running. /// You can now make use of the DispatchToMainThread-actions & WaitForNextFrame /// </summary> /// <param name="targetMethod">The method that will be executed by the new thread</param> /// <param name="argument">Object to pass to the targetMethod as soon as the Thread is started</param> /// <param name="priority">Thread Priority</param> /// <param name="safeMode">Default TRUE: Executes the targetMethod within a Try-Catch statement and will log any errors back to the MainThread</param> /// <returns>Newly instantiated Thread</returns> public static Thread StartSingleThread(ParameterizedThreadStart targetMethod, object argument, System.Threading.ThreadPriority priority = System.Threading.ThreadPriority.Normal, bool safeMode = true) { return(SingleThreadStarter.StartSingleThread(targetMethod, argument, priority, safeMode)); }
const int TimeOutWait = 5 * 1000; //超时等待时间 /// <summary> /// 下载方法(断点续传) /// </summary> /// <param name="url">URL下载地址</param> /// <param name="savePath">Save path保存路径</param> /// <param name="callBack">Call back回调函数</param> public void DownLoad(string url, string savePath, string fileName, Action callBack, System.Threading.ThreadPriority threadPriority = System.Threading.ThreadPriority.Normal) { isStop = false; System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch(); speed = 0; //开启子线程下载,使用匿名方法 thread = new Thread(delegate() { lastRecordTime = DateTime.Now; stopWatch.Start(); //判断保存路径是否存在 if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } //这是要下载的文件名,比如从服务器下载a.zip到D盘,保存的文件名是test string filePath = savePath + "/" + fileName; //使用流操作文件 FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write); //获取文件现在的长度 long fileLength = fs.Length; //获取下载文件的总长度 Log.Info(url + " " + fileName); long totalLength = GetLength(url); Log.Info("<color=red>文件:{0} 已下载{1}M,剩余{2}M</color>", fileName, fileLength / 1024 / 1024, (totalLength - fileLength) / 1024 / 1024); //如果没下载完 if (fileLength < totalLength) { //断点续传核心,设置本地文件流的起始位置 fs.Seek(fileLength, SeekOrigin.Begin); HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.ReadWriteTimeout = ReadWriteTimeOut; request.Timeout = TimeOutWait; //断点续传核心,设置远程访问文件流的起始位置 request.AddRange((int)fileLength); Stream stream = request.GetResponse().GetResponseStream(); byte[] buffer = new byte[1024]; //使用流读取内容到buffer中 //注意方法返回值代表读取的实际长度,并不是buffer有多大,stream就会读进去多少 int length = stream.Read(buffer, 0, buffer.Length); //Debug.LogFormat("<color=red>length:{0}</color>" + length); while (length > 0) { //如果Unity客户端关闭,停止下载 if (isStop) { break; } //将内容再写入本地文件中 fs.Write(buffer, 0, length); //计算进度 fileLength += length; progress = fileLength / (float)totalLength; //UnityEngine.Debug.Log(progress); //类似尾递归 length = stream.Read(buffer, 0, buffer.Length); if ((DateTime.Now - lastRecordTime).TotalMilliseconds >= 500) { speed = (uint)((fileLength - lastLength)); lastRecordTime = DateTime.Now; lastLength = fileLength; lastRecordTime = DateTime.Now; } Log.Warning("Speed" + speed); Log.Info("Progress : " + progress); } stream.Close(); stream.Dispose(); } else { progress = 1; } stopWatch.Stop(); Debug.Log("耗时: " + stopWatch.ElapsedMilliseconds / 1000.0f); fs.Close(); fs.Dispose(); //如果下载完毕,执行回调 if (progress == 1) { isDone = true; if (callBack != null) { callBack(); } thread.Abort(); } Log.Info("download finished"); }); //开启子线程 thread.IsBackground = true; thread.Priority = threadPriority; thread.Start(); }
public void SetPriorityForThread(int threadID, System.Threading.ThreadPriority p) { iSCDThreadContainer container = ContainerForID(threadID); container.SetPriority(p); }
const int TimeOutWait = 5 * 1000; //超时等待时间 /// <summary> /// 单文件下载方法(支持断点续传) /// </summary> /// <param name="url"></param> /// <param name="savePath"></param> /// <param name="fileName"></param> /// <param name="isNewFile">true不断点续传</param> /// <param name="callBack"></param> /// <param name="threadPriority"></param> public void DownLoad(string url, string savePath, string fileName, bool isNewFile, Action callBack, System.Threading.ThreadPriority threadPriority = System.Threading.ThreadPriority.Normal) { isStop = false; System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch(); //开启子线程下载,使用匿名方法 thread = new Thread(delegate() { stopWatch.Start(); //判断保存路径是否存在 if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } //获取下载文件的总长度 UnityEngine.Debug.Log(url + " " + fileName); long totalLength = GetLength(url); //获取文件现在的长度 string filePath = savePath + "/" + fileName; FileStream fs = null; if (isNewFile) { //覆盖下载 fs = new FileStream(filePath, FileMode.Create, FileAccess.Write); } else { //断点续传 fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write); } long fileLength = fs.Length; Logger.LogWarp.LogFormat("文件:{0} 已下载{1},剩余{2}", fileName, fileLength, (totalLength - fileLength)); //如果没下载完 if (fileLength < totalLength) { //断点续传核心,设置本地文件流的起始位置 fs.Seek(fileLength, SeekOrigin.Begin); HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest; request.ReadWriteTimeout = ReadWriteTimeOut; request.Timeout = TimeOutWait; //断点续传核心,设置远程访问文件流的起始位置 request.AddRange((int)fileLength); Stream stream = null; try { stream = request.GetResponse().GetResponseStream(); } catch (Exception ex) { Logger.LogWarp.LogError(ex.ToString()); } byte[] buffer = new byte[1024]; //使用流读取内容到buffer中 //注意方法返回值代表读取的实际长度,并不是buffer有多大,stream就会读进去多少 int length = stream.Read(buffer, 0, buffer.Length); while (length > 0) { //如果Unity客户端关闭,停止下载 if (isStop) { break; } //将内容再写入本地文件中 fs.Write(buffer, 0, length); //计算进度 fileLength += length; progress = (float)fileLength / (float)totalLength; //类似尾递归 length = stream.Read(buffer, 0, buffer.Length); } stream.Close(); stream.Dispose(); } else { progress = 1; } stopWatch.Stop(); Logger.LogWarp.Log("耗时: " + stopWatch.ElapsedMilliseconds); fs.Close(); fs.Dispose(); //如果下载完毕,执行回调 if (progress == 1) { isDone = true; if (callBack != null) { callBack(); } Logger.LogWarp.Log(url + " download finished"); thread.Abort(); } }); //开启子线程 thread.IsBackground = true; thread.Priority = threadPriority; thread.Start(); }