进度条不与backgroundworker一起工作 [英] progress bar not working with backgroundworker
问题描述
大家好,
我有一个服务器 - 客户端系统正在进行控制服务器表单的客户端命令。
命令是连接,断开连接,发送和接收。
当客户想要发送数据时/从服务器接收数据(命令==发送/接收),在同一服务器表单上显示进度条(选框样式)并在发送/接收完成时隐藏。进度条工作,直到我开始调用服务器命令(冻结)。
问题是服务器命令是(并且必须)由主服务器执行线。
所以即使我在后台工作程序中运行命令,它也无济于事,因为在主线程上调用了实际的服务器命令 - 显示相同的线程进度条。
有什么想法吗?
Hi guys,
I have a server-client system going on with client commands controlling the server form.
The commands are connect, disconnect, send and receive.
When client wants to send data to/receive data from server (command == send/receive), there will be a progress bar (marquee style) shown on the same server form and hide when sending/receiving is done. The progress bar works until i start to invoke server commands (freeze).
The problem is that the server commands are (and must be) executed by server in the main thread.
So even if i run the command in a backgroundworker, it doesn’t help since the actual server commands are invoked on the main thread – the same thread that shows the progress bar.
Any ideas?
private SynchronizationContext context = SynchronizationContext.Current ?? new SynchronizationContext();
private Thread _serverThread = null;
public bool startServer()
{
if (_serverThread != null)
{
return false;
}
Server _Server = null;
_Server = new Server();
_Server.callback += new EventHandler(server_callback);
return true;
}
void server_callback(object sender, EventArgs e)
{
ServerEventArgs types = e as ServerEventArgs;
if (types != null)
{
context.Send(new SendOrPostCallback(processCommand), e);
}
}
public void processCommand(Object o)
{
ServerEventArgs args = o as ServerEventArgs;
if (args != null)
{
string input = args.command;
string status = _revitWorker.ProcessCommand(app, input, true);
statusUpdate(status);
string output = _revitWorker.ProcessCommand(app, input, false);
args.response = output;
}
}
//update status
private void statusUpdate(string status)
{
if (status == "disconnect")
{
disconnectFlag();
}
else if (status == "loading")
{
ProcessingFlag();
}
else
{
connectFlag();
}
}
//disconnect
public void disconnectFlag()
{
label1.Text = "disconnect";
progressBar1.Visible = false;
}
//processing
public void processingFlag()
{
label1.Text = "processing";
progressBar1.Visible = true; //progress bar appear only when loading
}
//connect
public void connectFlag()
{
label1.Text = "ready";
progressBar1.Visible = false;
}
推荐答案
代替进度条使用可以使用ajax加载器(.gif)文件这样更容易。
instead of progress bar use can use ajax loaders (.gif) files this is easier.
你好,
你试过吗?
Hi,
Have you tried this.
//disconnect
public void SetStatusDelegate (bool state, string label);
public void disconnectFlag()
{
new SetStatusDelegate(SetStatus).Invoke(false, "disconnect");
}
//processing
public void processingFlag()
{
new SetStatusDelegate(SetStatus).Invoke(true, "processing");
//progress bar appear only when loading
}
//connect
public void connectFlag()
{
new SetStatusDelegate(SetStatus).Invoke(false, "ready");
}
private void SetStatus(bool status, string label)
{
label1.Text = label;
progressBar1.Visible = status;
}
问候
Jegan
Regards
Jegan
你好
这意味着你阻止了主线程。
试试这个:
Hi That means you are blocking the main thread.
Try this:
public bool startServer()
{
if (_serverThread != null)
{
return false;
}
Task taskA = Task.Factory.StartNew(() =>
{
Server _Server = null;
_Server = new Server();
_Server.callback += new EventHandler(server_callback);
});
return true;
}
任务工厂将保证启动一个单独的线程,我不确定你是否需要检查完全是_serverThread。
问候
Jegan
The Task factory will guarantee to start a separate thread, and I am not sure whether do you need to check the "_serverThread" at all.
Regards
Jegan
这篇关于进度条不与backgroundworker一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!