我的服务在随机的时间内完美地工作,然后停止.
我正在捕捉我能看到的所有异常.我已经在服务创建中添加了一个try/catch来尝试获得某种错误,但我什么都没得到.服务刚刚停止.
try
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new my_service() };
ServiceBase.Run(ServicesToRun);
}
catch (Exception e)
{
logEvent(e); // Log any errors to a .txt file.
}
Run Code Online (Sandbox Code Playgroud)
事件查看器显示以下内容:
Faulting application name: my_service.exe, version: 0.9.2.1, time stamp: 0x50717154
Faulting module name: ntdll.dll, version: 6.1.7601.17514, time stamp: 0x4ce7b96e
Exception code: 0xc0000005
Fault offset: 0x00055f99
Faulting process id: 0x%9
Faulting application start time: 0x%10
Faulting application path: %11
Faulting module path: %12
Report Id: %13
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法来使用Windows上的C#按名称杀死进程
.NET Compact Framework没有Process.GetProcessByName()方法,它只有一个.GetProcessById()方法.
但我不知道如何弄清楚我正在运行的进程的进程ID.
我想我可以循环所有进程ID,但这很可怕,因为我不知道进程ID的最大数量.谁知道更好的方法?
for (int i = 1; i < 40000; i++)
{
Process prs = Process.GetProcessById(i);
if (prs.StartInfo.FileName == "MyExe.exe")
{
prs.Kill();
}
prs.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
编辑:我找到了解决问题的方法.代码项目链接. http://www.codeproject.com/Articles/36841/Compact-Framework-Process-class-that-supports-full
我正在研究一个项目,该项目有很多时间根据这些时间表安排和执行任务.
我只是想知道是否有一种更简单的方法来测试我的应用程序在我的服务器的不同时区,当他们在我的电脑上运行时.
我不想更改区域设置中的时区,而是更改某个应用程序的上下文.
数据库将所有内容存储为UTC,但应用程序会在本地时间向用户显示此内容.但是我在UTC时区对我来说是一样的.
我正在使用JSON.NET将一些XML转换为JSON.
我的XML看起来像这样:
<Root>
<Product>
<Name />
<Id />
</Product>
<Product>
<Name />
<Id />
</Product>
</Root>
Run Code Online (Sandbox Code Playgroud)
我使用这种方法转换xml:
private string ConvertToJson(string xml)
{
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.LoadXml(xml);
var JsonString = JsonConvert.SerializeXmlNode(XmlDoc);
return JsonString;
}
Run Code Online (Sandbox Code Playgroud)
只要有多个产品,JSON.NET就会创建一个JSON数组.但是,如果只有一个产品,JSON.NET将不会创建JSON数组,但我需要它.
有什么方法可以强制它创建一个JSON数组?
所以我有一个如下所示的 TextBlock,其中我在中间显示一个名称和绑定名称。
<TextBlock>
<Run Text="Hello"/> <Run Text="{Binding Name}" /><Run Text=","/>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
有没有什么方法可以在此处设置一个条件,即当“Name”为 null 时,或者如果当前 DataContext 对象为 null 则更好,那么我根本不显示任何内容?
我使用 ConcurrentBag 来包含字符串列表。有时它会包含一个副本。
但是我在添加新条目之前检查了它的内容,所以它不应该有重复。
ConcurrentDictionary<string, string> SystemFiles = PopulateSystemFiles();
ConcurrentBag<string> SystemNames = new ConcurrentBag<string>();
Parallel.ForEach(SystemFiles, file =>
{
string name = GetSystemName(file.Value);
if (!SystemNames.Contains(name))
{
SystemNames.Add(name);
}
});
Run Code Online (Sandbox Code Playgroud)
我的假设是 .Contains 方法不是线程安全的。我对么?
c# parallel-processing concurrency multithreading thread-safety
我目前正在从几个数据库查询中生成大量输出.生成的XML文件大约为2GB.(这是一年的数据).为了节省一些磁盘空间和客户端的下载时间,我使用GZipStream类将此文件添加到压缩文件中.请参阅下文,了解我目前如何将文件压缩为gz.注意:fi对象是FileInfo.
using (FileStream inFile = fi.OpenRead())
using (FileStream outFile = File.Create(fi.FullName + ".gz"))
using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress))
{
byte[] buffer = new byte[65536];
int numRead;
while ((numRead = inFile.Read(buffer, 0, buffer.Length)) != 0)
{
Compress.Write(buffer, 0, numRead);
}
}
Run Code Online (Sandbox Code Playgroud)
这种方法工作正常,但要求我将2GB文本文件写入磁盘,然后再将其全部读回来,以便将其添加到GZipStream中,然后将其作为压缩文件再次写回.这似乎是浪费时间.
有没有办法直接将我的2GB字符串添加到GZipStream而无需先写入磁盘?
在下面的示例中,为什么Convert.FromBase64String()不会抛出异常.当然应该抛出格式异常?
string s = "This is just a test string which we will test if it is base64";
bool isBase64 = false;
try
{
byte[] b = Convert.FromBase64String(s);
isBase64 = true;
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
isBase64 = false;
}
Run Code Online (Sandbox Code Playgroud) 今天我遇到了这个问题,我想在我现有的类库项目中添加一个WPF窗口,但是“添加新项”列表中不存在该窗口。
我找到了一个解决方案,只想将其发布在这里,因为我在其他地方找不到它。
c# ×9
.net ×4
wpf ×2
base64 ×1
binding ×1
compression ×1
concurrency ×1
exception ×1
gzipstream ×1
json ×1
json.net ×1
kill ×1
process ×1
stream ×1
timezone ×1
window ×1
windows-ce ×1
xaml ×1