我有一个简单的Delphi程序,我正在努力,我试图使用线程将程序的功能与其GUI分离,并在更冗长的任务期间保持GUI响应等.基本上,我有一个'控制器'TThread和'视图'TForm.视图知道控制器的句柄,它用于通过控制器发送控制器消息PostThreadMessage.我过去没有问题使用这种模型用于不是主要形式的表单,但出于某种原因,当我尝试将此模型用于主表单时,线程的消息循环才会退出.
这是我的线程消息循环的代码:
procedure TController.Execute;
var
Msg : TMsg;
begin
while not Terminated do begin
if (Integer(GetMessage(Msg, hwnd(0), 0, 0)) = -1) then begin
Synchronize(Terminate);
end;
TranslateMessage(Msg);
DispatchMessage(Msg);
case Msg.message of
// ...call different methods based on message
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
要设置控制器,我这样做:
Controller := TController.Create(true); // Create suspended
Controller.FreeOnTerminate := True;
Controller.Resume;
Run Code Online (Sandbox Code Playgroud)
为了处理主表单的消息,我尝试使用两个Application.Run和以下循环(紧接在后面Controller.Resume)
while not Application.Terminated do begin
Application.ProcessMessages;
end;
Run Code Online (Sandbox Code Playgroud)
我已经被困在这里 - 任何帮助将不胜感激.
我正在研究一种从我们的应用程序更安全地发送电子邮件的方法。
我们目前正在将电子邮件直接发送到IIS SMTP服务器,但是正在研究在服务器发生故障,重新启动等情况下更安全地传递电子邮件的方法。
我在想实现此目的的一种方法是将电子邮件(带有附件)存储在队列中,由一个单独的进程进行处理,或者将电子邮件存储在要处理的数据库中。
我想获得一些建议,任何建议将不胜感激。
谢谢
我不确定以下是否可行,但我想以一种受限制的方式在Paralell中调用一些Actions,但是要保持处理流程的连续性,而不是恢复使用定时器或循环/睡眠周期.
到目前为止,我已经开始工作了它从一些来源加载大量输入......然后以受控方式并行处理它们并像下面一样循环.
static void Main(string[] args)
{
while(true) //Simulate a Timer Elapsing...
{
IEnumerable<int> inputs = new List<int>() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//Simulate querying database queue tables for next batch of entries
RunAllActions(inputs, 3); //Max 3 at a time.
}
}
static void RunAllActions(IEnumerable<int> inputs, int maxConcurrency)
{
var options = new ParallelOptions() {MaxDegreeOfParallelism = maxConcurrency};
Parallel.ForEach<int>(inputs, options, DoWork);
//Blocks here until all inputs are processed.
Console.WriteLine("Batch of Work Done!!!");
}
static void DoWork(int …Run Code Online (Sandbox Code Playgroud) 我见过的工作队列的正常实现涉及互斥和条件变量.
A) Acquires Lock
B) While Queue empty
Wait on Condition Variable (thus suspending thread and releasing lock)
C) Work object retrieved from queue
D) Lock is released
E) Do Work
F) GOTO A
Run Code Online (Sandbox Code Playgroud)
A) Acquires Lock
B) Work is added to queue
C) condition variable is signaled (potentially releasing worker)
D) Lock is released
Run Code Online (Sandbox Code Playgroud)
我一直在浏览一些代码,我看到了一个使用POSIX管道的实现(我以前没见过这种技术).
A) Do select on pipe (thus suspending thread while no work)
B) Get Job from pipe
C) Do Work
D) GOTO …Run Code Online (Sandbox Code Playgroud) 这是我的情况:
我的问题:
我最初的解决方案:
我最初的解决方案限制:
我的问题(最终!):
所有这些都变得非常复杂,并且增加了错误风险和代码维护问题。我真的不喜欢它!有没有人已经遇到过这种问题?他们可以接受的解决方法吗?有人对我的方案有一个干净的解决方案的想法吗?
谢谢!
你好真棒的Erlang社区!
我正在制作一个包含客户端和后端的小项目.(复杂..对吧?):)
我在erlang中制作它.
客户端和后端将是两个独立的进程,我想知道我是否需要(或应该)使用某种消息队列来让它们进行交互?
我知道我可以让他们使用他们的PID进行交互并使用"!"发送消息.运营商.
我想我想说的是我在努力寻找这个问题的答案:
"为什么或何时应该在Erlang中使用诸如RabbitMQ,ZeroMQ之类的消息队列"?
在.NET上的MSMQ中,我使用MessageEnumerator来查看队列中的所有消息.我想删除符合特定条件的邮件.
当我调用MoveNext来逐步执行队列时,我得到一个布尔值来告诉我当前消息是否存在.但是,当我执行RemoveCurrent时,如何知道删除后的当前消息是否存在?检查Current并处理异常的唯一方法是什么?
这是一个示例,我使用简单的条件删除超过一年的消息.假设队列是在别处创建的,并使用MessageReadPropertyFilter.ArrivedTime = true进行设置:
private void RemoveOldMessages(MessageQueue q)
{
MessageEnumerator me = q.GetMessageEnumerator2();
bool hasMessage = me.MoveNext();
while (hasMessage)
{
if (me.Current.ArrivedTime < DateTime.Now.AddYears(-1))
{
Message m = me.RemoveCurrent(new TimeSpan(1));
// Removes and returns the current message
// then moves to the cursor to the next message
// How do I know if there is another message? Is this right?:
try
{
m = me.Current;
hasMessage = true;
}
catch (MessageQueueException ex)
{
hasMessage = false;
}
}
else …Run Code Online (Sandbox Code Playgroud) 下面的代码中提供了ActiveMQ实现。有时,系统停止工作并变得非常缓慢。当我使用JavaMelody检查线程转储时-我看到太多线程长时间处于Runnable状态,并且没有终止。
ActiveMQ版本 -activemq-all-5.3.0.jar
请参考以下代码:
经纪人:
public class ActiveMQ extends HttpServlet {
private static final long serialVersionUID = -1234568008764323456;
private static final Logger logger = Logger.getLogger(ActiveMQ.class.getName());
public Listener listener;
private String msgBrokerUrl = "tcp://localhost:61602";
public BrokerService broker = null;
public TransportConnector connector = null;
@Override
public void init() throws ServletException {
try {
broker = new BrokerService();
broker.setPersistent(false);
broker.setUseJmx(false);
connector = broker.addConnector(msgBrokerUrl);
broker.setUseShutdownHook(true);
System.out.println("BROKER LOADED");
broker.start();
broker.deleteAllMessages();
listener = new Listener();
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
}
听众: …
我正在使用python语言来使用Azure总线服务队列发送和接收消息。
使用以下代码从队列中删除消息时,出现“提供的锁无效。该锁已过期,或者消息已从队列中删除”。
sbs.delete_queue_message('taskqueue',5,'ef4e2189-bfef-42ac-ba09-7fd20287f6a9')sbs.delete_queue_message('taskqueue','SequenceNumber','LockToken')
from azure.servicebus.control_client import ServiceBusService, Message, Topic, Rule, DEFAULT_RULE_NAME
key_name = '###############' # SharedAccessKeyName from Azure portal
key_value = '####################' # SharedAccessKey from Azure portal
service_namespace = '###########'
sbs = ServiceBusService(service_namespace,shared_access_key_name=key_name,shared_access_key_value=key_value)
msg = sbs.receive_queue_message('taskqueue')
sbs.delete_queue_message('taskqueue',5,'ef4e2189-bfef-42ac-ba09-7fd20287f6a9')
Run Code Online (Sandbox Code Playgroud)