Update DataGridView from thread - database

So I have simple DataGridView with rows containing name, surname, status.
Now I have another sub which is starting 5 threads. Each thread will do it's job and update DGV accordingly to its result.
Problem is that to run thread accessing update method for DGV I had to set:
_namethread= New Thread(AddressOf namethread)
_namethread.IsBackground = True
_namethread.SetApartmentState(ApartmentState.STA)
With set STA I end up without errors BUT DGV isn't updated and nothing happens. It looks like call for update method simple is skipping (on update method I do have Application.DoEvents just in case)
There is also another thing. I want to use threads to update database, refresh DGV for other tables, etc.
Thing is that I know how to access textboxes from threads (by delegating method) but I have no idea how to do it with accessing database by SQL query or even DHV to show progress.
Maybe I should use event riser or something else.
How are you doing this normally :) ?
If I'm correct if I would create custom Event, I could call it from thread ?
Is this DoEvents working like other subs (so it would go step by step through all events rised, so for example would update 6 records in database ?)
Something like a queue ?
I just need to know in which direction I should go with threads (as I want do to very heavily multithreaded application)

I think I solved this issue by using delegates.

Related

Insert values in database at some intervals from a visual c++ program/project

I want to create a visual c++ program that automatically inserts some random values and the current system datetime in the database at particular intervals. I would be using the srand() function for this. I am not sure how to do this. I was able to achieve this using the winForm projects and scheduling it to run every 30 mins using Task Scheduler but the issue is that every time an empty form pops up and until and unless i close it the values are not entered. What i need is that all these happen on their own and the window should not pop up as its empty. Is winform the right way to achieve this and if not then what kinf of project should i choose. Maybe timers, threads...Please shed some light as i am relatively new to this.
Regards
PS: Here's the code
String^ constring = L"datasource=localhost;port=3306;username=root;password=root";
MySqlConnection^ conDataBase = gcnew MySqlConnection(constring);
MySqlCommand^ cmdDataBase = gcnew MySqlCommand("INSERT INTO `data`.`test` (`datetime`,`temp`,`pressure`) VALUES ('"+dt+"','"+rand_temp+"','"+rand_pressure+"');",conDataBase); MySqlDataReader^ myReader;
try{
conDataBase->Open();
myReader = cmdDataBase->ExecuteReader();
//MessageBox::Show("Data Inserted");
while(myReader->Read()){
}
}catch(Exception^ex){
MessageBox::Show(ex->Message);
}
Application::Exit();
The above code is inside my formload method
Use a console application project instead of a winforms project
You should put the code in the "Main" method and not in the constructor of your Form. You do not Need a Form at all...
Also I do not recommend using a console application, because this will popup a Console-Window...
please share your code for deeper insight.
the program is writing to the DB the values, but the DB won't be updated(A.K.A committed) until you'll close the connection/do manual commit. again share your code please. i would advise not to use winforms if you don't need a form. you can use a variety of different ways to achieve DB update. for example, you can use sleep so the thread will work once in a while(depends on how much u gave him on the sleep method).
in anyway, give us more information so we can help you
edited
after the cmdDataBase->ExecuteReader(); use the MySQLConnection.commit() in order to commit the transaction. take all your code to the main function rather then in the creation of the form. it doesn't seems you need a form at all, right?
use the Sleep method to wait for some time if you need(read here) for more information or search google.
anything more?

Multi-threading issue + DataReady already open

We are developing a WinForms application in my company.
We are facing a threading issue.
A thread TH is started in the runtime beginning.
TH reads regularly a record from a table TB.
A DataTable is used for retrieving the record.
The DataTable is filled by a SqlDataAdapter.
The DataTable is disposed as soon as an object is created modeling the retrieved record.
A DataGridView in a Form could be filled with data from TB during the runtime.
The DataGridView must be filled on the main thread that is the thread where user controls are created.
But an exception is thrown when filling a DataTable.
The exception message adverts that a DataReader is already open on the Command used to retrieve records from TB.
I have tried without success to surround a statement executed on TH in a lock block.
I am not used to threading programmation so I do not know what I can do to prevent the exception.
Any help will be greatly appreciated.
I have got useful information from this web page : http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.80).aspx
The web page contains this information :
Note that while a DataReader is open, the Connection is in use
exclusively by that DataReader. You cannot execute any commands for
the Connection, including creating another DataReader, until the
original DataReader is closed.
In my context :
DataReaders used respectively on the main thread and thread TH execute a select command for the same connection.
The SqlDataAdapter.Fill method probably uses an internal DataReader.
A try to execute the Fill method can be made on the main thread while another call to the Fill method is not yet finished on thread TH.

Salesforce Trigger workflow on record delete

I want to listen change in my legacy system whenever there is any change in SF object (add/update/delete). So I have created outbound message and workflow. But in workflow I don't see any way to fire if object is deleted.
Is there anyway I can trigger outbound message on record delete? I know have heard that it can be done by trigger. But I don't want to write apex code for this.
To the best of my knowledge it cannot be done, the workflow actions are decoupled from the workflow rule (you can even reuse them) so they probably do not receive the transaction scope and when they execute the record is already gone and any reference inside action would point to a non-existing data. Thus the only way I know how to do it is via trigger.
Here is a workaround. However this will only be able to capture deletion made via std. Salesforce UI.
1.Create a custom checkbox field "Is Deleted"
2.Override the Del link with a custom VF page, that first updates the record status to "Is Deleted", and deletes the record.
3.Write workflow rule using the "Is Deleted" field.
Perhaps a compromise architecture would be to write an extremely small and simple after delete trigger that simply copies the deleted records in question to some new custom object. That new custom object fires your workflow rule and thus sends the outbound message you're looking for. The only issue with this would be to periodically clean up your custom object data that would grow in size as you deleted records from your other object. In other words, your "scratch" object would just need periodic cleaning - which could be done on a nightly schedule with batch Apex.
Here's a delete trigger that would do the trick using Opportunity as an example:
trigger AfterDelete on Opportunity (after delete)
{
List<CustObj__c> co = new List<CustObj__c>();
for(Opportunity o : Trigger.old)
{
CustObj__c c = new CustObj__c();
c.Name = o.Name;
c.Amount__c = o.Amount;
c.CloseDate__c = o.CloseDate;
c.Description__c = o.Description;
// etc.
co.add(c);
}
insert co;
}
It's not ideal but at least this would save you from having to code your own trigger-based outbound messages. These can only be done using the #Future annotation, btw, since callouts directly from triggers are forbidden. Hope that helps.
write a single email send in the trigger delete event. You have it in less than 1 hour.

When does the defered execution occur?

I've got a situation which I want to fetch data from a database, and assign it to the tooltips of each row in a ListView control in WPF. (I'm using C# 4.0.) Since I've not done this sort of thing before, I've started a smaller, simpler app to get the ideas down before I attempt to use them in my main WPF app.
One of my concerns is the amount of data that could potentially come down. For that reason I thought I would use LINQ to SQL, which uses deferred execution. I thought that would help and not pull down the data until the user passes their mouse over the relevant row. To do this, I'm going to use a separate function to assign the values to the tooltip, from the database, passed upon the parameters I need to pass to the relevant stored procedures. I'm doing 2 queries using LINQ to SQL, using 2 different stored procedures, and assigning the results to 2 different DataGrids.
Even though I know that LINQ to SQL does use deferred execution, I'm beginning to wonder if some of the code I'm writing may defeat my whole intent of using LINQ to SQL. For example, in testing in my simpler app, I am choosing several different values to see how it works. One selection of values brought no data back, as there was no data for the given parameters. I thought this could potentially cause the user confusion, so I thought I would check the Count property of the list that I assign from running the DBML associated method (related to the stored procedure). Thinking about it, I would think it would be necessary for LINQ to run the query, in order to give me a result for the Count property. Am I not correct?
If I eliminate the call to the list's Count property, I'm still wondering if I might have a problem; if LINQ may still be invoked, because I'm associating the tooltip to the control via a function call?
You are correct, when you call the Count property it iterates over the result set. Not clear on your last question, but the LINQ probably gets called at the point where you populate your DataGrids, way after the tooltip comes into play.
EDIT: however, this does not mean there is anything wrong with deffered execution or your use of it, it executes at the latest possible stage, right when you need the data. If you still want to check the Count ahead of actually fetching all the data, you could have a simple LINQ to SQL function that checks for Any() rows. (Actually Any() is probably what you want more than Count > 0)
You should use Any(), not Count(), but even Any() will cause the query to be executed - after all, it can't determine whether or not there are any rows in the result set without executing the query. But there's executing the query, and there's fetching the result set. Any() will fetch one row, Count() will fetch them all.
That said, I think that having a non-instantaneous operation that occurs on mouseover is just a bad idea. There was a build of Outlook, once, that displayed a helpful tooltip when you moused over the Print button. Less helpfully, it got the data for that tooltip by calling the system function that finds out what printers are available. So you'd be reaching for a menu, and the button would grab the mouse pointer and the UI would freeze for two seconds while it went out and figured out how to display a tooltip that you weren't even asking for. I still hate this program today. Don't be this guy.
A better approach would be to get your tooltip data asynchronously after populating the visible data on the screen. It's easy enough to create a BackgroundWorker that fetches the data into a DataTable, and then make the DataTable available to the view models in the RunWorkerCompleted event handler. (Do it there so that you don't do any updates to UI-bound data on the UI thread.) You can implement a ToolTip property in your view model that returns a default value (probably null, but maybe something like "Fetching data...") if the DataTable containing tool tip data is null, and that calculates the value if it's not. That should work admirably. You can even implement property-change notification so that the ToolTip will still get updated if the user keeps the mouse pointer over it while you're fetching the data.
Alex is correct that calling Count() or Any() will enumerate the LINQ expression causing the query to execute. I would recommend re-thinking your design as you probably don't want a query to the database executed every time the user moves his/her mouse. There is also the issue of the delay to query the database. What might be instantaneous on your dev box with a local database might have a multi-second delay on a heavily loaded server. I would recommend creating a DisplayTooltip() function that takes a lazily evaluated LINQ expression. You can then cache the results or apply other heuristics to decide whether you should actually be querying the database or not.

manage Asynchronous sequence in silverlight?

In my project I want remove some rows first then afterwards insert new rows.
But some times what happens is it inserts the new rows first then afterwards removes the starting rows.
To solve this problem I need to manage the operations in a proper sequence.
Please help me out.
This is a common pattern/problem with Silverlight as pretty much "everything" is asynchronous (for good reasons).
Depending on how your Adds and Removes are triggered, you could queue up tasks (e.g. a list of delegates) and have each task execute the next one off the list when they complete.
The alternative is going to sound a little complex, but the solution we came up with is to create a SequentialAsynchronousTaskManager class that operates in a similar way to the SilverlightTest class which uses EnqueueConditional() methods to add wait conditions and EnqueueCallback()s to execute code.
It basically holds a list of delegates (which can be simple Lambda expressions) and either executes it regularly until it returns true (EnqueueConditional) or just executes some code (EnqueueCallback).

Resources