c# - Show Form Message using non-Main UI Thread -


i have project without usign form/button or nothing that, connects websocket , using async methods receives message(on form created myself) supposed appear on top-right corner of screen.

but message can appear time time (2 or 3 minutes) on screen if websocket doesn't must stop. , message can big enough, in order make better make message appear in more 1 form.

it causes impression it's notification. class connects websocket , receives message async, calls class using thread controller. purpose of controller time time, show message in various new form() notifications , don't if websocket doesn't return message.

but when call form.show program stops working. i've looked around stackoverflow already, ideas i've found didn't seem work.

some should use invoke, kept giving error saying "invoke or begininvoke cannot called on control until window handle has been created", tried solve this: c# calling form.show() thread didn't work.

some said should use .showdialog instead of .show, doesn't appear good, because waits window closed terminate method , said need open more 1 notification @ same time.

some said form open .show, open little period of time. couldn't notice if case , if couldn't solve it. well, matter i'm stuck , don't know more.

edited code:

//main application.run(new socketservice());  //socketservice class         public socketservice()         {             alerta = null;              while (true)             {                 try                 {                     //console.writeline("nome usĂșario:" + environment.username);                     thread.sleep(2000);                     connect("ws://192.168.120.38:9091").wait();                 }                 catch (exception ex)                 {                     console.writeline(ex);                 }             }         }          public static async task connect(string uri)         {             clientwebsocket websocket = null;              try             {                 websocket = new clientwebsocket();                 await websocket.connectasync(new uri(uri), cancellationtoken.none);                 await login(websocket);             }             catch (exception ex)             {                 throw ex;             }                         {                 if (websocket != null)                     websocket.dispose();                  lock (consolelock)                 {                     console.foregroundcolor = consolecolor.red;                     console.writeline("websocket closed.");                     console.resetcolor();                 }             }         }          private static async task login(clientwebsocket websocket)         {             arraysegment<byte> buffer = new arraysegment<byte>(encoder.getbytes(        "{\"event\":\"loginbrowser\",\"data\":{\"login\":\"000000003077\",\"data\":\"1\"}}"));             await websocket.sendasync(buffer, websocketmessagetype.text, true, cancellationtoken.none);              if (websocket.state == websocketstate.open)             {                 if (showmessage.created != true)                 {                     var dummy = new control(); // initialize synchronizationcontext                     _sync = synchronizationcontext.current;                     new thread(threadproc).start();                 }                  await receive(websocket);                            }          }          private static async task receive(clientwebsocket websocket)         {             while (websocket.state == websocketstate.open)             {                 arraysegment<byte> buffer = new arraysegment<byte>(new byte[256]);                 var result = await websocket.receiveasync(buffer, cancellationtoken.none);                  if (result.messagetype == websocketmessagetype.close)                 {                     await websocket.closeasync(websocketclosestatus.normalclosure, string.empty,        cancellationtoken.none);                 }                 else                 {                     if (result.endofmessage)                     {                         message += encoder.getstring(buffer.toarray());                         sendmessage(message);                     }                     else                     {                         message += encoder.getstring(buffer.toarray());                     }                 }             }         }          public static void showformfromanotherthread(string text)         {             _sync.post(sendorpostcallback, text);         }          private static void sendorpostcallback(object state)         {             var form = new notification();             form.text = (string)state;             form.show();         }          private static void threadproc()         {             while (true)             {                 thread.sleep(2000); // wait imitation                 showformfromanotherthread("hi");             }         }           /*notification form , depending on put part:          var dummy = new control(); // initialize synchronizationcontext         _sync = synchronizationcontext.current;         new thread(threadproc).start(); 

or doesn't call login or doesn't enter receive() method or best case receives information calls threadproc , showformfromanotherthread doesn't enter sednorpostcallback*/

using system.threading; using system.windows.forms;  namespace consolethreadsync {     internal class program     {         private static void main(string[] args)         {             application.run(new app());         }     }      public class app : applicationcontext     {         private readonly synchronizationcontext _sync;          public app()         {             var dummy = new control(); // initialize synchronizationcontext             _sync = synchronizationcontext.current;             new thread(threadproc).start();         }          public void showformfromanotherthread(string text)         {             _sync.post(sendorpostcallback, text);         }          private void sendorpostcallback(object state)         {             var form = new form1();             form.text = (string)state;             form.show();         }          private void threadproc()         {             while (true)             {                 thread.sleep(2000); // wait imitation                 showformfromanotherthread("hi");             }         }     } } 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -