小编Wow*_*owa的帖子

C#有foreach oneliner吗?

我只是想知道C#中是否有foreach oneliner,就像if oneliner一样.(exp) ? then : else

c# syntax foreach

15
推荐指数
4
解决办法
2万
查看次数

C#:如何通过WebRequest发布大字符串?

我如何使用POST上传一个大字符串(在我的情况下使用BLOB的XML)而不使用GetResponse获取Timeout?

更改超时有帮助,但这不是一个真正的解决方案.如果服务器确实死亡或POST中断,我必须等待极端大超时.

任何的想法?

HttpWebRequest webRequest = null;
string response = "";
byte[] bytes = Encoding.UTF8.GetBytes(xml);

try
{
    webRequest = (HttpWebRequest)WebRequest.Create("http://" + this.host + ":" + this.port);
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.Method = "POST";
    webRequest.Timeout = 5000;

    webRequest.ContentLength = bytes.Length;
    using (Stream requeststream = webRequest.GetRequestStream())
    {
        requeststream.Write(bytes, 0, bytes.Length);
        requeststream.Close();
    }

    using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
    {
        using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
        {
            response = sr.ReadToEnd().Trim();
            sr.Close();
        }
        webResponse.Close();
    }
}
catch(Exception ex)
{
    MessageBox.Show(ex.ToString());
}
return response;
Run Code Online (Sandbox Code Playgroud)

c# timeout http-post httpwebrequest

9
推荐指数
1
解决办法
7339
查看次数

WebRequest GetResponseStream读取字节

我正在尝试从ResponeStream读取字节,但我怎么能说等待数据呢?

如果我在GetResponseStream之后设置一个断点并等待几秒钟,一切正常.使用StreamReader.ReadToEnd()也可以正常工作,但我想自己读取字节.

byte[] response = null;

int left = 0;
int steps = 0;
int pos = 0;

int bytelength = 1024;

OnReceiveStart();

using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) {
    using (Stream sr = webResponse.GetResponseStream()) {

        response = new byte[(int)webResponse.ContentLength];

        left = (int)webResponse.ContentLength % bytelength;
        steps = (int)webResponse.ContentLength / bytelength;
        pos = 0;

        for (int i = 0; i < steps; i++) {
            sr.Read(response, pos, bytelength);
            pos += bytelength;
            OnReceiveProgress((int)webResponse.ContentLength, pos);
        }

        if (left != 0) {
            sr.Read(response, pos, left);
        } …
Run Code Online (Sandbox Code Playgroud)

c# webrequest stream httpwebrequest

6
推荐指数
1
解决办法
7313
查看次数

如何释放Outlook MailItem正确?

我只是无法释放我的Outlook MailItems.打开200 Mails后,Exchange Sever返回达到的最大打开电子邮件数.

我将从所有选定的邮件中删除我的UserProperty.

我的代码:


foreach (var selection in Globals.ThisAddIn.Application.ActiveExplorer().Selection)
{
 if (selection is MailItem)
 {
  MailItem mi = (MailItem)selection;
  UserProperty up = mi.UserProperties.Find("MyProp");
  if (up != null)
  {
   up.Delete();
   //##################################
   // I also tried :
   //----------------------------------
   //    Marshal.ReleaseComObject(up);
   //    up = null;
   //----------------------------------
  }

  mi.Save();

  //##################################
  // I also tried :
  //----------------------------------
  //     mi.Close(OlInspectorClose.olDiscard);
  //----------------------------------


  // I don't know if this loop is necessary, but I have found it somewhere on the web
  while (Marshal.ReleaseComObject(mi) > 0);
  mi = …
Run Code Online (Sandbox Code Playgroud)

c# com outlook mailitem

5
推荐指数
1
解决办法
8041
查看次数

.Net应用极端缓慢.杀死svchost.exe后,它会再次快速运行.任何的想法?

我在Windows7 x64上运行了一个C#.Net 3.5应用程序(x86).

几天或几周之后,无论出于何种原因,应用程序都运行速度极慢.

所有其他应用程序运行正常(另一个C#.Net 3.5应用程序(x86)).

杀死一些svchost.exe后,应用程序再次运行正常.重新启动而不杀死svchost.exe也无济于事.Windows可能会保存svchost.exe的状态.如果我注销用户并再次登录,它也会再次运行,而不会杀死svchost.在注销时,svchost可能已被清除.

有人知道如何解决这个问题吗?

迎接Wowa

编辑:

Main-Form只是一个MDI-Container,左侧是Treeview,带有静态节点.

应用程序检查通过网络路径启动更新(检查文件creationdate),但这不是问题,因为所有其他PC工作没有问题.

更新后,每个MDI子表单开始需要1-2分钟,甚至是空表单.

编辑: 对不起,我已经忘了,这个应用程序不再完成更新,而是另一个,这与运行缓慢的应用程序无关.

更新慢速应用程序的应用运行正常.如果它没有运行,它只会更新慢速应用程序.

但是慢速App会删除本地Temp-path中的文件. System.IO.File.Delete()

.net c# x86-64 windows-7

1
推荐指数
1
解决办法
2359
查看次数