In Outlook I have this code...
_connection = New HubConnection(Me._url)
_connection.Credentials = CredentialCache.DefaultCredentials
_hub = _connection.CreateHubProxy(Me._hubName)
_hub.On(Of String)("NewMessage", Function(message)
Dim f As New TestForm
f.Show()
Return ""
End Function)
_connection.Start()
But showing my form "TestForm" crashes since its in the main thread and SignalR is on another thread.
Any idea how I can make it work?
Your best attempt would be to use a dedicated thread to do your SignalR jobs and get back on the main STA thread for accessing Outlook Object Model or displaying forms/WPF components.
This how you can "continue" your work on the main thread:
Hooked events Outlook VSTO continuing job on main Thread
Related
I'm trying to make a game (Universal DX11 application) and at some point I need access to image library to allow user to select avatar. But for some reason call of PickSingleFileAsync on picker rises an exception.
Windows::Storage::Pickers::FileOpenPicker^ openPicker = ref new Windows::Storage::Pickers::FileOpenPicker();
openPicker->SuggestedStartLocation = Windows::Storage::Pickers::PickerLocationId::PicturesLibrary;
openPicker->ViewMode = Windows::Storage::Pickers::PickerViewMode::Thumbnail;
// Filter to include a sample subset of file types.
auto filters = openPicker->FileTypeFilter;
filters->Clear();
filters->Append(".png");
openPicker->PickSingleFileAsync();// same exception with create_task(...);
Seems like the sample works only if I put it into UI thread. How can I use picker from my own thread?
UPD: HRESULT:0x80004005
Ok, I just decided to call dipatcher's RunAsync to execute this code. But I still have no idea why I cannot open picker inside non-UI thread.
I have a lightswitch app which sends an email when a new job is added. I added a sendemail helper class which is called when the record is inserted.
This works however the interface hangs on save waiting for the email to be sent.
I would like to perform this asychronously so that the user can go on and do his thing while the email sends in the background.
I've tried creating a new thread in the inserted part, hoping it would spin off the thread and then return to the user, but it doesn't work, it is still waiting for the thread to finish.
Thread caseemail = new Thread(new ParameterizedThreadStart(newSendmail.generateCaseEmail));
string[] paramsToPass = new String[] { entity.ProjectNumber, entity.CreatedBy, entity.TheProjectClientManagerFixed, entity.ProjectName };
caseemail.Start(paramsToPass);
How should I be doing this?
So in the end this code works, the errors were from problems access dataworkspaces from the other thread, which is obviously not allowed.
I will leave this here as an example of how to spin off a task into the background from lightswitch, leaving the interface responsive while lightswitch goes away and does something else.
I am running the following code when i click on a button:
foreach (string item in urlQueue)
{
log("creating job " + iia.ToString());
_smartThreadPool.QueueWorkItem(
new Amib.Threading.Func<string, int, int, string, int>(checkURLSmart),
item, iia, 5000, kryptonTextBox1.Text);
iia++;
}
Application.DoEvents();
_smartThreadPool.Start();
_smartThreadPool.WaitForIdle();
_smartThreadPool.Shutdown();
For some reason this is blocking the UI thread, any ideas how to fix this? I want the UI to be responsive while the queue is working
You shouldn't call WaitForIdle() in GUI thread.
Use http://msdn.microsoft.com/en-us/library/a06c0dc2.aspx in .NET or http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html invokeLater() in Java instead.
This mechanism put your code in queue for execution in GUI thread. So you are able to update GUI view with processed data in callback parameters.
Also see explanation about differences between Invoke & BeginInvoke: What's the difference between Invoke() and BeginInvoke()
Im exporting a reports from dataset to a execel file,i have a lots of reports so it consuming me a lots of time,so I try to solve this by the background worker cuz I'm working on windows form application,but the new issue come,when begin excuting the report my form is not responding,I'm not able to move the form or click on the stop button(that stop the application).
this is a sample of my code :
#region x Report
PrimaryReportsThreads++;
ADIR_Parameters ADIR_Parms = ConfigManager.GetADIRParameters();
BackgroundWorker ADIR_worker = new BackgroundWorker();
AllThreads.Add(ADIR_worker);
ADIR_worker.DoWork += new DoWorkEventHandler(ADIR_worker_DoWork);
ADIR_worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
ADIR_worker.RunWorkerAsync(ADIR_Parms);
#endregion
Sounds like COM issue.
Probably you should try using MTA, instead of STA
Helpfull links:
http://msdn.microsoft.com/en-us/library/ms809971.aspx
http://msdn.microsoft.com/en-us/library/system.threading.apartmentstate.aspx
I need help how to unfreeze my dialog box. I'm using MFC and I have an infinite loop I want to execute when a button is pressed. However, the dialog box freezes when the infinite loop starts. Now I looked at this thread where someone was having a similar problem.
Unfortunately I tried multithreading but I found out that It can't work for me because I'm using an api that uses OLE automation and I'm getting an unhandled memory exception. I think this is because program uses the serial port and i read somewhere you can only use the handle to the serial port in one thread.
My program is simply to see if someone has dialed in to my modem and wait for them to send me a file, then hangup. Here is my loop.
while(1)
{
//get rid of input buffer
ts->_this->m_pHAScript->haReleaseRemoteInput();
ts-> _this->textBox->SetWindowTextA("thread Commence");
//wait for connected
if(success = ts->_this->m_pHAScript->haWaitForString("CONNECT",timeout))
{
//getFile
if(success = ts->_this->m_pHAScript->haWaitForXfer(5000))
{
//hangup
ts->_this->haTypeText("+++ath\r");
}
}
}
Is there a way to unfreeze the dialog box?
Add this code inside while loop:
MSG msg;
while(PeekMessage(&msg, GetSafeHwnd(), 0, 0, PM_REMOVE))
{
DispatchMessage(&msg);
}
The GUI in Windows relies on a message loop - somewhere in your code, either explicitly or hidden in a framework, there's a loop that checks for a message in a queue and processes it. If anything blocks the code from returning to that loop, the GUI gets frozen.
There are a few ways around this. One was given by David Brabant, essentially duplicating the loop. Another is to start a new "worker" thread that runs the blocking operation independently. If your message loop has a function that it calls when it is idle, i.e. no more messages are in the queue, you can do some processing there; that's not possible in your example however.