小编koe*_*oen的帖子

Lock()在静态方法中

我有一个多线程应用程序,使用静态方法写入设置xml文件.我想避免文件同时更新两次(导致访问/写入异常).

我怎么做?

这不起作用:

namespace Program
{
    public class Settings
    {
        private static void SetSettingsValue (string settings, string value)
        {
            // make this thread safe to avoid writing to a locked settings xml file
            lock (typeof(Settings))
            {
                //write data to xml file
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# locking

19
推荐指数
2
解决办法
3万
查看次数

两次服务器500错误后,C#HttpWebRequest超时

在我生成两个由于"(500)内部服务器错误500"而引发异常的C#HttpWebRequests之后,第三次尝试抛出超时异常.为什么不抛出另一个(500)内部服务器错误异常?

当我重新启动我的应用程序时,它会抛出两个500错误,然后再次开始超时.

这是我的代码:

GetPages GetPages = new GetPages();
string test = GetPages.GetPage(); /* Exception: (500) Internal Server Error */
GetPages.Dispose();

GetPages GetPages = new GetPages();
string test = GetPages.GetPage();  /* Exception: (500) Internal Server Error */
GetPages.Dispose();

GetPages GetPages = new GetPages();
string test = GetPages.GetPage();  /* Exception: time out, why? */
GetPages.Dispose();
Run Code Online (Sandbox Code Playgroud)

这是GetPages类和GetPage方法:

namespace MyNamespace
{
    class GetPages
    {
        public string GetPage()
        {
            this.httpRequest = (HttpWebRequest)WebRequest.Create("http://myurl");

            try
            {
                StringBuilder postData = new StringBuilder(100);
                postData.Append("test=test");
                byte[] dataArray = Encoding.UTF8.GetBytes(postData.ToString());

                httpRequest.CachePolicy …
Run Code Online (Sandbox Code Playgroud)

c# httpwebrequest

16
推荐指数
2
解决办法
1万
查看次数

C#第一次使用GetRequestStream()需要20秒

我用C#.

我第一次在代码中使用WebRequest GetRequestStream()时,最多需要20秒.之后需要不到1秒钟.

以下是我的代码.行"this.requestStream = httpRequest.GetRequestStream()"导致延迟.

StringBuilder postData = new StringBuilder(100);
postData.Append("param=");
postData.Append("test");
byte[] dataArray = Encoding.UTF8.GetBytes(postData.ToString());

this.httpRequest = (HttpWebRequest)WebRequest.Create("http://myurl.com");

httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";

httpRequest.ContentLength = dataArray.Length;

this.requestStream = httpRequest.GetRequestStream();

using (requestStream)
    requestStream.Write(dataArray, 0, dataArray.Length);

this.webResponse = (HttpWebResponse)httpRequest.GetResponse();

Stream responseStream = webResponse.GetResponseStream();
StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
String responseString = responseReader.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)

我怎么才能看出是什么原因造成的?(例如:DNS查找?服务器没有响应?)

感谢和问候,Koen

c#

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

MySQL查询使用filesort和临时

我使用的是一个简单的MySQL查询,但由于使用了ORDER BY,性能确实很差.我无法弄清楚为什么MySQL使用filesort和临时.

我的查询是:

EXPLAIN 
SELECT * FROM Events
INNER JOIN EventLogFiles ON ServerID = 42
AND Events.LogFileID = EventLogFiles.LogFileID
ORDER BY ReportID DESC , TimeWritten DESC 
LIMIT 100
Run Code Online (Sandbox Code Playgroud)

这是EXPLAIN的输出:

Mysql EXPLAIN输出

表事件结构

表事件结构

表事件索引

表事件索引

表EventLogFiles结构

表EventLogFiles结构

表EventLogFiles索引

表EventLogFiles索引

更新:

我试图创建两个新索引,但两者仍然迫使MySQL使用filesort和临时.

ALTER TABLE Events ADD INDEX  `ReportID_TimeWritten_ServerID_LogFileID` ( ReportID DESC,  TimeWritten DESC,  ServerID,  LogFileID)

ALTER TABLE Events ADD INDEX  `ServerID_LogFileID_ReportID_TimeWritten` ( ServerID,  LogFileID, ReportID DESC,  TimeWritten DESC)
Run Code Online (Sandbox Code Playgroud)

mysql query-optimization

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

在运行时更改消息/文本(Inno Setup)

在innosetup脚本中,可以定义如下消息:

[Messages]
WelcomeLabel2=This wizard will update [name] to version [name/ver]
Run Code Online (Sandbox Code Playgroud)

现在我想在运行时更改此消息,如下所示:

procedure InitializeWizard;
begin
    //this doesn't work        
    WelcomeLabel2=NEW MESSAGE 
end;
Run Code Online (Sandbox Code Playgroud)

这样做的正确方法是什么?我想动态更改欢迎页面的内容,以显示安装程序是执行新安装还是更新.基于安装目录中是否存在某些可执行文件.

inno-setup

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

Jquery选择锚的父亲的孩子

我想选择并切换()锚点父级父级的子级.

这是我想要做的一个例子.它不起作用:)

<div>

    <div>
        <a href="#" onclick="$(this + ':parent:parent .inner').toggle();">hide</a>
    </div>

    <div class="inner">
        To be toggled.
    </div>

</div>
Run Code Online (Sandbox Code Playgroud)

jquery

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

json_decode无法解码JSON字符串

我有一个gzip压缩的base64 JSON字符串,转换后这个字符串json_decode()无法解码它.我认为这是一个编码问题,但一直无法解决.

我正在运行PHP 5.2,它不支持json_last_error().

有谁知道这里发生了什么?

<?php 
$gzipped_base64 = "7b0HYBxJliUmL23Ke39K9UrX4HShCIBgEyTYkEAQ7MGIzeaS7B1pRyMpqyqBymVWZV1mFkDM7Z28995777333nvvvfe6O51OJ/ff/z9cZmQBbPbOStrJniGAqsgfP358Hz8ifvGv8dGv8frXaH+N7NeY/hpvf40vf43LXyP/Nepf4/zXKH+N6te4ou8e0f9f0e/4tqHff8mv8f8A";

$json = gzinflate(base64_decode($gzipped_base64));

echo $json; // prints: {"StackOverflow":"Rocks"}

$array = json_decode($json, true);

echo var_dump($array); // prints: NULL
?>
Run Code Online (Sandbox Code Playgroud)

php json

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

如何在 mainloop() 之后创建新的 Tkinter 窗口?

我想在我的屏幕上动态创建 Tkinter 窗口。我知道我应该只有一个 mainloop()。我使用线程模块使 mainloop 在单独的线程中执行,因此它不会阻塞脚本。

执行主循环后如何创建更多 Tkinter 窗口?

请看一下我的代码:

from Tkinter import *
import threading
import time

class box:
    def __init__(self, pos):
        self.master = Tk()
        self.master.geometry(pos) 
        self.canvas = Canvas(self.master, width=50, height=50, highlightthickness=0 )
        self.canvas.pack()
        self.rect = self.canvas.create_rectangle(0, 0, 50, 50, fill="red", outline="red")
        self.text = self.canvas.create_text(25, 24, text="99",fill="white", font=("calibri", 24, "bold"))

    def changeFill(self, color):
        self.canvas.itemconfig(self.rect, fill=color, outline=color) # change color

class box_manager(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.boxes = {}
        self.add_box(1, "50x50+300+300")
        self.add_box(2, "50x50+100+100")
    def add_box(self, num, pos):
        self.boxes[num] = box(pos)
    def …
Run Code Online (Sandbox Code Playgroud)

python tkinter

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