Click here to Skip to main content
15,886,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

C#
    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;
    }
Posted

instead of progress bar use can use ajax loaders (.gif) files this is easier.
 
Share this answer
 
Comments
jkhann 14-Feb-13 2:27am    
It is still the same. :(
Hi,
Have you tried this.
C#
//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;
}


Regards
Jegan
 
Share this answer
 
v3
Comments
jkhann 14-Feb-13 2:22am    
Nope, still the same . Frozen when loading.
Hi That means you are blocking the main thread.

Try this:

C#
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;
}


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
 
Share this answer
 
Comments
jkhann 15-Feb-13 1:02am    
Thanks Jegan. How is Task better than backgrounworker? This code induced some sort of error but i will work from this direction.
Hi Have a look at this article,

http://jeremybytes.blogspot.ca/2012/05/backgroundworker-component-im-not-dead.html[^]

This explains when to use background worker and when to use TPL. This article also has a link to solution similar to what you are looking for.

http://www.jeremybytes.com/Demos.aspx#KYUIR[^]


Regards
Jegan
 
Share this answer
 
Comments
jkhann 15-Feb-13 4:45am    
Thanks again.I am looking at this tutorial and hope something good will happen soon !
http://www.rocksolidknowledge.com/ScreenCasts.mvc/Watch?video=TasksAndThreadAffinity.wmv

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900