multithreading

Show Progressbar when loading data using EntityFrameWork in WPF

醉酒当歌 提交于 2021-01-28 06:08:09
问题 I want to show the progress of Data being fetched from Database in a Progressbar . Currently i am updating the content of a Button . Have no idea how to implement in this code . private async void FetchInvoicesDataFunc(object sender, RoutedEventArgs e) { ProgressBtn.Content = "Loading Data ..."; InvoiceGrid.ItemsSource = await Task.Run(FetchInvoiceDataAsync); ProgressBtn.Content = "Loaded !"; } private async Task<List<Invoice>> FetchInvoiceDataAsync() { List<Invoice> result; using(var context

Multi-threaded websocket server on Python

感情迁移 提交于 2021-01-28 05:31:43
问题 Please help me to improve this code: import base64 import hashlib import threading import socket class WebSocketServer: def __init__(self, host, port, limit, **kwargs): """ Initialize websocket server. :param host: Host name as IP address or text definition. :param port: Port number, which server will listen. :param limit: Limit of connections in queue. :param kwargs: A dict of key/value pairs. It MAY contains:<br> <b>onconnect</b> - function, called after client connected. <b>handshake</b> -

Multi-threading and Single-threading performance issues in CPU-bound task

冷暖自知 提交于 2021-01-28 05:31:05
问题 The two following single-threading and multi-threading scripts are taking the same time when I give as input a big number like 555550000 single thread import threading, time a=[] def print_factors(x): for i in range(1, x + 1): if x % i == 0: a.append(i) n=int(input("Please enter a large number")) print ("Starting time is %s" % ( time.ctime(time.time()) )) print("The factors of",n,"are:") thread = threading.Thread(target=print_factors,args=(n,)) thread.start() thread.join() print("Finishing

Dispose WaitHandle for basic thread synchronization

天大地大妈咪最大 提交于 2021-01-28 05:08:40
问题 According to documentation, WaitHandle in .NET should be explicitly/implicitly disposed. However, I'm having trouble achieving this for the following basic synchronization task: a time consuming task is being executed on a thread. the main thread waits for the task to complete for a predefined time-period. The main thread must proceed if a. the task is completed or b. the timeout occurred. Here my attempt at using an AutoResetEvent object: using(var waitHandle = new AutoResetEvent(false)){

How to limit the number of Threads

≡放荡痞女 提交于 2021-01-28 04:50:24
问题 There are 101 things stored in THINGS variable. The code declares 101 threads and executes them all at once instantly all at the same time. I wonder if we can limit the number of the active threads to just 12. At first only 12 threads should pick their 12 THINGS to process. The rest of the threads should be waiting for the first 12 to finish their jobs. When the first 12 threads are all done then the next 12 threads would pick up the next 12 THINGS to process. And so one. Would it be possible

pclose() prematurely returning in a multi-threaded environment (Solaris 11)

牧云@^-^@ 提交于 2021-01-28 04:39:45
问题 I'm trying to implement a tool that starts 2 ssh connections and executes a script that requires root permission. Here is a very simple implementation: void* SshConnection(void* args) { char buffer[5000]; FILE* popenReturn = NULL; //get the hostname to connect to const char* hostname = (const char*)args; snprintf(buffer, sizeof(buffer), "/usr/bin/gnome-terminal -x bash -c \"" "ssh -t user@%s " "-i /home/user/.ssh/key.key " "\\\"/home/user/DoRootThings.bash\\\" ",hostname); popenReturn = popen

Making class fields thread-safe

有些话、适合烂在心里 提交于 2021-01-28 04:00:55
问题 Say I have a List of values that I accessed by different threads. So I make it thread safe by doing something like this: private static object syncObject; syncObject = new Object(); public List<double> MyList { get { lock(syncObject) { return myList;} } set { lock(syncObject) { myList = value;} } } If in one of my functions, I need to access the Count attribute of the List , it may not be thread safe anymore. So how do I write it such that even when accessing the attributes, it will be thread

Making class fields thread-safe

大憨熊 提交于 2021-01-28 03:28:20
问题 Say I have a List of values that I accessed by different threads. So I make it thread safe by doing something like this: private static object syncObject; syncObject = new Object(); public List<double> MyList { get { lock(syncObject) { return myList;} } set { lock(syncObject) { myList = value;} } } If in one of my functions, I need to access the Count attribute of the List , it may not be thread safe anymore. So how do I write it such that even when accessing the attributes, it will be thread

CreateNamedPipe ERROR_INVALID_NAME

对着背影说爱祢 提交于 2021-01-28 03:10:23
问题 Code snippet: void RunThread(void* unused_args) { PSECURITY_DESCRIPTOR sdsc; ULONG size; ConvertStringSecurityDescriptorToSecurityDescriptor("S:(ML;;NW;;;LW)", SDDL_REVISION_1, &sdsc, &size); SECURITY_ATTRIBUTES sa; sa.nLength = sizeof(sa); sa.bInheritHandle = false; sa.lpSecurityDescriptor = sdsc; HANDLE pipe = CreateNamedPipe("\\.\pipe\mmaivpc_test_pipe", PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE, 255, 1024, 1024, 0, &sa); DWORD error = GetLastError(); } If you haven't figured it out from the

Waiting for a result without blocking a thread

半城伤御伤魂 提交于 2021-01-28 03:04:15
问题 I have this code doing what I want: TriggerSomeExternalProcess(); double secondsElapsed = 0; DateTime startTime = DateTime.UtcNow; double timeoutInSeconds = 10; while (secondsElapsed < timeoutInSeconds) { // TODO: this seems bad... secondsElapsed = DateTime.UtcNow.Subtract(startTime).TotalSeconds; } CheckStatusOfExternalProcess(); The goal is to TriggerSomeExternalProcess and then CheckStatusOfSomeExternalProcess - but that process runs on the same thread so I can't do Thread.Sleep() . It's