繁体   English   中英

C#如何终止和启动进程

[英]c# how to terminate and start a process

在我的C#程序(使用Visual Studio 2010)中,我正在将文件上传到SharePoint文档库,问题是我正在运行我们的内存,显然进程“ sqlservr.exe * 32”就像是超过1 GB的内存。 要上传文件,我首先将文件的字节读取到byte []数组中。 然后将数组作为文件上传到文档库。 我需要一种在上载每个文件之前清除内存的方法。

该程序在循环访问目录的循环中上载文件。 那么有没有办法清除byte []数组的内存?

其实有一种方法可以重新启动进程“ sqlservr.exe * 32”以释放内存吗?

这是我用来上传的代码:

我在网站上看到的错误消息是(发生问题时,我认为是内存不足,有时消息中还有其他文件):URL“测试库/myfolder/file.txt”无效。 它可能引用了不存在的文件或文件夹,或者引用了不在当前Web中的有效文件或文件夹。

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.IO;
using Microsoft.SharePoint;

namespace CustomApplicationPage.Layouts.CustomApplicationPage
{
    public partial class CustomApplicationPage : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            // Check if user posted a file
            if (File1.PostedFile == null)
            {
                return;
            }

            try
            {
                // Get the directories in the path
                string[] array1 = Directory.GetDirectories("C:\\myfolder\\");

                // Declare variables
                string[] temp;
                byte[] contents;
                string dir_name;
                string file_name;
                int i, j;
                int chunk_size = 5;
                int chunk_index = 0;
                int start_id = chunk_size * chunk_index;  
                int end_id = 1 + (chunk_size * (chunk_index+1));

                // Free the memory
                GC.Collect();

                // For each directory in the main path
                for (i = 0; i < array1.Length; i++)
                {
                    // Get directory name
                    dir_name = Path.GetFileName(array1[i]);

                    // Skip this file
                    if (dir_name == "viDesktop_files" || i < start_id || i >= end_id)
                    {
                        continue;
                    }

                    // Get the site
                    SPSite site = new Microsoft.SharePoint.SPSite("http://mysite/");

                    // Turn security off
                    Microsoft.SharePoint.Administration.SPWebApplication webApp = site.WebApplication;
                    webApp.FormDigestSettings.Enabled = false;

                    // Get site root
                    SPWeb spWeb = site.RootWeb;

                    // Get the specified list/library from the site root
                    SPList docLib = spWeb.Lists["Test Library"];

                    // Get all files in the directory
                    temp = Directory.GetFiles(array1[i]);

                    // Create a folder in the list/library
                    SPListItem folder = docLib.Folders.Add(docLib.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, dir_name);

                    // Activate the newly created folder
                    folder.Update();

                    // For each file in the directory
                    for (j = 0; j < temp.Length; j++)
                    {
                        // Get file name
                        file_name = Path.GetFileName(temp[j]);

                        // If the file is a .mht file
                        if (file_name.EndsWith(".mht"))
                        {
                            // Read the contents of the uploaded file into the variable 'contents'
                            contents = File.ReadAllBytes(temp[j]);

                            // Add the file with the specified filename in the folder
                            SPFile file = folder.Folder.Files.Add(file_name, contents);

                            // Activate the file
                            file.Update();
                        }
                    }

                    // Turn security on
                    webApp.FormDigestSettings.Enabled = true;

                    // Close the site
                    site.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

释放结果并关闭连接...您可以重新启动过程,但是可能存在更大的问题...尝试使用LINQ和DataContext

http://msdn.microsoft.com/zh-CN/library/system.data.linq.datacontext.aspx

如果您相信所有设置都正确,则尝试使用SQL事件探查器,并查看数据库级别发生了什么...

http://msdn.microsoft.com/en-us/library/ms181091.aspx

尝试在网站,webApp,spWeb,docLib,文件夹,文件以及任何其他一次性使用的对象上调用.Dispose()。

其他应用程序占用的内存极有可能不会导致您的应用程序收到OutOfMemoryException。 Windows在需要时使用虚拟内存,并将其他应用程序的内存放入页面文件中。 在32位过程中,您的应用程序应具有2G的可用内存(即使您没有那么多的可用RAM)。

根据我对OutOfMemory异常的了解,它们通常是由于clr导致的,因为碎片导致clr无法为我的应用程序提供足够大的连续内存,足以满足我的尝试。

无论如何,您应该从处理那些一次性的东西开始。

而且,顺便说一下,您不需要调用GC.Collect();。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM