|
lock(x)
{
DoSomething();
}
What the lock statement does is wrap an object known as a mutual exclusion lock, or mutex, around the variable in the round brackets. the mutex will remain in place while the compound statement attached to the lock keyword is executed. While the mutex is wrapped around a variable, no other thread is permitted access to that variable. If the next thread to gain to gain the time slice attempts to access the variable x, acceess to the variable will be denied. Instead, Windows will simply put the thread to sleep until the mutex has been released.
The above was quoted from "Professional C# 3rd Edition". They also say you must just be aware of deadlocks
Hope that helps you, works perfectly for what I need.
|
|
|
|
|
lock(x) is great, but in your case you should be allowing the database to decide locking, and use 2 connection instances.
|
|
|
|
|
Hi,
In my C# Windows application, I am using so many gifs and jpgs from various website. Say like for Search Button, I searched in Google Image search and found some small search icons, I just downloaded and used. Now Is there any copyright problem I may face When I will publish this software for retail distribution ? Or may any one sue me just to use those images ?
Emran
|
|
|
|
|
Hi!
Yes, you could run into copyright issues there.
You should assume that every image you find on Google image search is copyrighted by the owner and that you are not allowed to freely use it.
I guess some standard icons (for example, those usually used for save, cut/copy/paste, new document, etc.) can be used freely, but I can't draw a precise line what's ok and what isn't.
Regards,
mav
--
Black holes are the places where god divided by 0...
|
|
|
|
|
No one can draw a precise line, as this is different from country to country.
|
|
|
|
|
emran834 wrote: Now Is there any copyright problem I may face When I will publish this software for retail distribution ?
Yes. You must obtain a license for each of the images. Some images are so common they appear to be free - such as the standard file-open/-save/cut/copy/paste however these too come with a licence from Microsoft (you get the redistributable licence with purchases of various developer products, such as Visual Studio)
emran834 wrote: Or may any one sue me just to use those images ?
Yes, the copyright holder. In most countries copyright is automatically assigned, even if the holder doesn't make it explicit. You can reuse copyright material under conditions of "fair use" (or "fair dealing" in the UK) but that refers to using only a small extract in, say, a critique of the work as a whole. I would doubt using a button image from one place as a button image in another would be regarded as fair use.
Bottom line: Find the copyright holder and obtain permission. There may already be a statement of putting the images into the public domain (which is an explicit act in most countries) in which case you can reuse the images already.
ColinMackay.net
Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?
|
|
|
|
|
Thanks Colin,
I extremely appreciate you response.
Regards,
Emran
|
|
|
|
|
Is its possible that two processes in a computer write the information Simultaneously in one XML file(test.XML)
OR
I have to use Database
Please guide me if it is possible.
Ashwani
|
|
|
|
|
I read some where that the XML classes prevent concurrent writes to an XML file. However, you will have other problems. For example, if user "A" modifies one part of the document and user "B" modifies another part, either user "A" or "B" will lose his\her modification dependent upon who wrote to the file last.
|
|
|
|
|
Thanks,
My requirement is to only Insert the new records in a XML file( no modification, deletion)
Ashwani
|
|
|
|
|
You may loose inserted data too!
1. User A loads file.
2. User B loads file.
3. User A inserts data and saves it.
4. User B inserts data and saves it, and User A inserted data is lost.
|
|
|
|
|
Right !
Ok ! I was thinking what about accessing SQL database by Two Threads simultaneously !! I am planning to do something like,
[One thread is generating email address and email message and dump that record in SQL, another thread read email message from that SQL, send email and after that delete that record and scan any new record inserted]
A) Thread A inserts record in SQL
B) Thread B reads a record from SQL, may be the same one which was just inserted by Thread A.
C) Thread B do some work based on the information of that record.
D) Thread B Delete that record after finishing its work, and then look for new record again which was inserted by Thread A.
::: Now, Is there any chance of conflict ? I was worried about one thing, When Thread A is inserting record, does it lock the Database so that Thread B cannot read or delete any other record (not the exactly one which is being inserted) ? Or, When Thread B is reading or Deleting record, does it lock the database so that thread A cannot insert new record ?
Thanks and regards...
-- modified at 0:26 Thursday 23rd February, 2006
|
|
|
|
|
If you were back to the xml file thing, you could have a common write function used by the two threads, and a lock(this) at the start of the function so that the other thread cannot execute that method when one thread is inside it.
After every write into the file, you will have to save the file, so that when the next guy opens it, he will be appending to the already existing content of the xml file.
There has to be more to life than just this
|
|
|
|
|
Hello,
I understood about the XML file, but what about the SQL database file. Do i need to lock the database for similar operation ? Here I dont have multi user, but multi thread. I have two threads, one will insert record, another will read and delete record. at the same time, is there any chance of conflict and Hang ? Do I need to lock the database ?
|
|
|
|
|
If you are using independent connections as suggested already earlier, it all comes down to whether the database maintains integrity of transactions. Yes it does, that is why we pay so much money to use an Oracle or SQL Server database. If thread A is inserting a row into the database, the concerned tables will be locked and thread B will be made to wait before it can read or delete stuff.
So, yes, database is the most convinient method, albeit a tad slower.
There has to be more to life than just this
|
|
|
|
|
Something driving me insane that I cannot find in the options anywhere is how to turn of the auto-close functions so that when I type a quote like " I don't get another quote instantly. It drives me nuts. How can I disable that?
Thanks,
Rex
I only read CP for the articles.
Code-frog System Architects, Inc.
|
|
|
|
|
in development environment of .net, when design mode, Application.StartPath is the path of development environment, but run the application the path is program's path. I want to get the path what it is always the application the path is program's path. How to do it?
|
|
|
|
|
When you're in design mode, there is no program to have a path, the IDE is actually running your code.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
So....
I get UTC time from a C server in a 32-bit unsigned integer (aka long). Let's say for sake of discussion the UTC time is in the following variable:
uint utcTime;
And let's say I also have:
DateTime locTime;
And of course, there seems to be no combination of DateTime() methods that will address my simple need.
Any ideas?
Mike Luster
|
|
|
|
|
I'm not quite sure what you're asking. Are you trying to turn your uint into a DateTime?
Edit:
Boy, MSDN[^] is sure helpful:
Convert.ToDateTime Method (UInt32): Return Value
This conversion is not supported. No value is returned.
Jon Sagara
Look at him. He runs like a Welshman. Doesn't he run like a Welshman? Doesn't he? I think he runs like a Welshman.
Sagara.org | Blog | My Articles
-- modified at 21:02 Wednesday 22nd February, 2006
|
|
|
|
|
Yea, but the Uint32 that holds the time contains UTC formatted time from a C based program sent over ethernet. I pull it out of a packet, decode and now I need to be able to convert it to DateTime to be able to use it the C# app. Thanks
|
|
|
|
|
I believe the Uint32 value is the number of seconds since January 1, 1970 (most likely). Thus, to get the correct DateTime, you must initialize the your DateTime variable to January 1, 1970 and add the Uint32 seconds to it.
-- modified at 23:33 Wednesday 22nd February, 2006
|
|
|
|
|
Thanks for the tip George. Here's what I have encountered:
uint uval
DateTime dt = new DateTime( 1970, 1, 1 );<br />
dt.AddSeconds( uval );<br />
Console.WriteLine( "Date is {0}", dt.ToLocalTime( ) );<br />
Here is a real result to illustrate:
2/23/2006 11:49:15 AM: Raw POSIX Date-Time: 1140713262, Conversion: 12/31/1969 7:00:00 PM<br />
I printed out the 2K/POSIX value, followed by the conversion to local time. I was thinking the conversion should have yielded a date time along the lines of the message preamble 2/23/2006 11:49:15 AM give or take a few seconds in terms of synchronization w/ the server time. However it was off by just a few years!
I was curious to what was happening so I changed the dt.ToLocalTime() to dt.ToUniversalTime() and it yields 1/1/1970 5:00:00 AM. Since the DateTime is initialized to DateTime( 1970, 1, 1 ) this begs the question of just what is dt.AddSeconds( Convert.ToDouble( uval ) ) adding?
Interestingly if I print out dt without the ToLocalTime() conversion it yields 1/1/1970 12:00 AM. That means the seconds added should be 18000 to result in 1/1/1970 5:00:00 AM. This is why I am a little suspect of the 2K/POSIX value, it doesn’t make sense to me to added a 10 digit value and only yield a 5 hour delta since the 1970 epoch.
Now I have quadruple checked the packet decoding logic and I can tell you that all fields before and those after in a variable length packet are all correct and at the appropriate offset. Thus I have revisited the decoding and it appears to be OK in every respect less the date time situation.
At this juncture I am wondering if the 2K flavor of UTC requires any special consideration. Or is there something I am just plain missing? I am stumped.
Final FYI – according to the documentation from the Server vendor, the date-time is stored as followed:
TIME 4 Bytes A date/time, expressed as the number of seconds since Midnight January 1, 1970 Coordinated Universal Time (UTC).
So at least I know I am playing with the right deck,
Mike Luster
|
|
|
|
|
I have succeeded in getting my code to successfully do mouse clicks on a commercial program, running at a specific screen location, using this code:
SetForegroundWindow(targetWindowHandle);
input.type = INPUT_MOUSE;
input.mi.dx = 900;
input.mi.dy = 50;
input.mi.mouseData = 0;
input.mi.time = 0;
input.mi.dwFlags = 0;
input.mi.dwExtraInfo = 0;
SetCursorPos(input.mi.dx, input.mi.dy);
input.mi.dwFlags = (MOUSEEVENTF_ABSOLUTE+MOUSEEVENTF_LEFTDOWN);
resSendInput = SendInput(1, ref input, Marshal.SizeOf(input));
input.mi.dwFlags = (MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_LEFTUP);
resSendInput = SendInput(1, ref input, Marshal.SizeOf(input));
The problem is, of course, if the window is moved, the clicks miss. I would love to be able to do the clicks relative to position 0,0 of the window pointed at by targetWindowHandle instead of relative to position 0,0 of the screen.
(I hope that statement makes sense to you guys.)
Any suggestions?
Thanks.
-- modified at 20:11 Wednesday 22nd February, 2006
|
|
|
|
|
for those interested ...
The solution was rather trivial after i found "GetWindowRect" in User32:
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern int GetWindowRect(int hwnd, ref RECT rc);
// Get the target window's screen coordinates
RECT rect = new RECT();
int hwnd = GetWindowRect((int)targetWindowHandle, ref rect);
Persistence pays ...
---
-- modified at 15:34 Thursday 23rd February, 2006
|
|
|
|
|